1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi, Guru Kathiresan, Bo Yang
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #ifdef wxUSE_GENERICDATAVIEWCTRL
26 #include "wx/msw/private.h"
27 #include "wx/msw/wrapwin.h"
28 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/msgdlg.h"
36 #include "wx/dcscreen.h"
40 #include "wx/stockitem.h"
41 #include "wx/calctrl.h"
42 #include "wx/popupwin.h"
43 #include "wx/renderer.h"
44 #include "wx/dcbuffer.h"
47 #include "wx/listimpl.cpp"
48 #include "wx/imaglist.h"
49 #include "wx/headerctrl.h"
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
58 static const int SCROLL_UNIT_X
= 15;
60 // the cell padding on the left/right
61 static const int PADDING_RIGHTLEFT
= 3;
63 // the expander space margin
64 static const int EXPANDER_MARGIN
= 4;
67 static const int EXPANDER_OFFSET
= 4;
69 static const int EXPANDER_OFFSET
= 1;
72 //Below is the compare stuff
73 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
74 static wxDataViewModel
* g_model
;
75 static int g_column
= -2;
76 static bool g_asending
= true;
78 //-----------------------------------------------------------------------------
79 // wxDataViewHeaderWindow
80 //-----------------------------------------------------------------------------
82 class wxDataViewHeaderWindow
: public wxHeaderCtrl
85 wxDataViewHeaderWindow(wxDataViewCtrl
*parent
)
86 : wxHeaderCtrl(parent
)
90 wxDataViewCtrl
*GetOwner() const
91 { return static_cast<wxDataViewCtrl
*>(GetParent()); }
94 // implement/override wxHeaderCtrl functions by forwarding them to the main
96 virtual const wxHeaderColumn
& GetColumn(unsigned int idx
) const
98 return *(GetOwner()->GetColumn(idx
));
101 virtual bool UpdateColumnWidthToFit(unsigned int idx
, int widthTitle
)
103 wxDataViewCtrl
* const owner
= GetOwner();
105 int widthContents
= owner
->GetBestColumnWidth(idx
);
106 owner
->GetColumn(idx
)->SetWidth(wxMax(widthTitle
, widthContents
));
107 owner
->OnColumnChange(idx
);
113 bool SendEvent(wxEventType type
, unsigned int n
)
115 wxDataViewCtrl
* const owner
= GetOwner();
116 wxDataViewEvent
event(type
, owner
->GetId());
118 event
.SetEventObject(owner
);
120 event
.SetDataViewColumn(owner
->GetColumn(n
));
121 event
.SetModel(owner
->GetModel());
123 // for events created by wxDataViewHeaderWindow the
124 // row / value fields are not valid
125 return owner
->GetEventHandler()->ProcessEvent(event
);
128 void OnClick(wxHeaderCtrlEvent
& event
)
130 const unsigned idx
= event
.GetColumn();
132 if ( SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, idx
) )
135 // default handling for the column click is to sort by this column or
136 // toggle its sort order
137 wxDataViewCtrl
* const owner
= GetOwner();
138 wxDataViewColumn
* const col
= owner
->GetColumn(idx
);
139 if ( !col
->IsSortable() )
141 // no default handling for non-sortable columns
146 if ( col
->IsSortKey() )
148 // already using this column for sorting, just change the order
149 col
->ToggleSortOrder();
151 else // not using this column for sorting yet
153 // first unset the old sort column if any
154 int oldSortKey
= owner
->GetSortingColumnIndex();
155 if ( oldSortKey
!= wxNOT_FOUND
)
157 owner
->GetColumn(oldSortKey
)->UnsetAsSortKey();
158 owner
->OnColumnChange(oldSortKey
);
161 owner
->SetSortingColumnIndex(idx
);
165 wxDataViewModel
* const model
= owner
->GetModel();
169 owner
->OnColumnChange(idx
);
172 void OnRClick(wxHeaderCtrlEvent
& event
)
174 if ( !SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
179 void OnEndResize(wxHeaderCtrlEvent
& event
)
181 wxDataViewCtrl
* const owner
= GetOwner();
183 const unsigned col
= event
.GetColumn();
184 owner
->GetColumn(col
)->SetWidth(event
.GetWidth());
185 GetOwner()->OnColumnChange(col
);
188 void OnEndReorder(wxHeaderCtrlEvent
& event
)
190 wxDataViewCtrl
* const owner
= GetOwner();
191 owner
->ColumnMoved(owner
->GetColumn(event
.GetColumn()),
192 event
.GetNewOrder());
195 DECLARE_EVENT_TABLE()
196 DECLARE_NO_COPY_CLASS(wxDataViewHeaderWindow
)
199 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
, wxHeaderCtrl
)
200 EVT_HEADER_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnClick
)
201 EVT_HEADER_RIGHT_CLICK(wxID_ANY
, wxDataViewHeaderWindow::OnRClick
)
203 EVT_HEADER_END_RESIZE(wxID_ANY
, wxDataViewHeaderWindow::OnEndResize
)
205 EVT_HEADER_END_REORDER(wxID_ANY
, wxDataViewHeaderWindow::OnEndReorder
)
208 //-----------------------------------------------------------------------------
209 // wxDataViewRenameTimer
210 //-----------------------------------------------------------------------------
212 class wxDataViewRenameTimer
: public wxTimer
215 wxDataViewMainWindow
*m_owner
;
218 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
222 //-----------------------------------------------------------------------------
223 // wxDataViewTreeNode
224 //-----------------------------------------------------------------------------
225 class wxDataViewTreeNode
;
226 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
227 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
229 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
230 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
232 class wxDataViewTreeNode
235 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
242 m_hasChildren
= false;
246 ~wxDataViewTreeNode()
250 wxDataViewTreeNode
* GetParent() const { return m_parent
; }
251 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
252 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
253 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
255 void AddNode( wxDataViewTreeNode
* node
)
257 m_leaves
.Add( node
->GetItem().GetID() );
259 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
262 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
264 void AddLeaf( void * leaf
)
266 m_leaves
.Add( leaf
);
268 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
271 wxDataViewItem
& GetItem() { return m_item
; }
272 const wxDataViewItem
& GetItem() const { return m_item
; }
273 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
275 unsigned int GetChildrenNumber() const { return m_leaves
.GetCount(); }
276 unsigned int GetNodeNumber() const { return m_nodes
.GetCount(); }
277 int GetIndentLevel() const
280 const wxDataViewTreeNode
* node
= this;
281 while( node
->GetParent()->GetParent() != NULL
)
283 node
= node
->GetParent();
296 int len
= m_nodes
.GetCount();
298 for ( int i
= 0;i
< len
; i
++)
299 sum
+= m_nodes
[i
]->GetSubTreeCount();
301 sum
+= m_leaves
.GetCount();
304 ChangeSubTreeCount(-sum
);
310 ChangeSubTreeCount(sum
);
313 bool HasChildren() const { return m_hasChildren
; }
314 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
316 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
317 int GetSubTreeCount() const { return m_subTreeCount
; }
318 void ChangeSubTreeCount( int num
)
322 m_subTreeCount
+= num
;
324 m_parent
->ChangeSubTreeCount(num
);
331 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
332 int len
= m_nodes
.GetCount();
333 for (int i
= 0; i
< len
; i
++)
334 m_nodes
[i
]->Resort();
335 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
340 wxDataViewTreeNode
*m_parent
;
341 wxDataViewTreeNodes m_nodes
;
342 wxDataViewTreeLeaves m_leaves
;
343 wxDataViewItem m_item
;
349 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
351 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
354 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
356 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
361 //-----------------------------------------------------------------------------
362 // wxDataViewMainWindow
363 //-----------------------------------------------------------------------------
365 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
367 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
368 WX_DEFINE_LIST(ItemList
)
370 class wxDataViewMainWindow
: public wxWindow
373 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
375 const wxPoint
&pos
= wxDefaultPosition
,
376 const wxSize
&size
= wxDefaultSize
,
377 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
378 virtual ~wxDataViewMainWindow();
380 bool IsVirtualList() const { return m_root
== NULL
; }
382 // notifications from wxDataViewModel
383 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
384 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
385 bool ItemChanged( const wxDataViewItem
&item
);
386 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
390 if (!IsVirtualList())
400 g_model
= GetOwner()->GetModel();
401 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
404 if (g_model
->HasDefaultCompare())
412 g_column
= col
->GetModelColumn();
413 g_asending
= col
->IsSortOrderAscending();
416 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
417 wxDataViewCtrl
*GetOwner() { return m_owner
; }
418 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
420 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
421 void OnPaint( wxPaintEvent
&event
);
422 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
423 void OnChar( wxKeyEvent
&event
);
424 void OnMouse( wxMouseEvent
&event
);
425 void OnSetFocus( wxFocusEvent
&event
);
426 void OnKillFocus( wxFocusEvent
&event
);
428 void UpdateDisplay();
429 void RecalculateDisplay();
430 void OnInternalIdle();
432 void OnRenameTimer();
434 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
435 void ScrollTo( int rows
, int column
);
437 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
438 void ChangeCurrentRow( unsigned int row
);
440 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
441 bool IsEmpty() { return GetRowCount() == 0; }
443 int GetCountPerPage() const;
444 int GetEndOfLastCol() const;
445 unsigned int GetFirstVisibleRow() const;
446 //I change this method to un const because in the tree view, the displaying number of the tree are changing along with the expanding/collapsing of the tree nodes
447 unsigned int GetLastVisibleRow();
448 unsigned int GetRowCount();
450 wxDataViewItem
GetSelection() const;
451 wxDataViewSelection
GetSelections(){ return m_selection
; }
452 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
453 void Select( const wxArrayInt
& aSelections
);
454 void SelectAllRows( bool on
);
455 void SelectRow( unsigned int row
, bool on
);
456 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
457 void ReverseRowSelection( unsigned int row
);
458 bool IsRowSelected( unsigned int row
);
459 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
461 void RefreshRow( unsigned int row
);
462 void RefreshRows( unsigned int from
, unsigned int to
);
463 void RefreshRowsAfter( unsigned int firstRow
);
465 // returns the colour to be used for drawing the rules
466 wxColour
GetRuleColour() const
468 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
471 wxRect
GetLineRect( unsigned int row
) const;
473 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
474 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
475 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
477 //Some useful functions for row and item mapping
478 wxDataViewItem
GetItemByRow( unsigned int row
) const;
479 int GetRowByItem( const wxDataViewItem
& item
) const;
481 //Methods for building the mapping tree
482 void BuildTree( wxDataViewModel
* model
);
484 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
485 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
487 void Expand( unsigned int row
) { OnExpanding( row
); }
488 void Collapse( unsigned int row
) { OnCollapsing( row
); }
489 bool IsExpanded( unsigned int row
) const;
491 bool EnableDragSource( const wxDataFormat
&format
);
492 bool EnableDropTarget( const wxDataFormat
&format
);
494 void RemoveDropHint();
495 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
496 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
497 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
501 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
502 //We did not need this temporarily
503 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
505 int RecalculateCount();
507 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
508 void OnExpanding( unsigned int row
);
509 void OnCollapsing( unsigned int row
);
511 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
514 wxDataViewCtrl
*m_owner
;
518 wxDataViewColumn
*m_currentCol
;
519 unsigned int m_currentRow
;
520 wxDataViewSelection m_selection
;
522 wxDataViewRenameTimer
*m_renameTimer
;
531 wxDataFormat m_dragFormat
;
534 wxDataFormat m_dropFormat
;
536 unsigned int m_dropHintLine
;
538 // for double click logic
539 unsigned int m_lineLastClicked
,
540 m_lineBeforeLastClicked
,
541 m_lineSelectSingleOnUp
;
543 // the pen used to draw horiz/vertical rules
546 // the pen used to draw the expander and the lines
549 //This is the tree structure of the model
550 wxDataViewTreeNode
* m_root
;
552 //This is the tree node under the cursor
553 wxDataViewTreeNode
* m_underMouse
;
555 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
556 DECLARE_EVENT_TABLE()
559 // ---------------------------------------------------------
560 // wxGenericDataViewModelNotifier
561 // ---------------------------------------------------------
563 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
566 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
567 { m_mainWindow
= mainWindow
; }
569 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
570 { return m_mainWindow
->ItemAdded( parent
, item
); }
571 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
572 { return m_mainWindow
->ItemDeleted( parent
, item
); }
573 virtual bool ItemChanged( const wxDataViewItem
& item
)
574 { return m_mainWindow
->ItemChanged(item
); }
575 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
576 { return m_mainWindow
->ValueChanged( item
, col
); }
577 virtual bool Cleared()
578 { return m_mainWindow
->Cleared(); }
579 virtual void Resort()
580 { m_mainWindow
->Resort(); }
582 wxDataViewMainWindow
*m_mainWindow
;
585 // ---------------------------------------------------------
586 // wxDataViewRenderer
587 // ---------------------------------------------------------
589 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
591 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
592 wxDataViewCellMode mode
,
594 wxDataViewRendererBase( varianttype
, mode
, align
)
603 wxDataViewRenderer::~wxDataViewRenderer()
609 wxDC
*wxDataViewRenderer::GetDC()
613 if (GetOwner() == NULL
)
615 if (GetOwner()->GetOwner() == NULL
)
617 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
623 void wxDataViewRenderer::SetAlignment( int align
)
628 int wxDataViewRenderer::GetAlignment() const
633 int wxDataViewRenderer::CalculateAlignment() const
635 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
637 if (GetOwner() == NULL
)
638 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
640 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
646 // ---------------------------------------------------------
647 // wxDataViewCustomRenderer
648 // ---------------------------------------------------------
650 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
652 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
653 wxDataViewCellMode mode
, int align
) :
654 wxDataViewRenderer( varianttype
, mode
, align
)
658 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
660 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
661 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
662 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
663 view
->GetForegroundColour();
664 dc
->SetTextForeground(col
);
665 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
668 // ---------------------------------------------------------
669 // wxDataViewTextRenderer
670 // ---------------------------------------------------------
672 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
674 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
675 wxDataViewCellMode mode
, int align
) :
676 wxDataViewCustomRenderer( varianttype
, mode
, align
)
680 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
682 m_text
= value
.GetString();
687 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
692 bool wxDataViewTextRenderer::HasEditorCtrl()
697 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
698 wxRect labelRect
, const wxVariant
&value
)
700 return new wxTextCtrl( parent
, wxID_ANY
, value
,
701 wxPoint(labelRect
.x
,labelRect
.y
),
702 wxSize(labelRect
.width
,labelRect
.height
) );
705 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
707 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
708 value
= text
->GetValue();
712 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
714 RenderText( m_text
, 0, cell
, dc
, state
);
718 wxSize
wxDataViewTextRenderer::GetSize() const
720 const wxDataViewCtrl
*view
= GetView();
724 view
->GetTextExtent( m_text
, &x
, &y
);
725 return wxSize( x
, y
);
727 return wxSize(80,20);
730 // ---------------------------------------------------------
731 // wxDataViewTextRendererAttr
732 // ---------------------------------------------------------
734 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
736 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
737 wxDataViewCellMode mode
, int align
) :
738 wxDataViewTextRenderer( varianttype
, mode
, align
)
743 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
750 if (m_attr
.HasColour())
752 colour
= dc
->GetTextForeground();
753 dc
->SetTextForeground( m_attr
.GetColour() );
756 if (m_attr
.GetBold() || m_attr
.GetItalic())
758 font
= dc
->GetFont();
759 wxFont myfont
= font
;
760 if (m_attr
.GetBold())
761 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
762 if (m_attr
.GetItalic())
763 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
764 dc
->SetFont( myfont
);
768 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
773 if (m_attr
.HasColour())
774 dc
->SetTextForeground( colour
);
776 if (m_attr
.GetBold() || m_attr
.GetItalic())
784 // ---------------------------------------------------------
785 // wxDataViewBitmapRenderer
786 // ---------------------------------------------------------
788 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
790 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
791 wxDataViewCellMode mode
, int align
) :
792 wxDataViewCustomRenderer( varianttype
, mode
, align
)
796 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
798 if (value
.GetType() == wxT("wxBitmap"))
800 if (value
.GetType() == wxT("wxIcon"))
806 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
811 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
814 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
815 else if (m_icon
.Ok())
816 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
821 wxSize
wxDataViewBitmapRenderer::GetSize() const
824 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
825 else if (m_icon
.Ok())
826 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
828 return wxSize(16,16);
831 // ---------------------------------------------------------
832 // wxDataViewToggleRenderer
833 // ---------------------------------------------------------
835 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
837 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
838 wxDataViewCellMode mode
, int align
) :
839 wxDataViewCustomRenderer( varianttype
, mode
, align
)
844 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
846 m_toggle
= value
.GetBool();
851 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
856 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
858 // User wxRenderer here
861 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
863 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
868 flags
|= wxCONTROL_CHECKED
;
869 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
870 flags
|= wxCONTROL_DISABLED
;
872 wxRendererNative::Get().DrawCheckBox(
873 GetOwner()->GetOwner(),
881 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
882 wxDataViewModel
*model
,
883 const wxDataViewItem
& item
, unsigned int col
)
885 bool value
= !m_toggle
;
886 wxVariant variant
= value
;
887 model
->SetValue( variant
, item
, col
);
888 model
->ValueChanged( item
, col
);
892 wxSize
wxDataViewToggleRenderer::GetSize() const
894 return wxSize(20,20);
897 // ---------------------------------------------------------
898 // wxDataViewProgressRenderer
899 // ---------------------------------------------------------
901 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
903 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
904 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
905 wxDataViewCustomRenderer( varianttype
, mode
, align
)
911 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
915 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
917 m_value
= (long) value
;
919 if (m_value
< 0) m_value
= 0;
920 if (m_value
> 100) m_value
= 100;
925 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
927 value
= (long) m_value
;
931 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
933 double pct
= (double)m_value
/ 100.0;
935 bar
.width
= (int)(cell
.width
* pct
);
936 dc
->SetPen( *wxTRANSPARENT_PEN
);
937 dc
->SetBrush( *wxBLUE_BRUSH
);
938 dc
->DrawRectangle( bar
);
940 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
941 dc
->SetPen( *wxBLACK_PEN
);
942 dc
->DrawRectangle( cell
);
947 wxSize
wxDataViewProgressRenderer::GetSize() const
949 return wxSize(40,12);
952 // ---------------------------------------------------------
953 // wxDataViewDateRenderer
954 // ---------------------------------------------------------
956 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
958 #if wxUSE_DATE_RENDERER_POPUP
960 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
963 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
964 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
965 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
970 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
971 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
972 sizer
->Add( m_cal
, 1, wxGROW
);
977 void OnCalendar( wxCalendarEvent
&event
);
979 wxCalendarCtrl
*m_cal
;
980 wxDataViewModel
*m_model
;
982 const wxDataViewItem
& m_item
;
985 virtual void OnDismiss()
990 DECLARE_EVENT_TABLE()
993 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
994 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
997 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
999 wxDateTime date
= event
.GetDate();
1000 wxVariant value
= date
;
1001 m_model
->SetValue( value
, m_item
, m_col
);
1002 m_model
->ValueChanged( m_item
, m_col
);
1006 #endif // wxUSE_DATE_RENDERER_POPUP
1008 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1010 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1011 wxDataViewCellMode mode
, int align
) :
1012 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1016 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1018 m_date
= value
.GetDateTime();
1023 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1029 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1031 wxString tmp
= m_date
.FormatDate();
1032 RenderText( tmp
, 0, cell
, dc
, state
);
1036 wxSize
wxDataViewDateRenderer::GetSize() const
1038 const wxDataViewCtrl
* view
= GetView();
1039 wxString tmp
= m_date
.FormatDate();
1041 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1042 return wxSize(x
,y
+d
);
1045 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1046 const wxDataViewItem
& item
, unsigned int col
)
1049 model
->GetValue( variant
, item
, col
);
1050 wxDateTime value
= variant
.GetDateTime();
1052 #if wxUSE_DATE_RENDERER_POPUP
1053 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1054 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1055 wxPoint pos
= wxGetMousePosition();
1058 popup
->Popup( popup
->m_cal
);
1059 #else // !wxUSE_DATE_RENDERER_POPUP
1060 wxMessageBox(value
.Format());
1061 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1065 // ---------------------------------------------------------
1066 // wxDataViewIconTextRenderer
1067 // ---------------------------------------------------------
1069 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1071 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1072 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1073 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1076 SetAlignment(align
);
1079 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1083 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1089 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1094 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1097 const wxIcon
&icon
= m_value
.GetIcon();
1100 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1101 xoffset
= icon
.GetWidth()+4;
1104 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1109 wxSize
wxDataViewIconTextRenderer::GetSize() const
1111 const wxDataViewCtrl
*view
= GetView();
1112 if (!m_value
.GetText().empty())
1115 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1117 if (m_value
.GetIcon().IsOk())
1118 x
+= m_value
.GetIcon().GetWidth() + 4;
1119 return wxSize( x
, y
);
1121 return wxSize(80,20);
1125 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1126 wxRect
WXUNUSED(labelRect
),
1127 const wxVariant
& WXUNUSED(value
))
1133 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1134 wxVariant
& WXUNUSED(value
))
1139 //-----------------------------------------------------------------------------
1140 // wxDataViewDropTarget
1141 //-----------------------------------------------------------------------------
1143 class wxBitmapCanvas
: public wxWindow
1146 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1147 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1150 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1153 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1156 dc
.DrawBitmap( m_bitmap
, 0, 0);
1162 class wxDataViewDropSource
: public wxDropSource
1165 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1173 ~wxDataViewDropSource()
1178 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1180 wxPoint pos
= wxGetMousePosition();
1184 int liney
= m_win
->GetLineStart( m_row
);
1186 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1187 m_win
->ClientToScreen( &linex
, &liney
);
1188 m_dist_x
= pos
.x
- linex
;
1189 m_dist_y
= pos
.y
- liney
;
1192 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1194 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1195 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1197 wxFRAME_TOOL_WINDOW
|
1198 wxFRAME_FLOAT_ON_PARENT
|
1199 wxFRAME_NO_TASKBAR
|
1201 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1206 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1207 m_hint
->SetTransparent( 128 );
1213 wxDataViewMainWindow
*m_win
;
1216 int m_dist_x
,m_dist_y
;
1220 class wxDataViewDropTarget
: public wxDropTarget
1223 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1229 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1231 wxDataFormat format
= GetMatchingPair();
1232 if (format
== wxDF_INVALID
)
1234 return m_win
->OnDragOver( format
, x
, y
, def
);
1237 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1239 wxDataFormat format
= GetMatchingPair();
1240 if (format
== wxDF_INVALID
)
1242 return m_win
->OnDrop( format
, x
, y
);
1245 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1247 wxDataFormat format
= GetMatchingPair();
1248 if (format
== wxDF_INVALID
)
1252 return m_win
->OnData( format
, x
, y
, def
);
1255 virtual void OnLeave()
1256 { m_win
->OnLeave(); }
1258 wxDataViewMainWindow
*m_win
;
1261 //-----------------------------------------------------------------------------
1262 // wxDataViewRenameTimer
1263 //-----------------------------------------------------------------------------
1265 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1270 void wxDataViewRenameTimer::Notify()
1272 m_owner
->OnRenameTimer();
1275 //-----------------------------------------------------------------------------
1276 // wxDataViewMainWindow
1277 //-----------------------------------------------------------------------------
1279 //The tree building helper, declared firstly
1280 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1282 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1284 if (row1
> row2
) return 1;
1285 if (row1
== row2
) return 0;
1290 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1292 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1293 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1294 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1295 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1296 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1297 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1300 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1301 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1302 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1303 m_selection( wxDataViewSelectionCmp
)
1308 m_lastOnSame
= false;
1309 m_renameTimer
= new wxDataViewRenameTimer( this );
1311 // TODO: user better initial values/nothing selected
1312 m_currentCol
= NULL
;
1315 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1318 m_dragStart
= wxPoint(0,0);
1319 m_lineLastClicked
= (unsigned int) -1;
1320 m_lineBeforeLastClicked
= (unsigned int) -1;
1321 m_lineSelectSingleOnUp
= (unsigned int) -1;
1323 m_dragEnabled
= false;
1324 m_dropEnabled
= false;
1326 m_dropHintLine
= (unsigned int) -1;
1330 SetBackgroundColour( *wxWHITE
);
1332 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1334 m_penRule
= wxPen(GetRuleColour());
1336 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1337 m_penExpander
= wxPen(wxColour(0,0,0));
1338 //Some new added code to deal with the tree structure
1339 m_root
= new wxDataViewTreeNode( NULL
);
1340 m_root
->SetHasChildren(true);
1342 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1344 m_underMouse
= NULL
;
1348 wxDataViewMainWindow::~wxDataViewMainWindow()
1351 delete m_renameTimer
;
1354 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1356 m_dragFormat
= format
;
1357 m_dragEnabled
= format
!= wxDF_INVALID
;
1362 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1364 m_dropFormat
= format
;
1365 m_dropEnabled
= format
!= wxDF_INVALID
;
1368 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1373 void wxDataViewMainWindow::RemoveDropHint()
1378 RefreshRow( m_dropHintLine
);
1379 m_dropHintLine
= (unsigned int) -1;
1383 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1387 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1388 unsigned int row
= GetLineAt( yy
);
1390 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1396 wxDataViewItem item
= GetItemByRow( row
);
1398 wxDataViewModel
*model
= GetOwner()->GetModel();
1400 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1401 event
.SetEventObject( m_owner
);
1402 event
.SetItem( item
);
1403 event
.SetModel( model
);
1404 event
.SetDataFormat( format
);
1405 if (!m_owner
->HandleWindowEvent( event
))
1411 if (!event
.IsAllowed())
1418 if (m_dropHint
&& (row
!= m_dropHintLine
))
1419 RefreshRow( m_dropHintLine
);
1421 m_dropHintLine
= row
;
1427 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1433 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1434 unsigned int row
= GetLineAt( yy
);
1436 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1439 wxDataViewItem item
= GetItemByRow( row
);
1441 wxDataViewModel
*model
= GetOwner()->GetModel();
1443 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1444 event
.SetEventObject( m_owner
);
1445 event
.SetItem( item
);
1446 event
.SetModel( model
);
1447 event
.SetDataFormat( format
);
1448 if (!m_owner
->HandleWindowEvent( event
))
1451 if (!event
.IsAllowed())
1457 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1461 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1462 unsigned int row
= GetLineAt( yy
);
1464 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1467 wxDataViewItem item
= GetItemByRow( row
);
1469 wxDataViewModel
*model
= GetOwner()->GetModel();
1471 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1473 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1474 event
.SetEventObject( m_owner
);
1475 event
.SetItem( item
);
1476 event
.SetModel( model
);
1477 event
.SetDataFormat( format
);
1478 event
.SetDataSize( obj
->GetSize() );
1479 event
.SetDataBuffer( obj
->GetData() );
1480 if (!m_owner
->HandleWindowEvent( event
))
1483 if (!event
.IsAllowed())
1489 void wxDataViewMainWindow::OnLeave()
1494 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1496 int height
= GetLineHeight( row
);
1498 unsigned int cols
= GetOwner()->GetColumnCount();
1500 for (col
= 0; col
< cols
; col
++)
1502 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1503 if (column
->IsHidden())
1504 continue; // skip it!
1505 width
+= column
->GetWidth();
1509 if (!IsVirtualList())
1511 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1512 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1513 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1517 wxBitmap
bitmap( width
, height
);
1518 wxMemoryDC
dc( bitmap
);
1519 dc
.SetFont( GetFont() );
1520 dc
.SetPen( *wxBLACK_PEN
);
1521 dc
.SetBrush( *wxWHITE_BRUSH
);
1522 dc
.DrawRectangle( 0,0,width
,height
);
1524 wxDataViewModel
*model
= m_owner
->GetModel();
1526 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1529 // TODO-RTL: last column for RTL support
1530 expander
= GetOwner()->GetColumnAt( 0 );
1531 GetOwner()->SetExpanderColumn(expander
);
1536 for (col
= 0; col
< cols
; col
++)
1538 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1539 wxDataViewRenderer
*cell
= column
->GetRenderer();
1541 if (column
->IsHidden())
1542 continue; // skip it!
1544 width
= column
->GetWidth();
1546 if (column
== expander
)
1550 wxDataViewItem item
= GetItemByRow( row
);
1551 model
->GetValue( value
, item
, column
->GetModelColumn());
1552 cell
->SetValue( value
);
1554 if (cell
->GetWantsAttr())
1556 wxDataViewItemAttr attr
;
1557 bool ret
= model
->GetAttr( item
, column
->GetModelColumn(), attr
);
1559 cell
->SetAttr( attr
);
1560 cell
->SetHasAttr( ret
);
1563 wxSize size
= cell
->GetSize();
1564 size
.x
= wxMin( 2*PADDING_RIGHTLEFT
+ size
.x
, width
);
1566 wxRect
item_rect(x
, 0, size
.x
, size
.y
);
1568 int align
= cell
->CalculateAlignment();
1569 // horizontal alignment:
1571 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1572 item_rect
.x
= x
+ (width
/ 2) - (size
.x
/ 2);
1573 else if (align
& wxALIGN_RIGHT
)
1574 item_rect
.x
= x
+ width
- size
.x
;
1575 //else: wxALIGN_LEFT is the default
1577 // vertical alignment:
1579 if (align
& wxALIGN_CENTER_VERTICAL
)
1580 item_rect
.y
= (height
/ 2) - (size
.y
/ 2);
1581 else if (align
& wxALIGN_BOTTOM
)
1582 item_rect
.y
= height
- size
.y
;
1583 //else: wxALIGN_TOP is the default
1586 item_rect
.x
+= PADDING_RIGHTLEFT
;
1587 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1589 //dc.SetClippingRegion( item_rect );
1590 cell
->Render( item_rect
, &dc
, 0 );
1591 //dc.DestroyClippingRegion();
1599 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1601 wxDataViewModel
*model
= GetOwner()->GetModel();
1602 wxAutoBufferedPaintDC
dc( this );
1605 dc
.SetPen( *wxTRANSPARENT_PEN
);
1606 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1607 dc
.SetBrush( *wxWHITE_BRUSH
);
1608 wxSize
size( GetClientSize() );
1609 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1613 GetOwner()->PrepareDC( dc
);
1614 dc
.SetFont( GetFont() );
1616 wxRect update
= GetUpdateRegion().GetBox();
1617 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1619 // compute which items needs to be redrawn
1620 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1621 unsigned int item_count
=
1622 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1623 (int)(GetRowCount( ) - item_start
));
1624 unsigned int item_last
= item_start
+ item_count
;
1626 // compute which columns needs to be redrawn
1627 unsigned int cols
= GetOwner()->GetColumnCount();
1628 unsigned int col_start
= 0;
1629 unsigned int x_start
;
1630 for (x_start
= 0; col_start
< cols
; col_start
++)
1632 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1633 if (col
->IsHidden())
1634 continue; // skip it!
1636 unsigned int w
= col
->GetWidth();
1637 if (x_start
+w
>= (unsigned int)update
.x
)
1643 unsigned int col_last
= col_start
;
1644 unsigned int x_last
= x_start
;
1645 for (; col_last
< cols
; col_last
++)
1647 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1648 if (col
->IsHidden())
1649 continue; // skip it!
1651 if (x_last
> (unsigned int)update
.GetRight())
1654 x_last
+= col
->GetWidth();
1657 // Draw horizontal rules if required
1658 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1660 dc
.SetPen(m_penRule
);
1661 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1663 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1665 int y
= GetLineStart( i
);
1666 dc
.DrawLine(x_start
, y
, x_last
, y
);
1670 // Draw vertical rules if required
1671 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1673 dc
.SetPen(m_penRule
);
1674 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1677 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1679 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1680 if (col
->IsHidden())
1681 continue; // skip it
1683 dc
.DrawLine(x
, GetLineStart( item_start
),
1684 x
, GetLineStart( item_last
) );
1686 x
+= col
->GetWidth();
1689 // Draw last vertical rule
1690 dc
.DrawLine(x
, GetLineStart( item_start
),
1691 x
, GetLineStart( item_last
) );
1694 // redraw the background for the items which are selected/current
1695 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1697 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1698 if (selected
|| item
== m_currentRow
)
1700 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1701 if (item
== m_currentRow
)
1702 flags
|= wxCONTROL_CURRENT
;
1704 flags
|= wxCONTROL_FOCUSED
;
1706 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1707 wxRendererNative::Get().DrawItemSelectionRect
1719 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
), x_last
, GetLineHeight( m_dropHintLine
) );
1720 dc
.SetPen( *wxBLACK_PEN
);
1721 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1722 dc
.DrawRectangle( rect
);
1725 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1728 // TODO-RTL: last column for RTL support
1729 expander
= GetOwner()->GetColumnAt( 0 );
1730 GetOwner()->SetExpanderColumn(expander
);
1733 // redraw all cells for all rows which must be repainted and all columns
1735 cell_rect
.x
= x_start
;
1736 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1739 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1740 wxDataViewRenderer
*cell
= col
->GetRenderer();
1741 cell_rect
.width
= col
->GetWidth();
1743 if (col
->IsHidden())
1744 continue; // skip it!
1746 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1748 // get the cell value and set it into the renderer
1750 wxDataViewTreeNode
*node
= NULL
;
1751 wxDataViewItem dataitem
;
1753 if (!IsVirtualList())
1755 node
= GetTreeNodeByRow(item
);
1759 dataitem
= node
->GetItem();
1761 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1766 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1769 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1770 cell
->SetValue( value
);
1772 if (cell
->GetWantsAttr())
1774 wxDataViewItemAttr attr
;
1775 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1777 cell
->SetAttr( attr
);
1778 cell
->SetHasAttr( ret
);
1782 cell_rect
.y
= GetLineStart( item
);
1783 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1785 //Draw the expander here.
1787 if ((!IsVirtualList()) && (col
== expander
))
1789 indent
= node
->GetIndentLevel();
1791 //Calculate the indent first
1792 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1794 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1795 // change the cell_rect.x to the appropriate pos
1796 int expander_x
= indent
+ EXPANDER_MARGIN
;
1797 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1798 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1799 dc
.SetPen( m_penExpander
);
1800 dc
.SetBrush( wxNullBrush
);
1801 if( node
->HasChildren() )
1803 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1805 if (m_underMouse
== node
)
1807 flag
|= wxCONTROL_CURRENT
;
1809 if( node
->IsOpen() )
1810 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1812 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1814 //force the expander column to left-center align
1815 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1817 if (node
&& !node
->HasChildren())
1819 // Yes, if the node does not have any child, it must be a leaf which
1820 // mean that it is a temporarily created by GetTreeNodeByRow
1824 // cannot be bigger than allocated space
1825 wxSize size
= cell
->GetSize();
1826 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1827 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1828 // size.y = wxMin( size.y, cell_rect.height );
1829 size
.y
= cell_rect
.height
;
1831 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1832 int align
= cell
->CalculateAlignment();
1834 // horizontal alignment:
1835 item_rect
.x
= cell_rect
.x
;
1836 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1837 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1838 else if (align
& wxALIGN_RIGHT
)
1839 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1840 //else: wxALIGN_LEFT is the default
1842 // vertical alignment:
1843 item_rect
.y
= cell_rect
.y
;
1844 if (align
& wxALIGN_CENTER_VERTICAL
)
1845 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1846 else if (align
& wxALIGN_BOTTOM
)
1847 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1848 //else: wxALIGN_TOP is the default
1851 item_rect
.x
+= PADDING_RIGHTLEFT
;
1852 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1854 //Here we add the tree indent
1855 item_rect
.x
+= indent
;
1858 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1859 state
|= wxDATAVIEW_CELL_SELECTED
;
1861 // TODO: it would be much more efficient to create a clipping
1862 // region for the entire column being rendered (in the OnPaint
1863 // of wxDataViewMainWindow) instead of a single clip region for
1864 // each cell. However it would mean that each renderer should
1865 // respect the given wxRect's top & bottom coords, eventually
1866 // violating only the left & right coords - however the user can
1867 // make its own renderer and thus we cannot be sure of that.
1868 dc
.SetClippingRegion( item_rect
);
1869 cell
->Render( item_rect
, &dc
, state
);
1870 dc
.DestroyClippingRegion();
1873 cell_rect
.x
+= cell_rect
.width
;
1877 void wxDataViewMainWindow::OnRenameTimer()
1879 // We have to call this here because changes may just have
1880 // been made and no screen update taken place.
1885 unsigned int cols
= GetOwner()->GetColumnCount();
1887 for (i
= 0; i
< cols
; i
++)
1889 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1891 continue; // skip it!
1893 if (c
== m_currentCol
)
1895 xpos
+= c
->GetWidth();
1897 wxRect
labelRect( xpos
,
1898 GetLineStart( m_currentRow
),
1899 m_currentCol
->GetWidth(),
1900 GetLineHeight( m_currentRow
) );
1902 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1903 &labelRect
.x
, &labelRect
.y
);
1905 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1906 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1910 //------------------------------------------------------------------
1911 // Helper class for do operation on the tree node
1912 //------------------------------------------------------------------
1917 virtual ~DoJob() { }
1919 //The return value control how the tree-walker tranverse the tree
1920 // 0: Job done, stop tranverse and return
1921 // 1: Ignore the current node's subtree and continue
1922 // 2: Job not done, continue
1923 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1924 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1925 virtual int operator() ( void * n
) = 0;
1928 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1933 switch( func( node
) )
1944 wxDataViewTreeNodes nodes
= node
->GetNodes();
1945 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1947 int len_nodes
= nodes
.GetCount();
1948 int len
= leaves
.GetCount();
1949 int i
= 0, nodes_i
= 0;
1951 for(; i
< len
; i
++ )
1953 void * n
= leaves
[i
];
1954 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1956 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1959 if( Walker( nd
, func
) )
1978 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1989 wxDataViewTreeNode
* node
;
1990 node
= FindNode(parent
);
1995 node
->SetHasChildren( true );
1997 if( g_model
->IsContainer( item
) )
1999 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2000 newnode
->SetItem(item
);
2001 newnode
->SetHasChildren( true );
2002 node
->AddNode( newnode
);
2005 node
->AddLeaf( item
.GetID() );
2007 node
->ChangeSubTreeCount(1);
2015 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2017 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2018 const wxDataViewItem
& item
)
2023 if( m_currentRow
> GetRowCount() )
2024 m_currentRow
= m_count
- 1;
2026 m_selection
.Empty();
2033 wxDataViewTreeNode
* node
= FindNode(parent
);
2035 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2036 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2039 node
->GetChildren().Remove( item
.GetID() );
2040 //Manuplate selection
2041 if( m_selection
.GetCount() > 1 )
2043 m_selection
.Empty();
2045 bool isContainer
= false;
2046 wxDataViewTreeNodes nds
= node
->GetNodes();
2047 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2049 if (nds
[i
]->GetItem() == item
)
2057 wxDataViewTreeNode
* n
= NULL
;
2058 wxDataViewTreeNodes nodes
= node
->GetNodes();
2059 int len
= nodes
.GetCount();
2060 for( int i
= 0; i
< len
; i
++)
2062 if( nodes
[i
]->GetItem() == item
)
2069 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2071 node
->GetNodes().Remove( n
);
2072 sub
-= n
->GetSubTreeCount();
2073 ::DestroyTreeHelper(n
);
2075 //Make the row number invalid and get a new valid one when user call GetRowCount
2077 node
->ChangeSubTreeCount(sub
);
2079 //Change the current row to the last row if the current exceed the max row number
2080 if( m_currentRow
> GetRowCount() )
2081 m_currentRow
= m_count
- 1;
2088 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2094 wxWindow
*parent
= GetParent();
2095 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2096 le
.SetEventObject(parent
);
2097 le
.SetModel(GetOwner()->GetModel());
2099 parent
->GetEventHandler()->ProcessEvent(le
);
2104 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2106 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2107 /*#define MAX_VIRTUAL_WIDTH 100000
2109 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2110 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2111 Refresh( true, &rect );
2119 wxWindow
*parent
= GetParent();
2120 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2121 le
.SetEventObject(parent
);
2122 le
.SetModel(GetOwner()->GetModel());
2125 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2126 parent
->GetEventHandler()->ProcessEvent(le
);
2131 bool wxDataViewMainWindow::Cleared()
2136 BuildTree( GetOwner()->GetModel() );
2143 void wxDataViewMainWindow::UpdateDisplay()
2148 void wxDataViewMainWindow::OnInternalIdle()
2150 wxWindow::OnInternalIdle();
2154 RecalculateDisplay();
2159 void wxDataViewMainWindow::RecalculateDisplay()
2161 wxDataViewModel
*model
= GetOwner()->GetModel();
2168 int width
= GetEndOfLastCol();
2169 int height
= GetLineStart( GetRowCount() );
2171 SetVirtualSize( width
, height
);
2172 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2177 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2179 wxWindow::ScrollWindow( dx
, dy
, rect
);
2181 if (GetOwner()->m_headerArea
)
2182 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2185 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2188 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2189 int sy
= GetLineStart( rows
)/y
;
2193 wxRect rect
= GetClientRect();
2197 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2198 for (x_start
= 0; colnum
< column
; colnum
++)
2200 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2201 if (col
->IsHidden())
2202 continue; // skip it!
2204 w
= col
->GetWidth();
2208 int x_end
= x_start
+ w
;
2209 xe
= xx
+ rect
.width
;
2212 sx
= ( xx
+ x_end
- xe
)/x
;
2219 m_owner
->Scroll( sx
, sy
);
2222 int wxDataViewMainWindow::GetCountPerPage() const
2224 wxSize size
= GetClientSize();
2225 return size
.y
/ m_lineHeight
;
2228 int wxDataViewMainWindow::GetEndOfLastCol() const
2232 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2234 const wxDataViewColumn
*c
=
2235 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2238 width
+= c
->GetWidth();
2243 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2247 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2249 return GetLineAt( y
);
2252 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2254 wxSize client_size
= GetClientSize();
2255 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2256 &client_size
.x
, &client_size
.y
);
2258 //we should deal with the pixel here
2259 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2261 return wxMin( GetRowCount()-1, row
);
2264 unsigned int wxDataViewMainWindow::GetRowCount()
2266 if ( m_count
== -1 )
2268 m_count
= RecalculateCount();
2274 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2281 void wxDataViewMainWindow::SelectAllRows( bool on
)
2288 m_selection
.Clear();
2289 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2290 m_selection
.Add( i
);
2295 unsigned int first_visible
= GetFirstVisibleRow();
2296 unsigned int last_visible
= GetLastVisibleRow();
2298 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2300 unsigned int row
= m_selection
[i
];
2301 if ((row
>= first_visible
) && (row
<= last_visible
))
2304 m_selection
.Clear();
2308 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2310 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2314 m_selection
.Add( row
);
2322 m_selection
.Remove( row
);
2328 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2332 unsigned int tmp
= from
;
2338 for (i
= from
; i
<= to
; i
++)
2340 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2343 m_selection
.Add( i
);
2348 m_selection
.Remove( i
);
2351 RefreshRows( from
, to
);
2354 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2356 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2358 int n
= aSelections
[i
];
2360 m_selection
.Add( n
);
2365 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2367 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2368 m_selection
.Add( row
);
2370 m_selection
.Remove( row
);
2374 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2376 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2379 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2381 wxWindow
*parent
= GetParent();
2382 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2384 le
.SetEventObject(parent
);
2385 le
.SetModel(GetOwner()->GetModel());
2388 parent
->GetEventHandler()->ProcessEvent(le
);
2391 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2393 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2394 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2396 wxSize client_size
= GetClientSize();
2397 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2398 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2399 if (intersect_rect
.width
> 0)
2400 Refresh( true, &intersect_rect
);
2403 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2407 unsigned int tmp
= to
;
2412 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2413 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2415 wxSize client_size
= GetClientSize();
2416 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2417 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2418 if (intersect_rect
.width
> 0)
2419 Refresh( true, &intersect_rect
);
2422 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2424 wxSize client_size
= GetClientSize();
2425 int start
= GetLineStart( firstRow
);
2426 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2427 if (start
> client_size
.y
) return;
2429 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2431 Refresh( true, &rect
);
2434 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2436 wxCHECK_RET( newCurrent
< GetRowCount(),
2437 _T("invalid item index in OnArrowChar()") );
2439 // if there is no selection, we cannot move it anywhere
2440 if (!HasCurrentRow())
2443 unsigned int oldCurrent
= m_currentRow
;
2445 // in single selection we just ignore Shift as we can't select several
2447 if ( event
.ShiftDown() && !IsSingleSel() )
2449 RefreshRow( oldCurrent
);
2451 ChangeCurrentRow( newCurrent
);
2453 // select all the items between the old and the new one
2454 if ( oldCurrent
> newCurrent
)
2456 newCurrent
= oldCurrent
;
2457 oldCurrent
= m_currentRow
;
2460 SelectRows( oldCurrent
, newCurrent
, true );
2461 if (oldCurrent
!=newCurrent
)
2462 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2466 RefreshRow( oldCurrent
);
2468 // all previously selected items are unselected unless ctrl is held
2469 if ( !event
.ControlDown() )
2470 SelectAllRows(false);
2472 ChangeCurrentRow( newCurrent
);
2474 if ( !event
.ControlDown() )
2476 SelectRow( m_currentRow
, true );
2477 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2480 RefreshRow( m_currentRow
);
2483 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2486 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2490 rect
.y
= GetLineStart( row
);
2491 rect
.width
= GetEndOfLastCol();
2492 rect
.height
= GetLineHeight( row
);
2497 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2499 const wxDataViewModel
*model
= GetOwner()->GetModel();
2501 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2503 // TODO make more efficient
2508 for (r
= 0; r
< row
; r
++)
2510 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2511 if (!node
) return start
;
2513 wxDataViewItem item
= node
->GetItem();
2515 if (node
&& !node
->HasChildren())
2517 // Yes, if the node does not have any child, it must be a leaf which
2518 // mean that it is a temporarily created by GetTreeNodeByRow
2522 unsigned int cols
= GetOwner()->GetColumnCount();
2524 int height
= m_lineHeight
;
2525 for (col
= 0; col
< cols
; col
++)
2527 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2528 if (column
->IsHidden())
2529 continue; // skip it!
2531 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2532 continue; // skip it!
2534 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2536 model
->GetValue( value
, item
, column
->GetModelColumn() );
2537 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2538 renderer2
->SetValue( value
);
2539 height
= wxMax( height
, renderer
->GetSize().y
);
2550 return row
* m_lineHeight
;
2554 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2556 const wxDataViewModel
*model
= GetOwner()->GetModel();
2558 // check for the easy case first
2559 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2560 return y
/ m_lineHeight
;
2562 // TODO make more efficient
2563 unsigned int row
= 0;
2564 unsigned int yy
= 0;
2567 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2570 // not really correct...
2571 return row
+ ((y
-yy
) / m_lineHeight
);
2574 wxDataViewItem item
= node
->GetItem();
2576 if (node
&& !node
->HasChildren())
2578 // Yes, if the node does not have any child, it must be a leaf which
2579 // mean that it is a temporarily created by GetTreeNodeByRow
2583 unsigned int cols
= GetOwner()->GetColumnCount();
2585 int height
= m_lineHeight
;
2586 for (col
= 0; col
< cols
; col
++)
2588 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2589 if (column
->IsHidden())
2590 continue; // skip it!
2592 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2593 continue; // skip it!
2595 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2597 model
->GetValue( value
, item
, column
->GetModelColumn() );
2598 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2599 renderer2
->SetValue( value
);
2600 height
= wxMax( height
, renderer
->GetSize().y
);
2611 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2613 const wxDataViewModel
*model
= GetOwner()->GetModel();
2615 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2617 wxASSERT( !IsVirtualList() );
2619 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2620 // wxASSERT( node );
2621 if (!node
) return m_lineHeight
;
2623 wxDataViewItem item
= node
->GetItem();
2625 if (node
&& !node
->HasChildren())
2627 // Yes, if the node does not have any child, it must be a leaf which
2628 // mean that it is a temporarily created by GetTreeNodeByRow
2632 int height
= m_lineHeight
;
2634 unsigned int cols
= GetOwner()->GetColumnCount();
2636 for (col
= 0; col
< cols
; col
++)
2638 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2639 if (column
->IsHidden())
2640 continue; // skip it!
2642 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2643 continue; // skip it!
2645 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2647 model
->GetValue( value
, item
, column
->GetModelColumn() );
2648 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2649 renderer2
->SetValue( value
);
2650 height
= wxMax( height
, renderer
->GetSize().y
);
2657 return m_lineHeight
;
2661 class RowToItemJob
: public DoJob
2664 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2665 virtual ~RowToItemJob() { }
2667 virtual int operator() ( wxDataViewTreeNode
* node
)
2670 if( current
== static_cast<int>(row
))
2672 ret
= node
->GetItem();
2676 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2678 current
+= node
->GetSubTreeCount();
2683 //If the current has no child node, we can find the desired item of the row number directly.
2684 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2685 if( node
->GetNodes().GetCount() == 0)
2687 int index
= static_cast<int>(row
) - current
- 1;
2688 ret
= node
->GetChildren().Item( index
);
2695 virtual int operator() ( void * n
)
2698 if( current
== static_cast<int>(row
))
2700 ret
= wxDataViewItem( n
);
2705 wxDataViewItem
GetResult(){ return ret
; }
2712 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2716 return wxDataViewItem( wxUIntToPtr(row
) );
2720 RowToItemJob
job( row
, -2 );
2721 Walker( m_root
, job
);
2722 return job
.GetResult();
2726 class RowToTreeNodeJob
: public DoJob
2729 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2732 this->current
= current
;
2736 virtual ~RowToTreeNodeJob(){ }
2738 virtual int operator() ( wxDataViewTreeNode
* node
)
2741 if( current
== static_cast<int>(row
))
2747 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2749 current
+= node
->GetSubTreeCount();
2755 //If the current has no child node, we can find the desired item of the row number directly.
2756 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2757 if( node
->GetNodes().GetCount() == 0)
2759 int index
= static_cast<int>(row
) - current
- 1;
2760 void * n
= node
->GetChildren().Item( index
);
2761 ret
= new wxDataViewTreeNode( parent
);
2762 ret
->SetItem( wxDataViewItem( n
));
2763 ret
->SetHasChildren(false);
2772 virtual int operator() ( void * n
)
2775 if( current
== static_cast<int>(row
))
2777 ret
= new wxDataViewTreeNode( parent
);
2778 ret
->SetItem( wxDataViewItem( n
));
2779 ret
->SetHasChildren(false);
2785 wxDataViewTreeNode
* GetResult(){ return ret
; }
2789 wxDataViewTreeNode
* ret
;
2790 wxDataViewTreeNode
* parent
;
2794 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2796 wxASSERT( !IsVirtualList() );
2798 RowToTreeNodeJob
job( row
, -2, m_root
);
2799 Walker( m_root
, job
);
2800 return job
.GetResult();
2803 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2805 wxWindow
*parent
= GetParent();
2806 wxDataViewEvent
le(type
, parent
->GetId());
2808 le
.SetEventObject(parent
);
2809 le
.SetModel(GetOwner()->GetModel());
2812 parent
->GetEventHandler()->ProcessEvent(le
);
2817 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2819 if (IsVirtualList())
2822 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2826 if (!node
->HasChildren())
2832 return node
->IsOpen();
2836 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2838 if (IsVirtualList())
2841 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2844 if( node
->HasChildren())
2846 if( !node
->IsOpen())
2848 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2849 //Check if the user prevent expanding
2850 if( e
.GetSkipped() )
2854 //Here I build the children of current node
2855 if( node
->GetChildrenNumber() == 0 )
2858 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2862 //Send the expanded event
2863 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2867 SelectRow( row
, false );
2868 SelectRow( row
+ 1, true );
2869 ChangeCurrentRow( row
+ 1 );
2870 SendSelectionChangedEvent( GetItemByRow(row
+1));
2878 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2880 if (IsVirtualList())
2883 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2886 wxDataViewTreeNode
* nd
= node
;
2888 if( node
->HasChildren() && node
->IsOpen() )
2890 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2891 if( e
.GetSkipped() )
2896 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2900 node
= node
->GetParent();
2903 int parent
= GetRowByItem( node
->GetItem() );
2906 SelectRow( row
, false);
2907 SelectRow(parent
, true );
2908 ChangeCurrentRow( parent
);
2909 SendSelectionChangedEvent( node
->GetItem() );
2913 if( !nd
->HasChildren())
2918 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2920 wxDataViewModel
* model
= GetOwner()->GetModel();
2927 //Compose the a parent-chain of the finding item
2929 list
.DeleteContents( true );
2930 wxDataViewItem
it( item
);
2933 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2934 list
.Insert( pItem
);
2935 it
= model
->GetParent( it
);
2938 //Find the item along the parent-chain.
2939 //This algorithm is designed to speed up the node-finding method
2940 wxDataViewTreeNode
* node
= m_root
;
2941 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
2943 if( node
->HasChildren() )
2945 if( node
->GetChildrenNumber() == 0 )
2948 ::BuildTreeHelper(model
, node
->GetItem(), node
);
2951 wxDataViewTreeNodes nodes
= node
->GetNodes();
2955 for (i
= 0; i
< nodes
.GetCount(); i
++)
2957 if (nodes
[i
]->GetItem() == (**iter
))
2959 if (nodes
[i
]->GetItem() == item
)
2976 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
2978 wxDataViewColumn
*col
= NULL
;
2979 unsigned int cols
= GetOwner()->GetColumnCount();
2980 unsigned int colnum
= 0;
2982 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
2983 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
2985 col
= GetOwner()->GetColumnAt(colnum
);
2986 if (col
->IsHidden())
2987 continue; // skip it!
2989 unsigned int w
= col
->GetWidth();
2990 if (x_start
+w
>= (unsigned int)x
)
2997 item
= GetItemByRow( GetLineAt( y
) );
3000 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3002 int row
= GetRowByItem(item
);
3003 int y
= GetLineStart( row
);
3004 int h
= GetLineHeight( m_lineHeight
);
3006 wxDataViewColumn
*col
= NULL
;
3007 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3009 col
= GetOwner()->GetColumnAt( i
);
3010 x
+= col
->GetWidth();
3011 if( GetOwner()->GetColumnAt(i
+1) == column
)
3014 int w
= col
->GetWidth();
3015 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3016 return wxRect(x
, y
, w
, h
);
3019 int wxDataViewMainWindow::RecalculateCount()
3023 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3025 return list_model
->GetLastIndex() + 1;
3027 return list_model
->GetLastIndex() - 1;
3032 return m_root
->GetSubTreeCount();
3036 class ItemToRowJob
: public DoJob
3039 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3046 //Maybe binary search will help to speed up this process
3047 virtual int operator() ( wxDataViewTreeNode
* node
)
3050 if( node
->GetItem() == item
)
3055 if( node
->GetItem() == **m_iter
)
3062 ret
+= node
->GetSubTreeCount();
3068 virtual int operator() ( void * n
)
3071 if( n
== item
.GetID() )
3075 //the row number is begin from zero
3076 int GetResult() { return ret
-1; }
3079 ItemList::const_iterator m_iter
;
3080 wxDataViewItem item
;
3085 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3087 const wxDataViewModel
* model
= GetOwner()->GetModel();
3093 return wxPtrToUInt( item
.GetID() );
3100 //Compose the a parent-chain of the finding item
3102 wxDataViewItem
* pItem
;
3103 list
.DeleteContents( true );
3104 wxDataViewItem
it( item
);
3107 pItem
= new wxDataViewItem( it
);
3108 list
.Insert( pItem
);
3109 it
= model
->GetParent( it
);
3111 pItem
= new wxDataViewItem( );
3112 list
.Insert( pItem
);
3114 ItemToRowJob
job( item
, list
.begin() );
3115 Walker(m_root
, job
);
3116 return job
.GetResult();
3120 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3122 if( !model
->IsContainer( item
) )
3125 wxDataViewItemArray children
;
3126 unsigned int num
= model
->GetChildren( item
, children
);
3128 unsigned int index
= 0;
3129 while( index
< num
)
3131 if( model
->IsContainer( children
[index
] ) )
3133 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3134 n
->SetItem(children
[index
]);
3135 n
->SetHasChildren( true );
3140 node
->AddLeaf( children
[index
].GetID() );
3144 node
->SetSubTreeCount( num
);
3145 wxDataViewTreeNode
* n
= node
->GetParent();
3147 n
->ChangeSubTreeCount(num
);
3151 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3155 if (GetOwner()->GetModel()->IsVirtualListModel())
3161 m_root
= new wxDataViewTreeNode( NULL
);
3162 m_root
->SetHasChildren(true);
3164 //First we define a invalid item to fetch the top-level elements
3165 wxDataViewItem item
;
3167 BuildTreeHelper( model
, item
, m_root
);
3171 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3173 if( node
->GetNodeNumber() != 0 )
3175 int len
= node
->GetNodeNumber();
3177 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3178 for(; i
< len
; i
++ )
3180 DestroyTreeHelper(nodes
[i
]);
3186 void wxDataViewMainWindow::DestroyTree()
3188 if (!IsVirtualList())
3190 ::DestroyTreeHelper(m_root
);
3196 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3198 if ( GetParent()->HandleAsNavigationKey(event
) )
3201 // no item -> nothing to do
3202 if (!HasCurrentRow())
3208 // don't use m_linesPerPage directly as it might not be computed yet
3209 const int pageSize
= GetCountPerPage();
3210 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3212 switch ( event
.GetKeyCode() )
3216 if (m_currentRow
>= 0)
3218 wxWindow
*parent
= GetParent();
3219 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3220 le
.SetItem( GetItemByRow(m_currentRow
) );
3221 le
.SetEventObject(parent
);
3222 le
.SetModel(GetOwner()->GetModel());
3224 parent
->GetEventHandler()->ProcessEvent(le
);
3229 if ( m_currentRow
> 0 )
3230 OnArrowChar( m_currentRow
- 1, event
);
3234 if ( m_currentRow
< GetRowCount() - 1 )
3235 OnArrowChar( m_currentRow
+ 1, event
);
3237 //Add the process for tree expanding/collapsing
3239 OnCollapsing(m_currentRow
);
3242 OnExpanding( m_currentRow
);
3246 OnArrowChar( GetRowCount() - 1, event
);
3251 OnArrowChar( 0, event
);
3256 int steps
= pageSize
- 1;
3257 int index
= m_currentRow
- steps
;
3261 OnArrowChar( index
, event
);
3267 int steps
= pageSize
- 1;
3268 unsigned int index
= m_currentRow
+ steps
;
3269 unsigned int count
= GetRowCount();
3270 if ( index
>= count
)
3273 OnArrowChar( index
, event
);
3282 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3284 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3286 // let the base handle mouse wheel events.
3291 int x
= event
.GetX();
3292 int y
= event
.GetY();
3293 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3294 wxDataViewColumn
*col
= NULL
;
3297 unsigned int cols
= GetOwner()->GetColumnCount();
3299 for (i
= 0; i
< cols
; i
++)
3301 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3303 continue; // skip it!
3305 if (x
< xpos
+ c
->GetWidth())
3310 xpos
+= c
->GetWidth();
3315 wxDataViewRenderer
*cell
= col
->GetRenderer();
3316 unsigned int current
= GetLineAt( y
);
3317 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3319 // Unselect all if below the last row ?
3323 //Test whether the mouse is hovered on the tree item button
3325 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3327 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3328 if( node
!=NULL
&& node
->HasChildren() )
3330 int indent
= node
->GetIndentLevel();
3331 indent
= GetOwner()->GetIndent()*indent
;
3332 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3333 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3334 m_lineHeight
-2*EXPANDER_MARGIN
,
3335 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3336 if( rect
.Contains( x
, y
) )
3338 //So the mouse is over the expander
3340 if (m_underMouse
&& m_underMouse
!= node
)
3342 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3343 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3345 if (m_underMouse
!= node
)
3347 //wxLogMessage("Do the row: %d", current);
3348 RefreshRow(current
);
3350 m_underMouse
= node
;
3353 if (node
!=NULL
&& !node
->HasChildren())
3358 if (m_underMouse
!= NULL
)
3360 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3361 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3362 m_underMouse
= NULL
;
3366 wxDataViewModel
*model
= GetOwner()->GetModel();
3368 if (event
.Dragging())
3370 if (m_dragCount
== 0)
3372 // we have to report the raw, physical coords as we want to be
3373 // able to call HitTest(event.m_pointDrag) from the user code to
3374 // get the item being dragged
3375 m_dragStart
= event
.GetPosition();
3380 if (m_dragCount
!= 3)
3383 if (event
.LeftIsDown())
3385 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
, &m_dragStart
.x
, &m_dragStart
.y
);
3386 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3387 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3389 // Notify cell about drag
3390 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3391 event
.SetEventObject( m_owner
);
3392 event
.SetItem( item
);
3393 event
.SetModel( model
);
3394 if (!m_owner
->HandleWindowEvent( event
))
3397 if (!event
.IsAllowed())
3400 wxDataObject
*obj
= event
.GetDataObject();
3404 wxDataViewDropSource
drag( this, drag_item_row
);
3405 drag
.SetData( *obj
);
3406 /* wxDragResult res = */ drag
.DoDragDrop();
3416 bool forceClick
= false;
3418 if (event
.ButtonDClick())
3420 m_renameTimer
->Stop();
3421 m_lastOnSame
= false;
3424 wxDataViewItem item
= GetItemByRow(current
);
3425 bool ignore_other_columns
=
3426 ((GetOwner()->GetExpanderColumn() != col
) &&
3427 (model
->IsContainer(item
)) &&
3428 (!model
->HasContainerColumns(item
)));
3430 if (event
.LeftDClick())
3432 if ( current
== m_lineLastClicked
)
3434 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3437 model
->GetValue( value
, item
, col
->GetModelColumn() );
3438 cell
->SetValue( value
);
3439 wxRect
cell_rect( xpos
, GetLineStart( current
),
3440 col
->GetWidth(), GetLineHeight( current
) );
3441 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3446 wxWindow
*parent
= GetParent();
3447 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3449 le
.SetEventObject(parent
);
3450 le
.SetModel(GetOwner()->GetModel());
3452 parent
->GetEventHandler()->ProcessEvent(le
);
3458 // The first click was on another item, so don't interpret this as
3459 // a double click, but as a simple click instead
3466 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3468 // select single line
3469 SelectAllRows( false );
3470 SelectRow( m_lineSelectSingleOnUp
, true );
3471 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3474 //Process the event of user clicking the expander
3475 bool expander
= false;
3476 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3478 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3479 if( node
!=NULL
&& node
->HasChildren() )
3481 int indent
= node
->GetIndentLevel();
3482 indent
= GetOwner()->GetIndent()*indent
;
3483 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
,
3484 GetLineStart( current
) + EXPANDER_MARGIN
+ (GetLineHeight(current
)/2) - (m_lineHeight
/2) - EXPANDER_OFFSET
,
3485 m_lineHeight
-2*EXPANDER_MARGIN
,
3486 m_lineHeight
-2*EXPANDER_MARGIN
+ EXPANDER_OFFSET
);
3488 if( rect
.Contains( x
, y
) )
3491 if( node
->IsOpen() )
3492 OnCollapsing(current
);
3494 OnExpanding( current
);
3497 if (node
&& !node
->HasChildren())
3500 //If the user click the expander, we do not do editing even if the column with expander are editable
3501 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3503 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3504 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3506 m_renameTimer
->Start( 100, true );
3510 m_lastOnSame
= false;
3511 m_lineSelectSingleOnUp
= (unsigned int)-1;
3515 // This is necessary, because after a DnD operation in
3516 // from and to ourself, the up event is swallowed by the
3517 // DnD code. So on next non-up event (which means here and
3518 // now) m_lineSelectSingleOnUp should be reset.
3519 m_lineSelectSingleOnUp
= (unsigned int)-1;
3522 if (event
.RightDown())
3524 m_lineBeforeLastClicked
= m_lineLastClicked
;
3525 m_lineLastClicked
= current
;
3527 // If the item is already selected, do not update the selection.
3528 // Multi-selections should not be cleared if a selected item is clicked.
3529 if (!IsRowSelected(current
))
3531 SelectAllRows(false);
3532 ChangeCurrentRow(current
);
3533 SelectRow(m_currentRow
,true);
3534 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3538 model
->GetValue( value
, item
, col
->GetModelColumn() );
3539 wxWindow
*parent
= GetParent();
3540 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3542 le
.SetEventObject(parent
);
3543 le
.SetModel(GetOwner()->GetModel());
3545 parent
->GetEventHandler()->ProcessEvent(le
);
3547 else if (event
.MiddleDown())
3550 if (event
.LeftDown() || forceClick
)
3554 m_lineBeforeLastClicked
= m_lineLastClicked
;
3555 m_lineLastClicked
= current
;
3557 unsigned int oldCurrentRow
= m_currentRow
;
3558 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3560 bool cmdModifierDown
= event
.CmdDown();
3561 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3563 if ( IsSingleSel() || !IsRowSelected(current
) )
3565 SelectAllRows( false );
3566 ChangeCurrentRow(current
);
3567 SelectRow(m_currentRow
,true);
3568 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3570 else // multi sel & current is highlighted & no mod keys
3572 m_lineSelectSingleOnUp
= current
;
3573 ChangeCurrentRow(current
); // change focus
3576 else // multi sel & either ctrl or shift is down
3578 if (cmdModifierDown
)
3580 ChangeCurrentRow(current
);
3581 ReverseRowSelection(m_currentRow
);
3582 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3584 else if (event
.ShiftDown())
3586 ChangeCurrentRow(current
);
3588 unsigned int lineFrom
= oldCurrentRow
,
3591 if ( lineTo
< lineFrom
)
3594 lineFrom
= m_currentRow
;
3597 SelectRows(lineFrom
, lineTo
, true);
3598 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3600 else // !ctrl, !shift
3602 // test in the enclosing if should make it impossible
3603 wxFAIL_MSG( _T("how did we get here?") );
3607 if (m_currentRow
!= oldCurrentRow
)
3608 RefreshRow( oldCurrentRow
);
3610 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3612 // Update selection here...
3615 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3616 (current
== oldCurrentRow
)) && oldWasSelected
;
3618 // Call LeftClick after everything else as under GTK+
3619 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3621 // notify cell about right click
3623 model
->GetValue( value
, item
, col
->GetModelColumn() );
3624 cell
->SetValue( value
);
3625 wxRect
cell_rect( xpos
, GetLineStart( current
),
3626 col
->GetWidth(), GetLineHeight( current
) );
3627 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3632 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3636 if (HasCurrentRow())
3642 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3646 if (HasCurrentRow())
3652 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3654 if( m_selection
.GetCount() != 1 )
3655 return wxDataViewItem();
3657 return GetItemByRow( m_selection
.Item(0));
3660 //-----------------------------------------------------------------------------
3662 //-----------------------------------------------------------------------------
3663 WX_DEFINE_LIST(wxDataViewColumnList
)
3665 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3667 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3668 EVT_SIZE(wxDataViewCtrl::OnSize
)
3671 wxDataViewCtrl::~wxDataViewCtrl()
3674 GetModel()->RemoveNotifier( m_notifier
);
3679 void wxDataViewCtrl::Init()
3681 m_cols
.DeleteContents(true);
3684 // No sorting column at start
3685 m_sortingColumnIdx
= wxNOT_FOUND
;
3687 m_headerArea
= NULL
;
3690 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3691 const wxPoint
& pos
, const wxSize
& size
,
3692 long style
, const wxValidator
& validator
)
3694 // if ( (style & wxBORDER_MASK) == 0)
3695 // style |= wxBORDER_SUNKEN;
3699 if (!wxControl::Create( parent
, id
, pos
, size
,
3700 style
| wxScrolledWindowStyle
, validator
))
3703 SetInitialSize(size
);
3706 MacSetClipChildren( true );
3709 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3711 if (HasFlag(wxDV_NO_HEADER
))
3712 m_headerArea
= NULL
;
3714 m_headerArea
= new wxDataViewHeaderWindow(this);
3716 SetTargetWindow( m_clientArea
);
3718 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3720 sizer
->Add( m_headerArea
, 0, wxGROW
);
3721 sizer
->Add( m_clientArea
, 1, wxGROW
);
3727 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3729 return wxBORDER_THEME
;
3733 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3737 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3740 // we need to process arrows ourselves for scrolling
3741 if ( nMsg
== WM_GETDLGCODE
)
3743 rc
|= DLGC_WANTARROWS
;
3751 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3753 wxSize newsize
= size
;
3754 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3755 newsize
.y
-= m_headerArea
->GetSize().y
;
3760 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3762 // We need to override OnSize so that our scrolled
3763 // window a) does call Layout() to use sizers for
3764 // positioning the controls but b) does not query
3765 // the sizer for their size and use that for setting
3766 // the scrollable area as set that ourselves by
3767 // calling SetScrollbar() further down.
3774 void wxDataViewCtrl::SetFocus()
3777 m_clientArea
->SetFocus();
3780 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3782 if (!wxDataViewCtrlBase::AssociateModel( model
))
3785 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3787 model
->AddNotifier( m_notifier
);
3789 m_clientArea
->DestroyTree();
3791 m_clientArea
->BuildTree(model
);
3793 m_clientArea
->UpdateDisplay();
3798 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3800 return m_clientArea
->EnableDragSource( format
);
3803 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3805 return m_clientArea
->EnableDropTarget( format
);
3808 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3810 if (!wxDataViewCtrlBase::AppendColumn(col
))
3813 m_cols
.Append( col
);
3814 OnColumnsCountChanged();
3818 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3820 if (!wxDataViewCtrlBase::PrependColumn(col
))
3823 m_cols
.Insert( col
);
3824 OnColumnsCountChanged();
3828 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3830 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3833 m_cols
.Insert( pos
, col
);
3834 OnColumnsCountChanged();
3838 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3841 m_headerArea
->UpdateColumn(idx
);
3843 m_clientArea
->UpdateDisplay();
3846 void wxDataViewCtrl::OnColumnsCountChanged()
3849 m_headerArea
->SetColumnCount(GetColumnCount());
3851 m_clientArea
->UpdateDisplay();
3854 void wxDataViewCtrl::DoSetExpanderColumn()
3856 m_clientArea
->UpdateDisplay();
3859 void wxDataViewCtrl::DoSetIndent()
3861 m_clientArea
->UpdateDisplay();
3864 unsigned int wxDataViewCtrl::GetColumnCount() const
3866 return m_cols
.GetCount();
3869 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3874 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3876 // columns can't be reordered if there is no header window which allows
3878 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3881 return GetColumn(idx
);
3884 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3886 const unsigned count
= m_cols
.size();
3887 for ( unsigned n
= 0; n
< count
; n
++ )
3889 if ( m_cols
[n
] == column
)
3896 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
3897 unsigned int WXUNUSED(new_pos
))
3899 // do _not_ reorder m_cols elements here, they should always be in the
3900 // order in which columns were added, we only display the columns in
3902 m_clientArea
->UpdateDisplay();
3905 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3907 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3912 OnColumnsCountChanged();
3917 bool wxDataViewCtrl::ClearColumns()
3920 OnColumnsCountChanged();
3924 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3928 unsigned int len
= GetColumnCount();
3929 for ( unsigned int i
= 0; i
< len
; i
++ )
3931 wxDataViewColumn
* col
= GetColumnAt(i
);
3932 if (col
->IsHidden())
3934 ret
+= col
->GetWidth();
3937 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
3944 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3946 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
3947 : GetColumn(m_sortingColumnIdx
);
3950 //Selection code with wxDataViewItem as parameters
3951 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3953 return m_clientArea
->GetSelection();
3956 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3959 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3960 int len
= selection
.GetCount();
3961 for( int i
= 0; i
< len
; i
++)
3963 unsigned int row
= selection
[i
];
3964 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3969 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3971 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
3973 wxDataViewItem last_parent
;
3975 int len
= sel
.GetCount();
3976 for( int i
= 0; i
< len
; i
++ )
3978 wxDataViewItem item
= sel
[i
];
3979 wxDataViewItem parent
= GetModel()->GetParent( item
);
3982 if (parent
!= last_parent
)
3983 ExpandAncestors(item
);
3986 last_parent
= parent
;
3987 int row
= m_clientArea
->GetRowByItem( item
);
3989 selection
.Add( static_cast<unsigned int>(row
) );
3992 m_clientArea
->SetSelections( selection
);
3995 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3997 ExpandAncestors( item
);
3999 int row
= m_clientArea
->GetRowByItem( item
);
4002 //Unselect all rows before select another in the single select mode
4003 if (m_clientArea
->IsSingleSel())
4004 m_clientArea
->SelectAllRows(false);
4005 m_clientArea
->SelectRow(row
, true);
4009 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4011 int row
= m_clientArea
->GetRowByItem( item
);
4013 m_clientArea
->SelectRow(row
, false);
4016 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4018 int row
= m_clientArea
->GetRowByItem( item
);
4021 return m_clientArea
->IsRowSelected(row
);
4026 //Selection code with row number as parameter
4027 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4030 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4031 int len
= selection
.GetCount();
4032 for( int i
= 0; i
< len
; i
++)
4034 unsigned int row
= selection
[i
];
4040 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4042 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4043 int len
= sel
.GetCount();
4044 for( int i
= 0; i
< len
; i
++ )
4048 selection
.Add( static_cast<unsigned int>(row
) );
4050 m_clientArea
->SetSelections( selection
);
4053 void wxDataViewCtrl::Select( int row
)
4057 if (m_clientArea
->IsSingleSel())
4058 m_clientArea
->SelectAllRows(false);
4059 m_clientArea
->SelectRow( row
, true );
4063 void wxDataViewCtrl::Unselect( int row
)
4066 m_clientArea
->SelectRow(row
, false);
4069 bool wxDataViewCtrl::IsSelected( int row
) const
4072 return m_clientArea
->IsRowSelected(row
);
4076 void wxDataViewCtrl::SelectRange( int from
, int to
)
4079 for( int i
= from
; i
< to
; i
++ )
4081 m_clientArea
->Select(sel
);
4084 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4086 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4087 for( int i
= from
; i
< to
; i
++ )
4088 if( sel
.Index( i
) != wxNOT_FOUND
)
4090 m_clientArea
->SetSelections(sel
);
4093 void wxDataViewCtrl::SelectAll()
4095 m_clientArea
->SelectAllRows(true);
4098 void wxDataViewCtrl::UnselectAll()
4100 m_clientArea
->SelectAllRows(false);
4103 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4107 if( row
> (int) m_clientArea
->GetRowCount() )
4108 row
= m_clientArea
->GetRowCount();
4110 int first
= m_clientArea
->GetFirstVisibleRow();
4111 int last
= m_clientArea
->GetLastVisibleRow();
4113 m_clientArea
->ScrollTo( row
, column
);
4114 else if( row
> last
)
4115 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4117 m_clientArea
->ScrollTo( first
, column
);
4120 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4122 ExpandAncestors( item
);
4124 m_clientArea
->RecalculateDisplay();
4126 int row
= m_clientArea
->GetRowByItem(item
);
4129 if( column
== NULL
)
4130 EnsureVisible(row
, -1);
4132 EnsureVisible( row
, GetColumnIndex(column
) );
4137 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4139 m_clientArea
->HitTest(point
, item
, column
);
4142 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4144 return m_clientArea
->GetItemRect(item
, column
);
4147 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4149 return m_clientArea
->GetItemByRow( row
);
4152 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4154 return m_clientArea
->GetRowByItem( item
);
4157 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4159 int row
= m_clientArea
->GetRowByItem( item
);
4161 m_clientArea
->Expand(row
);
4164 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4166 int row
= m_clientArea
->GetRowByItem( item
);
4168 m_clientArea
->Collapse(row
);
4171 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4173 int row
= m_clientArea
->GetRowByItem( item
);
4175 return m_clientArea
->IsExpanded(row
);
4181 // !wxUSE_GENERICDATAVIEWCTRL
4184 // wxUSE_DATAVIEWCTRL