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 wxDECLARE_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 #if wxUSE_DRAG_AND_DROP
421 wxBitmap
CreateItemBitmap( unsigned int row
, int &indent
);
422 #endif // wxUSE_DRAG_AND_DROP
423 void OnPaint( wxPaintEvent
&event
);
424 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
425 void OnChar( wxKeyEvent
&event
);
426 void OnMouse( wxMouseEvent
&event
);
427 void OnSetFocus( wxFocusEvent
&event
);
428 void OnKillFocus( wxFocusEvent
&event
);
430 void UpdateDisplay();
431 void RecalculateDisplay();
432 void OnInternalIdle();
434 void OnRenameTimer();
436 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
437 void ScrollTo( int rows
, int column
);
439 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
440 void ChangeCurrentRow( unsigned int row
);
442 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
443 bool IsEmpty() { return GetRowCount() == 0; }
445 int GetCountPerPage() const;
446 int GetEndOfLastCol() const;
447 unsigned int GetFirstVisibleRow() const;
448 //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
449 unsigned int GetLastVisibleRow();
450 unsigned int GetRowCount();
452 wxDataViewItem
GetSelection() const;
453 wxDataViewSelection
GetSelections(){ return m_selection
; }
454 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
455 void Select( const wxArrayInt
& aSelections
);
456 void SelectAllRows( bool on
);
457 void SelectRow( unsigned int row
, bool on
);
458 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
459 void ReverseRowSelection( unsigned int row
);
460 bool IsRowSelected( unsigned int row
);
461 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
463 void RefreshRow( unsigned int row
);
464 void RefreshRows( unsigned int from
, unsigned int to
);
465 void RefreshRowsAfter( unsigned int firstRow
);
467 // returns the colour to be used for drawing the rules
468 wxColour
GetRuleColour() const
470 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
473 wxRect
GetLineRect( unsigned int row
) const;
475 int GetLineStart( unsigned int row
) const; // row * m_lineHeight in fixed mode
476 int GetLineHeight( unsigned int row
) const; // m_lineHeight in fixed mode
477 int GetLineAt( unsigned int y
) const; // y / m_lineHeight in fixed mode
479 //Some useful functions for row and item mapping
480 wxDataViewItem
GetItemByRow( unsigned int row
) const;
481 int GetRowByItem( const wxDataViewItem
& item
) const;
483 //Methods for building the mapping tree
484 void BuildTree( wxDataViewModel
* model
);
486 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
487 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
489 void Expand( unsigned int row
) { OnExpanding( row
); }
490 void Collapse( unsigned int row
) { OnCollapsing( row
); }
491 bool IsExpanded( unsigned int row
) const;
493 #if wxUSE_DRAG_AND_DROP
494 bool EnableDragSource( const wxDataFormat
&format
);
495 bool EnableDropTarget( const wxDataFormat
&format
);
497 void RemoveDropHint();
498 wxDragResult
OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
499 bool OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
);
500 wxDragResult
OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
);
502 #endif // wxUSE_DRAG_AND_DROP
505 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
) const;
506 //We did not need this temporarily
507 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
509 int RecalculateCount();
511 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
512 void OnExpanding( unsigned int row
);
513 void OnCollapsing( unsigned int row
);
515 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
518 wxDataViewCtrl
*m_owner
;
522 wxDataViewColumn
*m_currentCol
;
523 unsigned int m_currentRow
;
524 wxDataViewSelection m_selection
;
526 wxDataViewRenameTimer
*m_renameTimer
;
531 #if wxUSE_DRAG_AND_DROP
536 wxDataFormat m_dragFormat
;
539 wxDataFormat m_dropFormat
;
541 unsigned int m_dropHintLine
;
542 #endif // wxUSE_DRAG_AND_DROP
544 // for double click logic
545 unsigned int m_lineLastClicked
,
546 m_lineBeforeLastClicked
,
547 m_lineSelectSingleOnUp
;
549 // the pen used to draw horiz/vertical rules
552 // the pen used to draw the expander and the lines
555 //This is the tree structure of the model
556 wxDataViewTreeNode
* m_root
;
558 //This is the tree node under the cursor
559 wxDataViewTreeNode
* m_underMouse
;
561 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
562 DECLARE_EVENT_TABLE()
565 // ---------------------------------------------------------
566 // wxGenericDataViewModelNotifier
567 // ---------------------------------------------------------
569 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
572 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
573 { m_mainWindow
= mainWindow
; }
575 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
576 { return m_mainWindow
->ItemAdded( parent
, item
); }
577 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
578 { return m_mainWindow
->ItemDeleted( parent
, item
); }
579 virtual bool ItemChanged( const wxDataViewItem
& item
)
580 { return m_mainWindow
->ItemChanged(item
); }
581 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
582 { return m_mainWindow
->ValueChanged( item
, col
); }
583 virtual bool Cleared()
584 { return m_mainWindow
->Cleared(); }
585 virtual void Resort()
586 { m_mainWindow
->Resort(); }
588 wxDataViewMainWindow
*m_mainWindow
;
591 // ---------------------------------------------------------
592 // wxDataViewRenderer
593 // ---------------------------------------------------------
595 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
597 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
598 wxDataViewCellMode mode
,
600 wxDataViewRendererBase( varianttype
, mode
, align
)
609 wxDataViewRenderer::~wxDataViewRenderer()
615 wxDC
*wxDataViewRenderer::GetDC()
619 if (GetOwner() == NULL
)
621 if (GetOwner()->GetOwner() == NULL
)
623 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
629 void wxDataViewRenderer::SetAlignment( int align
)
634 int wxDataViewRenderer::GetAlignment() const
639 int wxDataViewRenderer::CalculateAlignment() const
641 if (m_align
== wxDVR_DEFAULT_ALIGNMENT
)
643 if (GetOwner() == NULL
)
644 return wxALIGN_LEFT
| wxALIGN_CENTRE_VERTICAL
;
646 return GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
652 // ---------------------------------------------------------
653 // wxDataViewCustomRenderer
654 // ---------------------------------------------------------
656 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
658 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
659 wxDataViewCellMode mode
, int align
) :
660 wxDataViewRenderer( varianttype
, mode
, align
)
664 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
666 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
667 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
668 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
669 view
->GetForegroundColour();
670 dc
->SetTextForeground(col
);
671 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
674 // ---------------------------------------------------------
675 // wxDataViewTextRenderer
676 // ---------------------------------------------------------
678 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
680 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
681 wxDataViewCellMode mode
, int align
) :
682 wxDataViewCustomRenderer( varianttype
, mode
, align
)
686 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
688 m_text
= value
.GetString();
693 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
698 bool wxDataViewTextRenderer::HasEditorCtrl()
703 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
704 wxRect labelRect
, const wxVariant
&value
)
706 return new wxTextCtrl( parent
, wxID_ANY
, value
,
707 wxPoint(labelRect
.x
,labelRect
.y
),
708 wxSize(labelRect
.width
,labelRect
.height
) );
711 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
713 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
714 value
= text
->GetValue();
718 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
720 RenderText( m_text
, 0, cell
, dc
, state
);
724 wxSize
wxDataViewTextRenderer::GetSize() const
726 const wxDataViewCtrl
*view
= GetView();
730 view
->GetTextExtent( m_text
, &x
, &y
);
731 return wxSize( x
, y
);
733 return wxSize(80,20);
736 // ---------------------------------------------------------
737 // wxDataViewTextRendererAttr
738 // ---------------------------------------------------------
740 IMPLEMENT_CLASS(wxDataViewTextRendererAttr
, wxDataViewTextRenderer
)
742 wxDataViewTextRendererAttr::wxDataViewTextRendererAttr( const wxString
&varianttype
,
743 wxDataViewCellMode mode
, int align
) :
744 wxDataViewTextRenderer( varianttype
, mode
, align
)
749 bool wxDataViewTextRendererAttr::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
756 if (m_attr
.HasColour())
758 colour
= dc
->GetTextForeground();
759 dc
->SetTextForeground( m_attr
.GetColour() );
762 if (m_attr
.GetBold() || m_attr
.GetItalic())
764 font
= dc
->GetFont();
765 wxFont myfont
= font
;
766 if (m_attr
.GetBold())
767 myfont
.SetWeight( wxFONTWEIGHT_BOLD
);
768 if (m_attr
.GetItalic())
769 myfont
.SetStyle( wxFONTSTYLE_ITALIC
);
770 dc
->SetFont( myfont
);
774 dc
->DrawText( m_text
, cell
.x
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
779 if (m_attr
.HasColour())
780 dc
->SetTextForeground( colour
);
782 if (m_attr
.GetBold() || m_attr
.GetItalic())
790 // ---------------------------------------------------------
791 // wxDataViewBitmapRenderer
792 // ---------------------------------------------------------
794 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
796 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
797 wxDataViewCellMode mode
, int align
) :
798 wxDataViewCustomRenderer( varianttype
, mode
, align
)
802 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
804 if (value
.GetType() == wxT("wxBitmap"))
806 if (value
.GetType() == wxT("wxIcon"))
812 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
817 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
820 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
821 else if (m_icon
.Ok())
822 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
827 wxSize
wxDataViewBitmapRenderer::GetSize() const
830 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
831 else if (m_icon
.Ok())
832 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
834 return wxSize(16,16);
837 // ---------------------------------------------------------
838 // wxDataViewToggleRenderer
839 // ---------------------------------------------------------
841 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
843 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
844 wxDataViewCellMode mode
, int align
) :
845 wxDataViewCustomRenderer( varianttype
, mode
, align
)
850 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
852 m_toggle
= value
.GetBool();
857 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
862 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
864 // User wxRenderer here
867 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
869 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
874 flags
|= wxCONTROL_CHECKED
;
875 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
876 flags
|= wxCONTROL_DISABLED
;
878 wxRendererNative::Get().DrawCheckBox(
879 GetOwner()->GetOwner(),
887 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
888 wxDataViewModel
*model
,
889 const wxDataViewItem
& item
, unsigned int col
)
891 bool value
= !m_toggle
;
892 wxVariant variant
= value
;
893 model
->SetValue( variant
, item
, col
);
894 model
->ValueChanged( item
, col
);
898 wxSize
wxDataViewToggleRenderer::GetSize() const
900 return wxSize(20,20);
903 // ---------------------------------------------------------
904 // wxDataViewProgressRenderer
905 // ---------------------------------------------------------
907 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
909 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
910 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
911 wxDataViewCustomRenderer( varianttype
, mode
, align
)
917 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
921 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
923 m_value
= (long) value
;
925 if (m_value
< 0) m_value
= 0;
926 if (m_value
> 100) m_value
= 100;
931 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
933 value
= (long) m_value
;
937 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
939 double pct
= (double)m_value
/ 100.0;
941 bar
.width
= (int)(cell
.width
* pct
);
942 dc
->SetPen( *wxTRANSPARENT_PEN
);
943 dc
->SetBrush( *wxBLUE_BRUSH
);
944 dc
->DrawRectangle( bar
);
946 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
947 dc
->SetPen( *wxBLACK_PEN
);
948 dc
->DrawRectangle( cell
);
953 wxSize
wxDataViewProgressRenderer::GetSize() const
955 return wxSize(40,12);
958 // ---------------------------------------------------------
959 // wxDataViewDateRenderer
960 // ---------------------------------------------------------
962 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
964 #if wxUSE_DATE_RENDERER_POPUP
966 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
969 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
970 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
971 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
976 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
977 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
978 sizer
->Add( m_cal
, 1, wxGROW
);
983 void OnCalendar( wxCalendarEvent
&event
);
985 wxCalendarCtrl
*m_cal
;
986 wxDataViewModel
*m_model
;
988 const wxDataViewItem
& m_item
;
991 virtual void OnDismiss()
996 DECLARE_EVENT_TABLE()
999 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
1000 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
1003 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
1005 wxDateTime date
= event
.GetDate();
1006 wxVariant value
= date
;
1007 m_model
->SetValue( value
, m_item
, m_col
);
1008 m_model
->ValueChanged( m_item
, m_col
);
1012 #endif // wxUSE_DATE_RENDERER_POPUP
1014 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1016 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1017 wxDataViewCellMode mode
, int align
) :
1018 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1022 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1024 m_date
= value
.GetDateTime();
1029 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1035 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1037 wxString tmp
= m_date
.FormatDate();
1038 RenderText( tmp
, 0, cell
, dc
, state
);
1042 wxSize
wxDataViewDateRenderer::GetSize() const
1044 const wxDataViewCtrl
* view
= GetView();
1045 wxString tmp
= m_date
.FormatDate();
1047 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1048 return wxSize(x
,y
+d
);
1051 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1052 const wxDataViewItem
& item
, unsigned int col
)
1055 model
->GetValue( variant
, item
, col
);
1056 wxDateTime value
= variant
.GetDateTime();
1058 #if wxUSE_DATE_RENDERER_POPUP
1059 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1060 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1061 wxPoint pos
= wxGetMousePosition();
1064 popup
->Popup( popup
->m_cal
);
1065 #else // !wxUSE_DATE_RENDERER_POPUP
1066 wxMessageBox(value
.Format());
1067 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1071 // ---------------------------------------------------------
1072 // wxDataViewIconTextRenderer
1073 // ---------------------------------------------------------
1075 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1077 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1078 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1079 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1082 SetAlignment(align
);
1085 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1089 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1095 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1100 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1103 const wxIcon
&icon
= m_value
.GetIcon();
1106 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1107 xoffset
= icon
.GetWidth()+4;
1110 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1115 wxSize
wxDataViewIconTextRenderer::GetSize() const
1117 const wxDataViewCtrl
*view
= GetView();
1118 if (!m_value
.GetText().empty())
1121 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1123 if (m_value
.GetIcon().IsOk())
1124 x
+= m_value
.GetIcon().GetWidth() + 4;
1125 return wxSize( x
, y
);
1127 return wxSize(80,20);
1131 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1132 wxRect
WXUNUSED(labelRect
),
1133 const wxVariant
& WXUNUSED(value
))
1139 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1140 wxVariant
& WXUNUSED(value
))
1145 //-----------------------------------------------------------------------------
1146 // wxDataViewDropTarget
1147 //-----------------------------------------------------------------------------
1149 #if wxUSE_DRAG_AND_DROP
1151 class wxBitmapCanvas
: public wxWindow
1154 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1155 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1158 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1161 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1164 dc
.DrawBitmap( m_bitmap
, 0, 0);
1170 class wxDataViewDropSource
: public wxDropSource
1173 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1181 ~wxDataViewDropSource()
1186 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1188 wxPoint pos
= wxGetMousePosition();
1192 int liney
= m_win
->GetLineStart( m_row
);
1194 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1195 m_win
->ClientToScreen( &linex
, &liney
);
1196 m_dist_x
= pos
.x
- linex
;
1197 m_dist_y
= pos
.y
- liney
;
1200 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1202 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1203 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1205 wxFRAME_TOOL_WINDOW
|
1206 wxFRAME_FLOAT_ON_PARENT
|
1207 wxFRAME_NO_TASKBAR
|
1209 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1214 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1215 m_hint
->SetTransparent( 128 );
1221 wxDataViewMainWindow
*m_win
;
1224 int m_dist_x
,m_dist_y
;
1228 class wxDataViewDropTarget
: public wxDropTarget
1231 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1237 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1239 wxDataFormat format
= GetMatchingPair();
1240 if (format
== wxDF_INVALID
)
1242 return m_win
->OnDragOver( format
, x
, y
, def
);
1245 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1247 wxDataFormat format
= GetMatchingPair();
1248 if (format
== wxDF_INVALID
)
1250 return m_win
->OnDrop( format
, x
, y
);
1253 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1255 wxDataFormat format
= GetMatchingPair();
1256 if (format
== wxDF_INVALID
)
1260 return m_win
->OnData( format
, x
, y
, def
);
1263 virtual void OnLeave()
1264 { m_win
->OnLeave(); }
1266 wxDataViewMainWindow
*m_win
;
1269 #endif // wxUSE_DRAG_AND_DROP
1271 //-----------------------------------------------------------------------------
1272 // wxDataViewRenameTimer
1273 //-----------------------------------------------------------------------------
1275 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1280 void wxDataViewRenameTimer::Notify()
1282 m_owner
->OnRenameTimer();
1285 //-----------------------------------------------------------------------------
1286 // wxDataViewMainWindow
1287 //-----------------------------------------------------------------------------
1289 //The tree building helper, declared firstly
1290 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1292 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1294 if (row1
> row2
) return 1;
1295 if (row1
== row2
) return 0;
1300 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1302 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1303 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1304 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1305 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1306 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1307 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1310 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1311 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1312 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1313 m_selection( wxDataViewSelectionCmp
)
1318 m_lastOnSame
= false;
1319 m_renameTimer
= new wxDataViewRenameTimer( this );
1321 // TODO: user better initial values/nothing selected
1322 m_currentCol
= NULL
;
1325 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1327 #if wxUSE_DRAG_AND_DROP
1329 m_dragStart
= wxPoint(0,0);
1331 m_dragEnabled
= false;
1332 m_dropEnabled
= false;
1334 m_dropHintLine
= (unsigned int) -1;
1335 #endif // wxUSE_DRAG_AND_DROP
1337 m_lineLastClicked
= (unsigned int) -1;
1338 m_lineBeforeLastClicked
= (unsigned int) -1;
1339 m_lineSelectSingleOnUp
= (unsigned int) -1;
1343 SetBackgroundColour( *wxWHITE
);
1345 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1347 m_penRule
= wxPen(GetRuleColour());
1349 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1350 m_penExpander
= wxPen(wxColour(0,0,0));
1351 //Some new added code to deal with the tree structure
1352 m_root
= new wxDataViewTreeNode( NULL
);
1353 m_root
->SetHasChildren(true);
1355 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1357 m_underMouse
= NULL
;
1361 wxDataViewMainWindow::~wxDataViewMainWindow()
1364 delete m_renameTimer
;
1368 #if wxUSE_DRAG_AND_DROP
1369 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1371 m_dragFormat
= format
;
1372 m_dragEnabled
= format
!= wxDF_INVALID
;
1377 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1379 m_dropFormat
= format
;
1380 m_dropEnabled
= format
!= wxDF_INVALID
;
1383 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1388 void wxDataViewMainWindow::RemoveDropHint()
1393 RefreshRow( m_dropHintLine
);
1394 m_dropHintLine
= (unsigned int) -1;
1398 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1402 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1403 unsigned int row
= GetLineAt( yy
);
1405 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1411 wxDataViewItem item
= GetItemByRow( row
);
1413 wxDataViewModel
*model
= GetOwner()->GetModel();
1415 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1416 event
.SetEventObject( m_owner
);
1417 event
.SetItem( item
);
1418 event
.SetModel( model
);
1419 event
.SetDataFormat( format
);
1420 if (!m_owner
->HandleWindowEvent( event
))
1426 if (!event
.IsAllowed())
1433 if (m_dropHint
&& (row
!= m_dropHintLine
))
1434 RefreshRow( m_dropHintLine
);
1436 m_dropHintLine
= row
;
1442 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1448 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1449 unsigned int row
= GetLineAt( yy
);
1451 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1454 wxDataViewItem item
= GetItemByRow( row
);
1456 wxDataViewModel
*model
= GetOwner()->GetModel();
1458 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1459 event
.SetEventObject( m_owner
);
1460 event
.SetItem( item
);
1461 event
.SetModel( model
);
1462 event
.SetDataFormat( format
);
1463 if (!m_owner
->HandleWindowEvent( event
))
1466 if (!event
.IsAllowed())
1472 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1476 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1477 unsigned int row
= GetLineAt( yy
);
1479 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1482 wxDataViewItem item
= GetItemByRow( row
);
1484 wxDataViewModel
*model
= GetOwner()->GetModel();
1486 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1488 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1489 event
.SetEventObject( m_owner
);
1490 event
.SetItem( item
);
1491 event
.SetModel( model
);
1492 event
.SetDataFormat( format
);
1493 event
.SetDataSize( obj
->GetSize() );
1494 event
.SetDataBuffer( obj
->GetData() );
1495 if (!m_owner
->HandleWindowEvent( event
))
1498 if (!event
.IsAllowed())
1504 void wxDataViewMainWindow::OnLeave()
1509 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1511 int height
= GetLineHeight( row
);
1513 unsigned int cols
= GetOwner()->GetColumnCount();
1515 for (col
= 0; col
< cols
; col
++)
1517 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1518 if (column
->IsHidden())
1519 continue; // skip it!
1520 width
+= column
->GetWidth();
1524 if (!IsVirtualList())
1526 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1527 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1528 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1530 if(!node
->HasChildren())
1535 wxBitmap
bitmap( width
, height
);
1536 wxMemoryDC
dc( bitmap
);
1537 dc
.SetFont( GetFont() );
1538 dc
.SetPen( *wxBLACK_PEN
);
1539 dc
.SetBrush( *wxWHITE_BRUSH
);
1540 dc
.DrawRectangle( 0,0,width
,height
);
1542 wxDataViewModel
*model
= m_owner
->GetModel();
1544 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1547 // TODO-RTL: last column for RTL support
1548 expander
= GetOwner()->GetColumnAt( 0 );
1549 GetOwner()->SetExpanderColumn(expander
);
1554 for (col
= 0; col
< cols
; col
++)
1556 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1557 wxDataViewRenderer
*cell
= column
->GetRenderer();
1559 if (column
->IsHidden())
1560 continue; // skip it!
1562 width
= column
->GetWidth();
1564 if (column
== expander
)
1568 wxDataViewItem item
= GetItemByRow( row
);
1569 model
->GetValue( value
, item
, column
->GetModelColumn());
1570 cell
->SetValue( value
);
1572 if (cell
->GetWantsAttr())
1574 wxDataViewItemAttr attr
;
1575 bool ret
= model
->GetAttr( item
, column
->GetModelColumn(), attr
);
1577 cell
->SetAttr( attr
);
1578 cell
->SetHasAttr( ret
);
1581 wxSize size
= cell
->GetSize();
1582 size
.x
= wxMin( 2*PADDING_RIGHTLEFT
+ size
.x
, width
);
1584 wxRect
item_rect(x
, 0, size
.x
, size
.y
);
1586 int align
= cell
->CalculateAlignment();
1587 // horizontal alignment:
1589 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1590 item_rect
.x
= x
+ (width
/ 2) - (size
.x
/ 2);
1591 else if (align
& wxALIGN_RIGHT
)
1592 item_rect
.x
= x
+ width
- size
.x
;
1593 //else: wxALIGN_LEFT is the default
1595 // vertical alignment:
1597 if (align
& wxALIGN_CENTER_VERTICAL
)
1598 item_rect
.y
= (height
/ 2) - (size
.y
/ 2);
1599 else if (align
& wxALIGN_BOTTOM
)
1600 item_rect
.y
= height
- size
.y
;
1601 //else: wxALIGN_TOP is the default
1604 item_rect
.x
+= PADDING_RIGHTLEFT
;
1605 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1607 //dc.SetClippingRegion( item_rect );
1608 cell
->Render( item_rect
, &dc
, 0 );
1609 //dc.DestroyClippingRegion();
1617 #endif // wxUSE_DRAG_AND_DROP
1620 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1622 wxDataViewModel
*model
= GetOwner()->GetModel();
1623 wxAutoBufferedPaintDC
dc( this );
1626 dc
.SetPen( *wxTRANSPARENT_PEN
);
1627 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1628 dc
.SetBrush( *wxWHITE_BRUSH
);
1629 wxSize
size( GetClientSize() );
1630 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1634 GetOwner()->PrepareDC( dc
);
1635 dc
.SetFont( GetFont() );
1637 wxRect update
= GetUpdateRegion().GetBox();
1638 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1640 // compute which items needs to be redrawn
1641 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1642 unsigned int item_count
=
1643 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1644 (int)(GetRowCount( ) - item_start
));
1645 unsigned int item_last
= item_start
+ item_count
;
1647 // compute which columns needs to be redrawn
1648 unsigned int cols
= GetOwner()->GetColumnCount();
1649 unsigned int col_start
= 0;
1650 unsigned int x_start
;
1651 for (x_start
= 0; col_start
< cols
; col_start
++)
1653 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1654 if (col
->IsHidden())
1655 continue; // skip it!
1657 unsigned int w
= col
->GetWidth();
1658 if (x_start
+w
>= (unsigned int)update
.x
)
1664 unsigned int col_last
= col_start
;
1665 unsigned int x_last
= x_start
;
1666 for (; col_last
< cols
; col_last
++)
1668 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1669 if (col
->IsHidden())
1670 continue; // skip it!
1672 if (x_last
> (unsigned int)update
.GetRight())
1675 x_last
+= col
->GetWidth();
1678 // Draw horizontal rules if required
1679 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1681 dc
.SetPen(m_penRule
);
1682 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1684 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
1686 int y
= GetLineStart( i
);
1687 dc
.DrawLine(x_start
, y
, x_last
, y
);
1691 // Draw vertical rules if required
1692 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1694 dc
.SetPen(m_penRule
);
1695 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1698 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1700 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1701 if (col
->IsHidden())
1702 continue; // skip it
1704 dc
.DrawLine(x
, GetLineStart( item_start
),
1705 x
, GetLineStart( item_last
) );
1707 x
+= col
->GetWidth();
1710 // Draw last vertical rule
1711 dc
.DrawLine(x
, GetLineStart( item_start
),
1712 x
, GetLineStart( item_last
) );
1715 // redraw the background for the items which are selected/current
1716 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1718 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1719 if (selected
|| item
== m_currentRow
)
1721 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1722 if (item
== m_currentRow
)
1723 flags
|= wxCONTROL_CURRENT
;
1725 flags
|= wxCONTROL_FOCUSED
;
1727 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1728 wxRendererNative::Get().DrawItemSelectionRect
1738 #if wxUSE_DRAG_AND_DROP
1741 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1742 x_last
, GetLineHeight( m_dropHintLine
) );
1743 dc
.SetPen( *wxBLACK_PEN
);
1744 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1745 dc
.DrawRectangle( rect
);
1747 #endif // wxUSE_DRAG_AND_DROP
1749 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1752 // TODO-RTL: last column for RTL support
1753 expander
= GetOwner()->GetColumnAt( 0 );
1754 GetOwner()->SetExpanderColumn(expander
);
1757 // redraw all cells for all rows which must be repainted and all columns
1759 cell_rect
.x
= x_start
;
1760 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1763 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1764 wxDataViewRenderer
*cell
= col
->GetRenderer();
1765 cell_rect
.width
= col
->GetWidth();
1767 if (col
->IsHidden())
1768 continue; // skip it!
1770 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1772 // get the cell value and set it into the renderer
1774 wxDataViewTreeNode
*node
= NULL
;
1775 wxDataViewItem dataitem
;
1777 if (!IsVirtualList())
1779 node
= GetTreeNodeByRow(item
);
1783 dataitem
= node
->GetItem();
1785 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1790 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1793 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1794 cell
->SetValue( value
);
1796 if (cell
->GetWantsAttr())
1798 wxDataViewItemAttr attr
;
1799 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1801 cell
->SetAttr( attr
);
1802 cell
->SetHasAttr( ret
);
1806 cell_rect
.y
= GetLineStart( item
);
1807 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1809 //Draw the expander here.
1811 if ((!IsVirtualList()) && (col
== expander
))
1813 indent
= node
->GetIndentLevel();
1815 //Calculate the indent first
1816 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1818 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1819 // change the cell_rect.x to the appropriate pos
1820 int expander_x
= indent
+ EXPANDER_MARGIN
;
1821 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1822 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1823 dc
.SetPen( m_penExpander
);
1824 dc
.SetBrush( wxNullBrush
);
1825 if( node
->HasChildren() )
1827 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1829 if (m_underMouse
== node
)
1831 flag
|= wxCONTROL_CURRENT
;
1833 if( node
->IsOpen() )
1834 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1836 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1838 //force the expander column to left-center align
1839 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1841 if (node
&& !node
->HasChildren())
1843 // Yes, if the node does not have any child, it must be a leaf which
1844 // mean that it is a temporarily created by GetTreeNodeByRow
1848 // cannot be bigger than allocated space
1849 wxSize size
= cell
->GetSize();
1850 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1851 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1852 // size.y = wxMin( size.y, cell_rect.height );
1853 size
.y
= cell_rect
.height
;
1855 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1856 int align
= cell
->CalculateAlignment();
1858 // horizontal alignment:
1859 item_rect
.x
= cell_rect
.x
;
1860 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1861 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1862 else if (align
& wxALIGN_RIGHT
)
1863 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1864 //else: wxALIGN_LEFT is the default
1866 // vertical alignment:
1867 item_rect
.y
= cell_rect
.y
;
1868 if (align
& wxALIGN_CENTER_VERTICAL
)
1869 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1870 else if (align
& wxALIGN_BOTTOM
)
1871 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1872 //else: wxALIGN_TOP is the default
1875 item_rect
.x
+= PADDING_RIGHTLEFT
;
1876 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1878 //Here we add the tree indent
1879 item_rect
.x
+= indent
;
1882 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1883 state
|= wxDATAVIEW_CELL_SELECTED
;
1885 // TODO: it would be much more efficient to create a clipping
1886 // region for the entire column being rendered (in the OnPaint
1887 // of wxDataViewMainWindow) instead of a single clip region for
1888 // each cell. However it would mean that each renderer should
1889 // respect the given wxRect's top & bottom coords, eventually
1890 // violating only the left & right coords - however the user can
1891 // make its own renderer and thus we cannot be sure of that.
1892 dc
.SetClippingRegion( item_rect
);
1893 cell
->Render( item_rect
, &dc
, state
);
1894 dc
.DestroyClippingRegion();
1897 cell_rect
.x
+= cell_rect
.width
;
1901 void wxDataViewMainWindow::OnRenameTimer()
1903 // We have to call this here because changes may just have
1904 // been made and no screen update taken place.
1907 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1908 // (needs to be tested!)
1913 unsigned int cols
= GetOwner()->GetColumnCount();
1915 for (i
= 0; i
< cols
; i
++)
1917 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1919 continue; // skip it!
1921 if (c
== m_currentCol
)
1923 xpos
+= c
->GetWidth();
1926 // we have to take an expander column into account and compute its indentation
1927 // to get the editor at the correct x position where the actual text is
1929 if (!IsVirtualList() && GetOwner()->GetExpanderColumn() == m_currentCol
)
1931 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
1932 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1933 indent
= indent
+ m_lineHeight
;
1935 if(!node
->HasChildren())
1939 wxRect
labelRect( xpos
+ indent
,
1940 GetLineStart( m_currentRow
),
1941 m_currentCol
->GetWidth() - indent
,
1942 GetLineHeight( m_currentRow
) );
1944 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1945 &labelRect
.x
, &labelRect
.y
);
1947 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1948 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1951 //------------------------------------------------------------------
1952 // Helper class for do operation on the tree node
1953 //------------------------------------------------------------------
1958 virtual ~DoJob() { }
1960 //The return value control how the tree-walker tranverse the tree
1961 // 0: Job done, stop tranverse and return
1962 // 1: Ignore the current node's subtree and continue
1963 // 2: Job not done, continue
1964 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1965 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1966 virtual int operator() ( void * n
) = 0;
1969 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1974 switch( func( node
) )
1985 wxDataViewTreeNodes nodes
= node
->GetNodes();
1986 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1988 int len_nodes
= nodes
.GetCount();
1989 int len
= leaves
.GetCount();
1990 int i
= 0, nodes_i
= 0;
1992 for(; i
< len
; i
++ )
1994 void * n
= leaves
[i
];
1995 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1997 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
2000 if( Walker( nd
, func
) )
2019 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2030 wxDataViewTreeNode
* node
;
2031 node
= FindNode(parent
);
2036 node
->SetHasChildren( true );
2038 if( g_model
->IsContainer( item
) )
2040 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2041 newnode
->SetItem(item
);
2042 newnode
->SetHasChildren( true );
2043 node
->AddNode( newnode
);
2046 node
->AddLeaf( item
.GetID() );
2048 node
->ChangeSubTreeCount(1);
2056 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2058 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2059 const wxDataViewItem
& item
)
2064 if( m_currentRow
> GetRowCount() )
2065 m_currentRow
= m_count
- 1;
2067 m_selection
.Empty();
2074 wxDataViewTreeNode
* node
= FindNode(parent
);
2076 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2077 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2080 node
->GetChildren().Remove( item
.GetID() );
2081 //Manuplate selection
2082 if( m_selection
.GetCount() > 1 )
2084 m_selection
.Empty();
2086 bool isContainer
= false;
2087 wxDataViewTreeNodes nds
= node
->GetNodes();
2088 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2090 if (nds
[i
]->GetItem() == item
)
2098 wxDataViewTreeNode
* n
= NULL
;
2099 wxDataViewTreeNodes nodes
= node
->GetNodes();
2100 int len
= nodes
.GetCount();
2101 for( int i
= 0; i
< len
; i
++)
2103 if( nodes
[i
]->GetItem() == item
)
2110 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2112 node
->GetNodes().Remove( n
);
2113 sub
-= n
->GetSubTreeCount();
2114 ::DestroyTreeHelper(n
);
2116 //Make the row number invalid and get a new valid one when user call GetRowCount
2118 node
->ChangeSubTreeCount(sub
);
2120 //Change the current row to the last row if the current exceed the max row number
2121 if( m_currentRow
> GetRowCount() )
2122 m_currentRow
= m_count
- 1;
2129 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2135 wxWindow
*parent
= GetParent();
2136 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2137 le
.SetEventObject(parent
);
2138 le
.SetModel(GetOwner()->GetModel());
2140 parent
->GetEventHandler()->ProcessEvent(le
);
2145 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2147 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2148 /*#define MAX_VIRTUAL_WIDTH 100000
2150 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2151 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2152 Refresh( true, &rect );
2160 wxWindow
*parent
= GetParent();
2161 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2162 le
.SetEventObject(parent
);
2163 le
.SetModel(GetOwner()->GetModel());
2166 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2167 parent
->GetEventHandler()->ProcessEvent(le
);
2172 bool wxDataViewMainWindow::Cleared()
2177 BuildTree( GetOwner()->GetModel() );
2184 void wxDataViewMainWindow::UpdateDisplay()
2189 void wxDataViewMainWindow::OnInternalIdle()
2191 wxWindow::OnInternalIdle();
2195 RecalculateDisplay();
2200 void wxDataViewMainWindow::RecalculateDisplay()
2202 wxDataViewModel
*model
= GetOwner()->GetModel();
2209 int width
= GetEndOfLastCol();
2210 int height
= GetLineStart( GetRowCount() );
2212 SetVirtualSize( width
, height
);
2213 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2218 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2220 wxWindow::ScrollWindow( dx
, dy
, rect
);
2222 if (GetOwner()->m_headerArea
)
2223 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2226 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2229 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2230 int sy
= GetLineStart( rows
)/y
;
2234 wxRect rect
= GetClientRect();
2238 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2239 for (x_start
= 0; colnum
< column
; colnum
++)
2241 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2242 if (col
->IsHidden())
2243 continue; // skip it!
2245 w
= col
->GetWidth();
2249 int x_end
= x_start
+ w
;
2250 xe
= xx
+ rect
.width
;
2253 sx
= ( xx
+ x_end
- xe
)/x
;
2260 m_owner
->Scroll( sx
, sy
);
2263 int wxDataViewMainWindow::GetCountPerPage() const
2265 wxSize size
= GetClientSize();
2266 return size
.y
/ m_lineHeight
;
2269 int wxDataViewMainWindow::GetEndOfLastCol() const
2273 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2275 const wxDataViewColumn
*c
=
2276 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2279 width
+= c
->GetWidth();
2284 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2288 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2290 return GetLineAt( y
);
2293 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2295 wxSize client_size
= GetClientSize();
2296 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2297 &client_size
.x
, &client_size
.y
);
2299 //we should deal with the pixel here
2300 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2302 return wxMin( GetRowCount()-1, row
);
2305 unsigned int wxDataViewMainWindow::GetRowCount()
2307 if ( m_count
== -1 )
2309 m_count
= RecalculateCount();
2315 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2322 void wxDataViewMainWindow::SelectAllRows( bool on
)
2329 m_selection
.Clear();
2330 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2331 m_selection
.Add( i
);
2336 unsigned int first_visible
= GetFirstVisibleRow();
2337 unsigned int last_visible
= GetLastVisibleRow();
2339 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2341 unsigned int row
= m_selection
[i
];
2342 if ((row
>= first_visible
) && (row
<= last_visible
))
2345 m_selection
.Clear();
2349 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2351 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2355 m_selection
.Add( row
);
2363 m_selection
.Remove( row
);
2369 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2373 unsigned int tmp
= from
;
2379 for (i
= from
; i
<= to
; i
++)
2381 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2384 m_selection
.Add( i
);
2389 m_selection
.Remove( i
);
2392 RefreshRows( from
, to
);
2395 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2397 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2399 int n
= aSelections
[i
];
2401 m_selection
.Add( n
);
2406 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2408 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2409 m_selection
.Add( row
);
2411 m_selection
.Remove( row
);
2415 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2417 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2420 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2422 wxWindow
*parent
= GetParent();
2423 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2425 le
.SetEventObject(parent
);
2426 le
.SetModel(GetOwner()->GetModel());
2429 parent
->GetEventHandler()->ProcessEvent(le
);
2432 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2434 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2435 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2437 wxSize client_size
= GetClientSize();
2438 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2439 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2440 if (intersect_rect
.width
> 0)
2441 Refresh( true, &intersect_rect
);
2444 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2448 unsigned int tmp
= to
;
2453 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2454 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2456 wxSize client_size
= GetClientSize();
2457 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2458 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2459 if (intersect_rect
.width
> 0)
2460 Refresh( true, &intersect_rect
);
2463 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2465 wxSize client_size
= GetClientSize();
2466 int start
= GetLineStart( firstRow
);
2467 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2468 if (start
> client_size
.y
) return;
2470 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2472 Refresh( true, &rect
);
2475 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2477 wxCHECK_RET( newCurrent
< GetRowCount(),
2478 _T("invalid item index in OnArrowChar()") );
2480 // if there is no selection, we cannot move it anywhere
2481 if (!HasCurrentRow())
2484 unsigned int oldCurrent
= m_currentRow
;
2486 // in single selection we just ignore Shift as we can't select several
2488 if ( event
.ShiftDown() && !IsSingleSel() )
2490 RefreshRow( oldCurrent
);
2492 ChangeCurrentRow( newCurrent
);
2494 // select all the items between the old and the new one
2495 if ( oldCurrent
> newCurrent
)
2497 newCurrent
= oldCurrent
;
2498 oldCurrent
= m_currentRow
;
2501 SelectRows( oldCurrent
, newCurrent
, true );
2502 if (oldCurrent
!=newCurrent
)
2503 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2507 RefreshRow( oldCurrent
);
2509 // all previously selected items are unselected unless ctrl is held
2510 if ( !event
.ControlDown() )
2511 SelectAllRows(false);
2513 ChangeCurrentRow( newCurrent
);
2515 if ( !event
.ControlDown() )
2517 SelectRow( m_currentRow
, true );
2518 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2521 RefreshRow( m_currentRow
);
2524 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2527 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2531 rect
.y
= GetLineStart( row
);
2532 rect
.width
= GetEndOfLastCol();
2533 rect
.height
= GetLineHeight( row
);
2538 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2540 const wxDataViewModel
*model
= GetOwner()->GetModel();
2542 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2544 // TODO make more efficient
2549 for (r
= 0; r
< row
; r
++)
2551 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2552 if (!node
) return start
;
2554 wxDataViewItem item
= node
->GetItem();
2556 if (node
&& !node
->HasChildren())
2558 // Yes, if the node does not have any child, it must be a leaf which
2559 // mean that it is a temporarily created by GetTreeNodeByRow
2563 unsigned int cols
= GetOwner()->GetColumnCount();
2565 int height
= m_lineHeight
;
2566 for (col
= 0; col
< cols
; col
++)
2568 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2569 if (column
->IsHidden())
2570 continue; // skip it!
2572 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2573 continue; // skip it!
2575 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2577 model
->GetValue( value
, item
, column
->GetModelColumn() );
2578 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2579 renderer2
->SetValue( value
);
2580 height
= wxMax( height
, renderer
->GetSize().y
);
2591 return row
* m_lineHeight
;
2595 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2597 const wxDataViewModel
*model
= GetOwner()->GetModel();
2599 // check for the easy case first
2600 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2601 return y
/ m_lineHeight
;
2603 // TODO make more efficient
2604 unsigned int row
= 0;
2605 unsigned int yy
= 0;
2608 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2611 // not really correct...
2612 return row
+ ((y
-yy
) / m_lineHeight
);
2615 wxDataViewItem item
= node
->GetItem();
2617 if (node
&& !node
->HasChildren())
2619 // Yes, if the node does not have any child, it must be a leaf which
2620 // mean that it is a temporarily created by GetTreeNodeByRow
2624 unsigned int cols
= GetOwner()->GetColumnCount();
2626 int height
= m_lineHeight
;
2627 for (col
= 0; col
< cols
; col
++)
2629 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2630 if (column
->IsHidden())
2631 continue; // skip it!
2633 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2634 continue; // skip it!
2636 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2638 model
->GetValue( value
, item
, column
->GetModelColumn() );
2639 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2640 renderer2
->SetValue( value
);
2641 height
= wxMax( height
, renderer
->GetSize().y
);
2652 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2654 const wxDataViewModel
*model
= GetOwner()->GetModel();
2656 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2658 wxASSERT( !IsVirtualList() );
2660 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2661 // wxASSERT( node );
2662 if (!node
) return m_lineHeight
;
2664 wxDataViewItem item
= node
->GetItem();
2666 if (node
&& !node
->HasChildren())
2668 // Yes, if the node does not have any child, it must be a leaf which
2669 // mean that it is a temporarily created by GetTreeNodeByRow
2673 int height
= m_lineHeight
;
2675 unsigned int cols
= GetOwner()->GetColumnCount();
2677 for (col
= 0; col
< cols
; col
++)
2679 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2680 if (column
->IsHidden())
2681 continue; // skip it!
2683 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2684 continue; // skip it!
2686 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2688 model
->GetValue( value
, item
, column
->GetModelColumn() );
2689 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2690 renderer2
->SetValue( value
);
2691 height
= wxMax( height
, renderer
->GetSize().y
);
2698 return m_lineHeight
;
2702 class RowToItemJob
: public DoJob
2705 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2706 virtual ~RowToItemJob() { }
2708 virtual int operator() ( wxDataViewTreeNode
* node
)
2711 if( current
== static_cast<int>(row
))
2713 ret
= node
->GetItem();
2717 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2719 current
+= node
->GetSubTreeCount();
2724 //If the current has no child node, we can find the desired item of the row number directly.
2725 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2726 if( node
->GetNodes().GetCount() == 0)
2728 int index
= static_cast<int>(row
) - current
- 1;
2729 ret
= node
->GetChildren().Item( index
);
2736 virtual int operator() ( void * n
)
2739 if( current
== static_cast<int>(row
))
2741 ret
= wxDataViewItem( n
);
2746 wxDataViewItem
GetResult(){ return ret
; }
2753 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2757 return wxDataViewItem( wxUIntToPtr(row
) );
2761 RowToItemJob
job( row
, -2 );
2762 Walker( m_root
, job
);
2763 return job
.GetResult();
2767 class RowToTreeNodeJob
: public DoJob
2770 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2773 this->current
= current
;
2777 virtual ~RowToTreeNodeJob(){ }
2779 virtual int operator() ( wxDataViewTreeNode
* node
)
2782 if( current
== static_cast<int>(row
))
2788 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2790 current
+= node
->GetSubTreeCount();
2796 //If the current has no child node, we can find the desired item of the row number directly.
2797 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2798 if( node
->GetNodes().GetCount() == 0)
2800 int index
= static_cast<int>(row
) - current
- 1;
2801 void * n
= node
->GetChildren().Item( index
);
2802 ret
= new wxDataViewTreeNode( parent
);
2803 ret
->SetItem( wxDataViewItem( n
));
2804 ret
->SetHasChildren(false);
2813 virtual int operator() ( void * n
)
2816 if( current
== static_cast<int>(row
))
2818 ret
= new wxDataViewTreeNode( parent
);
2819 ret
->SetItem( wxDataViewItem( n
));
2820 ret
->SetHasChildren(false);
2826 wxDataViewTreeNode
* GetResult(){ return ret
; }
2830 wxDataViewTreeNode
* ret
;
2831 wxDataViewTreeNode
* parent
;
2835 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2837 wxASSERT( !IsVirtualList() );
2839 RowToTreeNodeJob
job( row
, -2, m_root
);
2840 Walker( m_root
, job
);
2841 return job
.GetResult();
2844 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2846 wxWindow
*parent
= GetParent();
2847 wxDataViewEvent
le(type
, parent
->GetId());
2849 le
.SetEventObject(parent
);
2850 le
.SetModel(GetOwner()->GetModel());
2853 parent
->GetEventHandler()->ProcessEvent(le
);
2858 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2860 if (IsVirtualList())
2863 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2867 if (!node
->HasChildren())
2873 return node
->IsOpen();
2877 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2879 if (IsVirtualList())
2882 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2885 if( node
->HasChildren())
2887 if( !node
->IsOpen())
2889 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2890 //Check if the user prevent expanding
2891 if( e
.GetSkipped() )
2895 //Here I build the children of current node
2896 if( node
->GetChildrenNumber() == 0 )
2899 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2902 // By expanding the node all row indices that are currently in the selection list
2903 // and are greater than our node have become invalid. So we have to correct that now.
2904 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2905 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2907 const unsigned testRow
= m_selection
[i
];
2908 // all rows above us are not affected, so skip them
2912 m_selection
[i
] += rowAdjustment
;
2915 if(m_currentRow
> row
)
2916 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2920 //Send the expanded event
2921 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2925 SelectRow( row
, false );
2926 SelectRow( row
+ 1, true );
2927 ChangeCurrentRow( row
+ 1 );
2928 SendSelectionChangedEvent( GetItemByRow(row
+1));
2936 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2938 if (IsVirtualList())
2941 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2944 wxDataViewTreeNode
* nd
= node
;
2946 if( node
->HasChildren() && node
->IsOpen() )
2948 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2949 if( e
.GetSkipped() )
2952 // Find out if there are selected items below the current node.
2953 bool selectCollapsingRow
= false;
2954 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2955 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2956 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2958 const unsigned testRow
= m_selection
[i
];
2959 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2961 selectCollapsingRow
= true;
2962 // get out as soon as we have found a node that is selected
2969 // If the node to be closed has selected items the user won't see those any longer.
2970 // We select the collapsing node in this case.
2971 if(selectCollapsingRow
)
2973 SelectAllRows(false);
2974 ChangeCurrentRow(row
);
2975 SelectRow(row
, true);
2976 SendSelectionChangedEvent(GetItemByRow(row
));
2980 // if there were no selected items below our node we still need to "fix" the
2981 // selection list to adjust for the changing of the row indices.
2982 // We actually do the opposite of what we are doing in OnExpanding().
2983 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2985 const unsigned testRow
= m_selection
[i
];
2986 // all rows above us are not affected, so skip them
2990 m_selection
[i
] -= rowAdjustment
;
2993 // if the "current row" is being collapsed away we change it to the current row ;-)
2994 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2995 ChangeCurrentRow(row
);
2996 else if(m_currentRow
> row
)
2997 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
3002 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3006 node
= node
->GetParent();
3009 int parent
= GetRowByItem( node
->GetItem() );
3012 SelectRow( row
, false);
3013 SelectRow(parent
, true );
3014 ChangeCurrentRow( parent
);
3015 SendSelectionChangedEvent( node
->GetItem() );
3019 if( !nd
->HasChildren())
3024 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3026 wxDataViewModel
* model
= GetOwner()->GetModel();
3033 //Compose the a parent-chain of the finding item
3035 list
.DeleteContents( true );
3036 wxDataViewItem
it( item
);
3039 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3040 list
.Insert( pItem
);
3041 it
= model
->GetParent( it
);
3044 //Find the item along the parent-chain.
3045 //This algorithm is designed to speed up the node-finding method
3046 wxDataViewTreeNode
* node
= m_root
;
3047 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3049 if( node
->HasChildren() )
3051 if( node
->GetChildrenNumber() == 0 )
3054 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3057 wxDataViewTreeNodes nodes
= node
->GetNodes();
3061 for (i
= 0; i
< nodes
.GetCount(); i
++)
3063 if (nodes
[i
]->GetItem() == (**iter
))
3065 if (nodes
[i
]->GetItem() == item
)
3082 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3084 wxDataViewColumn
*col
= NULL
;
3085 unsigned int cols
= GetOwner()->GetColumnCount();
3086 unsigned int colnum
= 0;
3088 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3089 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3091 col
= GetOwner()->GetColumnAt(colnum
);
3092 if (col
->IsHidden())
3093 continue; // skip it!
3095 unsigned int w
= col
->GetWidth();
3096 if (x_start
+w
>= (unsigned int)x
)
3103 item
= GetItemByRow( GetLineAt( y
) );
3106 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3108 int row
= GetRowByItem(item
);
3109 int y
= GetLineStart( row
);
3110 int h
= GetLineHeight( m_lineHeight
);
3112 wxDataViewColumn
*col
= NULL
;
3113 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3115 col
= GetOwner()->GetColumnAt( i
);
3116 x
+= col
->GetWidth();
3117 if( GetOwner()->GetColumnAt(i
+1) == column
)
3120 int w
= col
->GetWidth();
3121 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3122 return wxRect(x
, y
, w
, h
);
3125 int wxDataViewMainWindow::RecalculateCount()
3129 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3131 return list_model
->GetLastIndex() + 1;
3133 return list_model
->GetLastIndex() - 1;
3138 return m_root
->GetSubTreeCount();
3142 class ItemToRowJob
: public DoJob
3145 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3152 //Maybe binary search will help to speed up this process
3153 virtual int operator() ( wxDataViewTreeNode
* node
)
3156 if( node
->GetItem() == item
)
3161 if( node
->GetItem() == **m_iter
)
3168 ret
+= node
->GetSubTreeCount();
3174 virtual int operator() ( void * n
)
3177 if( n
== item
.GetID() )
3181 //the row number is begin from zero
3182 int GetResult() { return ret
-1; }
3185 ItemList::const_iterator m_iter
;
3186 wxDataViewItem item
;
3191 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3193 const wxDataViewModel
* model
= GetOwner()->GetModel();
3199 return wxPtrToUInt( item
.GetID() );
3206 //Compose the a parent-chain of the finding item
3208 wxDataViewItem
* pItem
;
3209 list
.DeleteContents( true );
3210 wxDataViewItem
it( item
);
3213 pItem
= new wxDataViewItem( it
);
3214 list
.Insert( pItem
);
3215 it
= model
->GetParent( it
);
3217 pItem
= new wxDataViewItem( );
3218 list
.Insert( pItem
);
3220 ItemToRowJob
job( item
, list
.begin() );
3221 Walker(m_root
, job
);
3222 return job
.GetResult();
3226 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3228 if( !model
->IsContainer( item
) )
3231 wxDataViewItemArray children
;
3232 unsigned int num
= model
->GetChildren( item
, children
);
3234 unsigned int index
= 0;
3235 while( index
< num
)
3237 if( model
->IsContainer( children
[index
] ) )
3239 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3240 n
->SetItem(children
[index
]);
3241 n
->SetHasChildren( true );
3246 node
->AddLeaf( children
[index
].GetID() );
3250 node
->SetSubTreeCount( num
);
3251 wxDataViewTreeNode
* n
= node
->GetParent();
3253 n
->ChangeSubTreeCount(num
);
3257 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3261 if (GetOwner()->GetModel()->IsVirtualListModel())
3267 m_root
= new wxDataViewTreeNode( NULL
);
3268 m_root
->SetHasChildren(true);
3270 //First we define a invalid item to fetch the top-level elements
3271 wxDataViewItem item
;
3273 BuildTreeHelper( model
, item
, m_root
);
3277 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3279 if( node
->GetNodeNumber() != 0 )
3281 int len
= node
->GetNodeNumber();
3283 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3284 for(; i
< len
; i
++ )
3286 DestroyTreeHelper(nodes
[i
]);
3292 void wxDataViewMainWindow::DestroyTree()
3294 if (!IsVirtualList())
3296 ::DestroyTreeHelper(m_root
);
3302 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3304 if ( GetParent()->HandleAsNavigationKey(event
) )
3307 // no item -> nothing to do
3308 if (!HasCurrentRow())
3314 // don't use m_linesPerPage directly as it might not be computed yet
3315 const int pageSize
= GetCountPerPage();
3316 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3318 switch ( event
.GetKeyCode() )
3322 if (m_currentRow
>= 0)
3324 wxWindow
*parent
= GetParent();
3325 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3326 le
.SetItem( GetItemByRow(m_currentRow
) );
3327 le
.SetEventObject(parent
);
3328 le
.SetModel(GetOwner()->GetModel());
3330 parent
->GetEventHandler()->ProcessEvent(le
);
3335 if ( m_currentRow
> 0 )
3336 OnArrowChar( m_currentRow
- 1, event
);
3340 if ( m_currentRow
< GetRowCount() - 1 )
3341 OnArrowChar( m_currentRow
+ 1, event
);
3343 //Add the process for tree expanding/collapsing
3345 OnCollapsing(m_currentRow
);
3348 OnExpanding( m_currentRow
);
3352 OnArrowChar( GetRowCount() - 1, event
);
3357 OnArrowChar( 0, event
);
3362 int steps
= pageSize
- 1;
3363 int index
= m_currentRow
- steps
;
3367 OnArrowChar( index
, event
);
3373 int steps
= pageSize
- 1;
3374 unsigned int index
= m_currentRow
+ steps
;
3375 unsigned int count
= GetRowCount();
3376 if ( index
>= count
)
3379 OnArrowChar( index
, event
);
3388 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3390 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3392 // let the base handle mouse wheel events.
3397 int x
= event
.GetX();
3398 int y
= event
.GetY();
3399 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3400 wxDataViewColumn
*col
= NULL
;
3403 unsigned int cols
= GetOwner()->GetColumnCount();
3405 for (i
= 0; i
< cols
; i
++)
3407 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3409 continue; // skip it!
3411 if (x
< xpos
+ c
->GetWidth())
3416 xpos
+= c
->GetWidth();
3421 wxDataViewRenderer
*cell
= col
->GetRenderer();
3422 unsigned int current
= GetLineAt( y
);
3423 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3425 // Unselect all if below the last row ?
3429 //Test whether the mouse is hovered on the tree item button
3430 bool hoverOverExpander
= false;
3431 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3433 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3434 if( node
!=NULL
&& node
->HasChildren() )
3436 int indent
= node
->GetIndentLevel();
3437 indent
= GetOwner()->GetIndent()*indent
;
3439 // we make the rectangle we are looking in a bit bigger than the actual
3440 // visual expander so the user can hit that little thing reliably
3441 wxRect
rect( xpos
+ indent
,
3442 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3443 m_lineHeight
, m_lineHeight
);
3444 if( rect
.Contains(x
, y
) )
3446 //So the mouse is over the expander
3447 hoverOverExpander
= true;
3448 if (m_underMouse
&& m_underMouse
!= node
)
3450 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3451 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3453 if (m_underMouse
!= node
)
3455 //wxLogMessage("Do the row: %d", current);
3456 RefreshRow(current
);
3458 m_underMouse
= node
;
3461 if (node
!=NULL
&& !node
->HasChildren())
3464 if (!hoverOverExpander
)
3466 if (m_underMouse
!= NULL
)
3468 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3469 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3470 m_underMouse
= NULL
;
3474 wxDataViewModel
*model
= GetOwner()->GetModel();
3476 #if wxUSE_DRAG_AND_DROP
3477 if (event
.Dragging())
3479 if (m_dragCount
== 0)
3481 // we have to report the raw, physical coords as we want to be
3482 // able to call HitTest(event.m_pointDrag) from the user code to
3483 // get the item being dragged
3484 m_dragStart
= event
.GetPosition();
3489 if (m_dragCount
!= 3)
3492 if (event
.LeftIsDown())
3494 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
, &m_dragStart
.x
, &m_dragStart
.y
);
3495 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3496 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3498 // Notify cell about drag
3499 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3500 event
.SetEventObject( m_owner
);
3501 event
.SetItem( item
);
3502 event
.SetModel( model
);
3503 if (!m_owner
->HandleWindowEvent( event
))
3506 if (!event
.IsAllowed())
3509 wxDataObject
*obj
= event
.GetDataObject();
3513 wxDataViewDropSource
drag( this, drag_item_row
);
3514 drag
.SetData( *obj
);
3515 /* wxDragResult res = */ drag
.DoDragDrop();
3524 #endif // wxUSE_DRAG_AND_DROP
3526 bool simulateClick
= false;
3528 if (event
.ButtonDClick())
3530 m_renameTimer
->Stop();
3531 m_lastOnSame
= false;
3534 wxDataViewItem item
= GetItemByRow(current
);
3535 bool ignore_other_columns
=
3536 ((GetOwner()->GetExpanderColumn() != col
) &&
3537 (model
->IsContainer(item
)) &&
3538 (!model
->HasContainerColumns(item
)));
3540 if (event
.LeftDClick())
3542 if(hoverOverExpander
)
3544 // a double click on the expander will be converted into a "simulated" normal click
3545 simulateClick
= true;
3547 else if ( current
== m_lineLastClicked
)
3549 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3552 model
->GetValue( value
, item
, col
->GetModelColumn() );
3553 cell
->SetValue( value
);
3554 wxRect
cell_rect( xpos
, GetLineStart( current
),
3555 col
->GetWidth(), GetLineHeight( current
) );
3556 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3561 wxWindow
*parent
= GetParent();
3562 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3564 le
.SetEventObject(parent
);
3565 le
.SetModel(GetOwner()->GetModel());
3567 parent
->GetEventHandler()->ProcessEvent(le
);
3573 // The first click was on another item, so don't interpret this as
3574 // a double click, but as a simple click instead
3575 simulateClick
= true;
3579 if (event
.LeftUp() && !hoverOverExpander
)
3581 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3583 // select single line
3584 SelectAllRows( false );
3585 SelectRow( m_lineSelectSingleOnUp
, true );
3586 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3589 //If the user click the expander, we do not do editing even if the column with expander are editable
3590 if (m_lastOnSame
&& !ignore_other_columns
)
3592 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3593 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3595 m_renameTimer
->Start( 100, true );
3599 m_lastOnSame
= false;
3600 m_lineSelectSingleOnUp
= (unsigned int)-1;
3602 else if(!event
.LeftUp())
3604 // This is necessary, because after a DnD operation in
3605 // from and to ourself, the up event is swallowed by the
3606 // DnD code. So on next non-up event (which means here and
3607 // now) m_lineSelectSingleOnUp should be reset.
3608 m_lineSelectSingleOnUp
= (unsigned int)-1;
3611 if (event
.RightDown())
3613 m_lineBeforeLastClicked
= m_lineLastClicked
;
3614 m_lineLastClicked
= current
;
3616 // If the item is already selected, do not update the selection.
3617 // Multi-selections should not be cleared if a selected item is clicked.
3618 if (!IsRowSelected(current
))
3620 SelectAllRows(false);
3621 ChangeCurrentRow(current
);
3622 SelectRow(m_currentRow
,true);
3623 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3627 model
->GetValue( value
, item
, col
->GetModelColumn() );
3628 wxWindow
*parent
= GetParent();
3629 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3631 le
.SetEventObject(parent
);
3632 le
.SetModel(GetOwner()->GetModel());
3634 parent
->GetEventHandler()->ProcessEvent(le
);
3636 else if (event
.MiddleDown())
3640 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3642 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3643 // hoverOverExpander being true tells us that our node must be valid and have children.
3644 // So we don't need any extra checks.
3645 if( node
->IsOpen() )
3646 OnCollapsing(current
);
3648 OnExpanding(current
);
3650 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3654 m_lineBeforeLastClicked
= m_lineLastClicked
;
3655 m_lineLastClicked
= current
;
3657 unsigned int oldCurrentRow
= m_currentRow
;
3658 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3660 bool cmdModifierDown
= event
.CmdDown();
3661 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3663 if ( IsSingleSel() || !IsRowSelected(current
) )
3665 SelectAllRows( false );
3666 ChangeCurrentRow(current
);
3667 SelectRow(m_currentRow
,true);
3668 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3670 else // multi sel & current is highlighted & no mod keys
3672 m_lineSelectSingleOnUp
= current
;
3673 ChangeCurrentRow(current
); // change focus
3676 else // multi sel & either ctrl or shift is down
3678 if (cmdModifierDown
)
3680 ChangeCurrentRow(current
);
3681 ReverseRowSelection(m_currentRow
);
3682 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3684 else if (event
.ShiftDown())
3686 ChangeCurrentRow(current
);
3688 unsigned int lineFrom
= oldCurrentRow
,
3691 if ( lineTo
< lineFrom
)
3694 lineFrom
= m_currentRow
;
3697 SelectRows(lineFrom
, lineTo
, true);
3698 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3700 else // !ctrl, !shift
3702 // test in the enclosing if should make it impossible
3703 wxFAIL_MSG( _T("how did we get here?") );
3707 if (m_currentRow
!= oldCurrentRow
)
3708 RefreshRow( oldCurrentRow
);
3710 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3712 // Update selection here...
3715 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3716 (current
== oldCurrentRow
)) && oldWasSelected
;
3718 // Call LeftClick after everything else as under GTK+
3719 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3721 // notify cell about right click
3723 model
->GetValue( value
, item
, col
->GetModelColumn() );
3724 cell
->SetValue( value
);
3725 wxRect
cell_rect( xpos
, GetLineStart( current
),
3726 col
->GetWidth(), GetLineHeight( current
) );
3727 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3732 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3736 if (HasCurrentRow())
3742 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3746 if (HasCurrentRow())
3752 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3754 if( m_selection
.GetCount() != 1 )
3755 return wxDataViewItem();
3757 return GetItemByRow( m_selection
.Item(0));
3760 //-----------------------------------------------------------------------------
3762 //-----------------------------------------------------------------------------
3763 WX_DEFINE_LIST(wxDataViewColumnList
)
3765 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3767 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3768 EVT_SIZE(wxDataViewCtrl::OnSize
)
3771 wxDataViewCtrl::~wxDataViewCtrl()
3774 GetModel()->RemoveNotifier( m_notifier
);
3779 void wxDataViewCtrl::Init()
3781 m_cols
.DeleteContents(true);
3784 // No sorting column at start
3785 m_sortingColumnIdx
= wxNOT_FOUND
;
3787 m_headerArea
= NULL
;
3790 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3791 const wxPoint
& pos
, const wxSize
& size
,
3792 long style
, const wxValidator
& validator
)
3794 // if ( (style & wxBORDER_MASK) == 0)
3795 // style |= wxBORDER_SUNKEN;
3799 if (!wxControl::Create( parent
, id
, pos
, size
,
3800 style
| wxScrolledWindowStyle
, validator
))
3803 SetInitialSize(size
);
3806 MacSetClipChildren( true );
3809 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3811 if (HasFlag(wxDV_NO_HEADER
))
3812 m_headerArea
= NULL
;
3814 m_headerArea
= new wxDataViewHeaderWindow(this);
3816 SetTargetWindow( m_clientArea
);
3818 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3820 sizer
->Add( m_headerArea
, 0, wxGROW
);
3821 sizer
->Add( m_clientArea
, 1, wxGROW
);
3827 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3829 return wxBORDER_THEME
;
3833 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3837 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3840 // we need to process arrows ourselves for scrolling
3841 if ( nMsg
== WM_GETDLGCODE
)
3843 rc
|= DLGC_WANTARROWS
;
3851 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3853 wxSize newsize
= size
;
3854 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3855 newsize
.y
-= m_headerArea
->GetSize().y
;
3860 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3862 // We need to override OnSize so that our scrolled
3863 // window a) does call Layout() to use sizers for
3864 // positioning the controls but b) does not query
3865 // the sizer for their size and use that for setting
3866 // the scrollable area as set that ourselves by
3867 // calling SetScrollbar() further down.
3874 void wxDataViewCtrl::SetFocus()
3877 m_clientArea
->SetFocus();
3880 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3882 if (!wxDataViewCtrlBase::AssociateModel( model
))
3885 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3887 model
->AddNotifier( m_notifier
);
3889 m_clientArea
->DestroyTree();
3891 m_clientArea
->BuildTree(model
);
3893 m_clientArea
->UpdateDisplay();
3898 #if wxUSE_DRAG_AND_DROP
3900 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3902 return m_clientArea
->EnableDragSource( format
);
3905 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3907 return m_clientArea
->EnableDropTarget( format
);
3910 #endif // wxUSE_DRAG_AND_DROP
3912 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3914 if (!wxDataViewCtrlBase::AppendColumn(col
))
3917 m_cols
.Append( col
);
3918 OnColumnsCountChanged();
3922 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3924 if (!wxDataViewCtrlBase::PrependColumn(col
))
3927 m_cols
.Insert( col
);
3928 OnColumnsCountChanged();
3932 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3934 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3937 m_cols
.Insert( pos
, col
);
3938 OnColumnsCountChanged();
3942 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3945 m_headerArea
->UpdateColumn(idx
);
3947 m_clientArea
->UpdateDisplay();
3950 void wxDataViewCtrl::OnColumnsCountChanged()
3953 m_headerArea
->SetColumnCount(GetColumnCount());
3955 m_clientArea
->UpdateDisplay();
3958 void wxDataViewCtrl::DoSetExpanderColumn()
3960 m_clientArea
->UpdateDisplay();
3963 void wxDataViewCtrl::DoSetIndent()
3965 m_clientArea
->UpdateDisplay();
3968 unsigned int wxDataViewCtrl::GetColumnCount() const
3970 return m_cols
.GetCount();
3973 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3978 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3980 // columns can't be reordered if there is no header window which allows
3982 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3985 return GetColumn(idx
);
3988 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3990 const unsigned count
= m_cols
.size();
3991 for ( unsigned n
= 0; n
< count
; n
++ )
3993 if ( m_cols
[n
] == column
)
4000 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
4001 unsigned int WXUNUSED(new_pos
))
4003 // do _not_ reorder m_cols elements here, they should always be in the
4004 // order in which columns were added, we only display the columns in
4006 m_clientArea
->UpdateDisplay();
4009 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4011 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4016 OnColumnsCountChanged();
4021 bool wxDataViewCtrl::ClearColumns()
4024 OnColumnsCountChanged();
4028 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4032 unsigned int len
= GetColumnCount();
4033 for ( unsigned int i
= 0; i
< len
; i
++ )
4035 wxDataViewColumn
* col
= GetColumnAt(i
);
4036 if (col
->IsHidden())
4038 ret
+= col
->GetWidth();
4041 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4048 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4050 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4051 : GetColumn(m_sortingColumnIdx
);
4054 //Selection code with wxDataViewItem as parameters
4055 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4057 return m_clientArea
->GetSelection();
4060 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4063 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4064 int len
= selection
.GetCount();
4065 for( int i
= 0; i
< len
; i
++)
4067 unsigned int row
= selection
[i
];
4068 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4073 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4075 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4077 wxDataViewItem last_parent
;
4079 int len
= sel
.GetCount();
4080 for( int i
= 0; i
< len
; i
++ )
4082 wxDataViewItem item
= sel
[i
];
4083 wxDataViewItem parent
= GetModel()->GetParent( item
);
4086 if (parent
!= last_parent
)
4087 ExpandAncestors(item
);
4090 last_parent
= parent
;
4091 int row
= m_clientArea
->GetRowByItem( item
);
4093 selection
.Add( static_cast<unsigned int>(row
) );
4096 m_clientArea
->SetSelections( selection
);
4099 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4101 ExpandAncestors( item
);
4103 int row
= m_clientArea
->GetRowByItem( item
);
4106 //Unselect all rows before select another in the single select mode
4107 if (m_clientArea
->IsSingleSel())
4108 m_clientArea
->SelectAllRows(false);
4109 m_clientArea
->SelectRow(row
, true);
4113 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4115 int row
= m_clientArea
->GetRowByItem( item
);
4117 m_clientArea
->SelectRow(row
, false);
4120 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4122 int row
= m_clientArea
->GetRowByItem( item
);
4125 return m_clientArea
->IsRowSelected(row
);
4130 //Selection code with row number as parameter
4131 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4134 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4135 int len
= selection
.GetCount();
4136 for( int i
= 0; i
< len
; i
++)
4138 unsigned int row
= selection
[i
];
4144 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4146 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4147 int len
= sel
.GetCount();
4148 for( int i
= 0; i
< len
; i
++ )
4152 selection
.Add( static_cast<unsigned int>(row
) );
4154 m_clientArea
->SetSelections( selection
);
4157 void wxDataViewCtrl::Select( int row
)
4161 if (m_clientArea
->IsSingleSel())
4162 m_clientArea
->SelectAllRows(false);
4163 m_clientArea
->SelectRow( row
, true );
4167 void wxDataViewCtrl::Unselect( int row
)
4170 m_clientArea
->SelectRow(row
, false);
4173 bool wxDataViewCtrl::IsSelected( int row
) const
4176 return m_clientArea
->IsRowSelected(row
);
4180 void wxDataViewCtrl::SelectRange( int from
, int to
)
4183 for( int i
= from
; i
< to
; i
++ )
4185 m_clientArea
->Select(sel
);
4188 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4190 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4191 for( int i
= from
; i
< to
; i
++ )
4192 if( sel
.Index( i
) != wxNOT_FOUND
)
4194 m_clientArea
->SetSelections(sel
);
4197 void wxDataViewCtrl::SelectAll()
4199 m_clientArea
->SelectAllRows(true);
4202 void wxDataViewCtrl::UnselectAll()
4204 m_clientArea
->SelectAllRows(false);
4207 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4211 if( row
> (int) m_clientArea
->GetRowCount() )
4212 row
= m_clientArea
->GetRowCount();
4214 int first
= m_clientArea
->GetFirstVisibleRow();
4215 int last
= m_clientArea
->GetLastVisibleRow();
4217 m_clientArea
->ScrollTo( row
, column
);
4218 else if( row
> last
)
4219 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4221 m_clientArea
->ScrollTo( first
, column
);
4224 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4226 ExpandAncestors( item
);
4228 m_clientArea
->RecalculateDisplay();
4230 int row
= m_clientArea
->GetRowByItem(item
);
4233 if( column
== NULL
)
4234 EnsureVisible(row
, -1);
4236 EnsureVisible( row
, GetColumnIndex(column
) );
4241 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4243 m_clientArea
->HitTest(point
, item
, column
);
4246 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4248 return m_clientArea
->GetItemRect(item
, column
);
4251 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4253 return m_clientArea
->GetItemByRow( row
);
4256 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4258 return m_clientArea
->GetRowByItem( item
);
4261 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4263 int row
= m_clientArea
->GetRowByItem( item
);
4265 m_clientArea
->Expand(row
);
4268 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4270 int row
= m_clientArea
->GetRowByItem( item
);
4272 m_clientArea
->Collapse(row
);
4275 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4277 int row
= m_clientArea
->GetRowByItem( item
);
4279 return m_clientArea
->IsExpanded(row
);
4285 // !wxUSE_GENERICDATAVIEWCTRL
4288 // wxUSE_DATAVIEWCTRL