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
) )
866 flags
|= wxCONTROL_CHECKED
;
867 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
868 flags
|= wxCONTROL_DISABLED
;
870 wxRendererNative::Get().DrawCheckBox(
871 GetOwner()->GetOwner(),
879 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
880 wxDataViewModel
*model
,
881 const wxDataViewItem
& item
, unsigned int col
)
883 bool value
= !m_toggle
;
884 wxVariant variant
= value
;
885 model
->SetValue( variant
, item
, col
);
886 model
->ValueChanged( item
, col
);
890 wxSize
wxDataViewToggleRenderer::GetSize() const
892 // the window parameter is not used by GetCheckBoxSize() so it's
894 return wxRendererNative::Get().GetCheckBoxSize(NULL
);
897 // ---------------------------------------------------------
898 // wxDataViewProgressRenderer
899 // ---------------------------------------------------------
901 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
903 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
904 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
905 wxDataViewCustomRenderer( varianttype
, mode
, align
)
911 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
915 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
917 m_value
= (long) value
;
919 if (m_value
< 0) m_value
= 0;
920 if (m_value
> 100) m_value
= 100;
925 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
927 value
= (long) m_value
;
931 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
933 double pct
= (double)m_value
/ 100.0;
935 bar
.width
= (int)(cell
.width
* pct
);
936 dc
->SetPen( *wxTRANSPARENT_PEN
);
937 dc
->SetBrush( *wxBLUE_BRUSH
);
938 dc
->DrawRectangle( bar
);
940 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
941 dc
->SetPen( *wxBLACK_PEN
);
942 dc
->DrawRectangle( cell
);
947 wxSize
wxDataViewProgressRenderer::GetSize() const
949 return wxSize(40,12);
952 // ---------------------------------------------------------
953 // wxDataViewDateRenderer
954 // ---------------------------------------------------------
956 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
958 #if wxUSE_DATE_RENDERER_POPUP
960 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
963 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
964 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
965 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
970 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
971 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
972 sizer
->Add( m_cal
, 1, wxGROW
);
977 void OnCalendar( wxCalendarEvent
&event
);
979 wxCalendarCtrl
*m_cal
;
980 wxDataViewModel
*m_model
;
982 const wxDataViewItem
& m_item
;
985 virtual void OnDismiss()
990 DECLARE_EVENT_TABLE()
993 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
994 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
997 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
999 wxDateTime date
= event
.GetDate();
1000 wxVariant value
= date
;
1001 m_model
->SetValue( value
, m_item
, m_col
);
1002 m_model
->ValueChanged( m_item
, m_col
);
1006 #endif // wxUSE_DATE_RENDERER_POPUP
1008 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
1010 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
1011 wxDataViewCellMode mode
, int align
) :
1012 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1016 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
1018 m_date
= value
.GetDateTime();
1023 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
1029 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1031 wxString tmp
= m_date
.FormatDate();
1032 RenderText( tmp
, 0, cell
, dc
, state
);
1036 wxSize
wxDataViewDateRenderer::GetSize() const
1038 const wxDataViewCtrl
* view
= GetView();
1039 wxString tmp
= m_date
.FormatDate();
1041 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
1042 return wxSize(x
,y
+d
);
1045 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
1046 const wxDataViewItem
& item
, unsigned int col
)
1049 model
->GetValue( variant
, item
, col
);
1050 wxDateTime value
= variant
.GetDateTime();
1052 #if wxUSE_DATE_RENDERER_POPUP
1053 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
1054 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
1055 wxPoint pos
= wxGetMousePosition();
1058 popup
->Popup( popup
->m_cal
);
1059 #else // !wxUSE_DATE_RENDERER_POPUP
1060 wxMessageBox(value
.Format());
1061 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
1065 // ---------------------------------------------------------
1066 // wxDataViewIconTextRenderer
1067 // ---------------------------------------------------------
1069 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1071 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1072 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1073 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1076 SetAlignment(align
);
1079 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1083 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1089 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1094 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1097 const wxIcon
&icon
= m_value
.GetIcon();
1100 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1101 xoffset
= icon
.GetWidth()+4;
1104 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1109 wxSize
wxDataViewIconTextRenderer::GetSize() const
1111 const wxDataViewCtrl
*view
= GetView();
1112 if (!m_value
.GetText().empty())
1115 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1117 if (m_value
.GetIcon().IsOk())
1118 x
+= m_value
.GetIcon().GetWidth() + 4;
1119 return wxSize( x
, y
);
1121 return wxSize(80,20);
1125 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1126 wxRect
WXUNUSED(labelRect
),
1127 const wxVariant
& WXUNUSED(value
))
1133 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1134 wxVariant
& WXUNUSED(value
))
1139 //-----------------------------------------------------------------------------
1140 // wxDataViewDropTarget
1141 //-----------------------------------------------------------------------------
1143 #if wxUSE_DRAG_AND_DROP
1145 class wxBitmapCanvas
: public wxWindow
1148 wxBitmapCanvas( wxWindow
*parent
, const wxBitmap
&bitmap
, const wxSize
&size
) :
1149 wxWindow( parent
, wxID_ANY
, wxPoint(0,0), size
)
1152 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxBitmapCanvas::OnPaint
) );
1155 void OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1158 dc
.DrawBitmap( m_bitmap
, 0, 0);
1164 class wxDataViewDropSource
: public wxDropSource
1167 wxDataViewDropSource( wxDataViewMainWindow
*win
, unsigned int row
) :
1175 ~wxDataViewDropSource()
1180 virtual bool GiveFeedback( wxDragResult
WXUNUSED(effect
) )
1182 wxPoint pos
= wxGetMousePosition();
1186 int liney
= m_win
->GetLineStart( m_row
);
1188 m_win
->GetOwner()->CalcUnscrolledPosition( 0, liney
, NULL
, &liney
);
1189 m_win
->ClientToScreen( &linex
, &liney
);
1190 m_dist_x
= pos
.x
- linex
;
1191 m_dist_y
= pos
.y
- liney
;
1194 wxBitmap ib
= m_win
->CreateItemBitmap( m_row
, indent
);
1196 m_hint
= new wxFrame( m_win
->GetParent(), wxID_ANY
, wxEmptyString
,
1197 wxPoint(pos
.x
- m_dist_x
, pos
.y
+ 5 ),
1199 wxFRAME_TOOL_WINDOW
|
1200 wxFRAME_FLOAT_ON_PARENT
|
1201 wxFRAME_NO_TASKBAR
|
1203 new wxBitmapCanvas( m_hint
, ib
, ib
.GetSize() );
1208 m_hint
->Move( pos
.x
- m_dist_x
, pos
.y
+ 5 );
1209 m_hint
->SetTransparent( 128 );
1215 wxDataViewMainWindow
*m_win
;
1218 int m_dist_x
,m_dist_y
;
1222 class wxDataViewDropTarget
: public wxDropTarget
1225 wxDataViewDropTarget( wxDataObject
*obj
, wxDataViewMainWindow
*win
) :
1231 virtual wxDragResult
OnDragOver( wxCoord x
, wxCoord y
, wxDragResult def
)
1233 wxDataFormat format
= GetMatchingPair();
1234 if (format
== wxDF_INVALID
)
1236 return m_win
->OnDragOver( format
, x
, y
, def
);
1239 virtual bool OnDrop( wxCoord x
, wxCoord y
)
1241 wxDataFormat format
= GetMatchingPair();
1242 if (format
== wxDF_INVALID
)
1244 return m_win
->OnDrop( format
, x
, y
);
1247 virtual wxDragResult
OnData( wxCoord x
, wxCoord y
, wxDragResult def
)
1249 wxDataFormat format
= GetMatchingPair();
1250 if (format
== wxDF_INVALID
)
1254 return m_win
->OnData( format
, x
, y
, def
);
1257 virtual void OnLeave()
1258 { m_win
->OnLeave(); }
1260 wxDataViewMainWindow
*m_win
;
1263 #endif // wxUSE_DRAG_AND_DROP
1265 //-----------------------------------------------------------------------------
1266 // wxDataViewRenameTimer
1267 //-----------------------------------------------------------------------------
1269 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1274 void wxDataViewRenameTimer::Notify()
1276 m_owner
->OnRenameTimer();
1279 //-----------------------------------------------------------------------------
1280 // wxDataViewMainWindow
1281 //-----------------------------------------------------------------------------
1283 //The tree building helper, declared firstly
1284 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1286 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1288 if (row1
> row2
) return 1;
1289 if (row1
== row2
) return 0;
1294 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1296 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1297 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1298 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1299 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1300 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1301 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1304 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1305 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1306 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1307 m_selection( wxDataViewSelectionCmp
)
1312 m_lastOnSame
= false;
1313 m_renameTimer
= new wxDataViewRenameTimer( this );
1315 // TODO: user better initial values/nothing selected
1316 m_currentCol
= NULL
;
1319 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1321 #if wxUSE_DRAG_AND_DROP
1323 m_dragStart
= wxPoint(0,0);
1325 m_dragEnabled
= false;
1326 m_dropEnabled
= false;
1328 m_dropHintLine
= (unsigned int) -1;
1329 #endif // wxUSE_DRAG_AND_DROP
1331 m_lineLastClicked
= (unsigned int) -1;
1332 m_lineBeforeLastClicked
= (unsigned int) -1;
1333 m_lineSelectSingleOnUp
= (unsigned int) -1;
1337 SetBackgroundColour( *wxWHITE
);
1339 SetBackgroundStyle(wxBG_STYLE_CUSTOM
);
1341 m_penRule
= wxPen(GetRuleColour());
1343 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1344 m_penExpander
= wxPen(wxColour(0,0,0));
1345 //Some new added code to deal with the tree structure
1346 m_root
= new wxDataViewTreeNode( NULL
);
1347 m_root
->SetHasChildren(true);
1349 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1351 m_underMouse
= NULL
;
1355 wxDataViewMainWindow::~wxDataViewMainWindow()
1358 delete m_renameTimer
;
1362 #if wxUSE_DRAG_AND_DROP
1363 bool wxDataViewMainWindow::EnableDragSource( const wxDataFormat
&format
)
1365 m_dragFormat
= format
;
1366 m_dragEnabled
= format
!= wxDF_INVALID
;
1371 bool wxDataViewMainWindow::EnableDropTarget( const wxDataFormat
&format
)
1373 m_dropFormat
= format
;
1374 m_dropEnabled
= format
!= wxDF_INVALID
;
1377 SetDropTarget( new wxDataViewDropTarget( new wxCustomDataObject( format
), this ) );
1382 void wxDataViewMainWindow::RemoveDropHint()
1387 RefreshRow( m_dropHintLine
);
1388 m_dropHintLine
= (unsigned int) -1;
1392 wxDragResult
wxDataViewMainWindow::OnDragOver( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1396 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1397 unsigned int row
= GetLineAt( yy
);
1399 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1405 wxDataViewItem item
= GetItemByRow( row
);
1407 wxDataViewModel
*model
= GetOwner()->GetModel();
1409 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1410 event
.SetEventObject( m_owner
);
1411 event
.SetItem( item
);
1412 event
.SetModel( model
);
1413 event
.SetDataFormat( format
);
1414 if (!m_owner
->HandleWindowEvent( event
))
1420 if (!event
.IsAllowed())
1427 if (m_dropHint
&& (row
!= m_dropHintLine
))
1428 RefreshRow( m_dropHintLine
);
1430 m_dropHintLine
= row
;
1436 bool wxDataViewMainWindow::OnDrop( wxDataFormat format
, wxCoord x
, wxCoord y
)
1442 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1443 unsigned int row
= GetLineAt( yy
);
1445 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1448 wxDataViewItem item
= GetItemByRow( row
);
1450 wxDataViewModel
*model
= GetOwner()->GetModel();
1452 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, m_owner
->GetId() );
1453 event
.SetEventObject( m_owner
);
1454 event
.SetItem( item
);
1455 event
.SetModel( model
);
1456 event
.SetDataFormat( format
);
1457 if (!m_owner
->HandleWindowEvent( event
))
1460 if (!event
.IsAllowed())
1466 wxDragResult
wxDataViewMainWindow::OnData( wxDataFormat format
, wxCoord x
, wxCoord y
, wxDragResult def
)
1470 m_owner
->CalcUnscrolledPosition( xx
, yy
, &xx
, &yy
);
1471 unsigned int row
= GetLineAt( yy
);
1473 if ((row
>= GetRowCount()) || (yy
> GetEndOfLastCol()))
1476 wxDataViewItem item
= GetItemByRow( row
);
1478 wxDataViewModel
*model
= GetOwner()->GetModel();
1480 wxCustomDataObject
*obj
= (wxCustomDataObject
*) GetDropTarget()->GetDataObject();
1482 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, m_owner
->GetId() );
1483 event
.SetEventObject( m_owner
);
1484 event
.SetItem( item
);
1485 event
.SetModel( model
);
1486 event
.SetDataFormat( format
);
1487 event
.SetDataSize( obj
->GetSize() );
1488 event
.SetDataBuffer( obj
->GetData() );
1489 if (!m_owner
->HandleWindowEvent( event
))
1492 if (!event
.IsAllowed())
1498 void wxDataViewMainWindow::OnLeave()
1503 wxBitmap
wxDataViewMainWindow::CreateItemBitmap( unsigned int row
, int &indent
)
1505 int height
= GetLineHeight( row
);
1507 unsigned int cols
= GetOwner()->GetColumnCount();
1509 for (col
= 0; col
< cols
; col
++)
1511 wxDataViewColumn
*column
= GetOwner()->GetColumnAt(col
);
1512 if (column
->IsHidden())
1513 continue; // skip it!
1514 width
+= column
->GetWidth();
1518 if (!IsVirtualList())
1520 wxDataViewTreeNode
*node
= GetTreeNodeByRow(row
);
1521 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1522 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1524 if(!node
->HasChildren())
1529 wxBitmap
bitmap( width
, height
);
1530 wxMemoryDC
dc( bitmap
);
1531 dc
.SetFont( GetFont() );
1532 dc
.SetPen( *wxBLACK_PEN
);
1533 dc
.SetBrush( *wxWHITE_BRUSH
);
1534 dc
.DrawRectangle( 0,0,width
,height
);
1536 wxDataViewModel
*model
= m_owner
->GetModel();
1538 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1541 // TODO-RTL: last column for RTL support
1542 expander
= GetOwner()->GetColumnAt( 0 );
1543 GetOwner()->SetExpanderColumn(expander
);
1548 for (col
= 0; col
< cols
; col
++)
1550 wxDataViewColumn
*column
= GetOwner()->GetColumnAt( col
);
1551 wxDataViewRenderer
*cell
= column
->GetRenderer();
1553 if (column
->IsHidden())
1554 continue; // skip it!
1556 width
= column
->GetWidth();
1558 if (column
== expander
)
1562 wxDataViewItem item
= GetItemByRow( row
);
1563 model
->GetValue( value
, item
, column
->GetModelColumn());
1564 cell
->SetValue( value
);
1566 if (cell
->GetWantsAttr())
1568 wxDataViewItemAttr attr
;
1569 bool ret
= model
->GetAttr( item
, column
->GetModelColumn(), attr
);
1571 cell
->SetAttr( attr
);
1572 cell
->SetHasAttr( ret
);
1575 wxSize size
= cell
->GetSize();
1576 size
.x
= wxMin( 2*PADDING_RIGHTLEFT
+ size
.x
, width
);
1578 wxRect
item_rect(x
, 0, size
.x
, size
.y
);
1580 int align
= cell
->CalculateAlignment();
1581 // horizontal alignment:
1583 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1584 item_rect
.x
= x
+ (width
/ 2) - (size
.x
/ 2);
1585 else if (align
& wxALIGN_RIGHT
)
1586 item_rect
.x
= x
+ width
- size
.x
;
1587 //else: wxALIGN_LEFT is the default
1589 // vertical alignment:
1591 if (align
& wxALIGN_CENTER_VERTICAL
)
1592 item_rect
.y
= (height
/ 2) - (size
.y
/ 2);
1593 else if (align
& wxALIGN_BOTTOM
)
1594 item_rect
.y
= height
- size
.y
;
1595 //else: wxALIGN_TOP is the default
1598 item_rect
.x
+= PADDING_RIGHTLEFT
;
1599 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1601 //dc.SetClippingRegion( item_rect );
1602 cell
->Render( item_rect
, &dc
, 0 );
1603 //dc.DestroyClippingRegion();
1611 #endif // wxUSE_DRAG_AND_DROP
1614 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1616 wxDataViewModel
*model
= GetOwner()->GetModel();
1617 wxAutoBufferedPaintDC
dc( this );
1620 dc
.SetPen( *wxTRANSPARENT_PEN
);
1621 dc
.SetBrush( wxBrush( GetBackgroundColour()) );
1622 dc
.SetBrush( *wxWHITE_BRUSH
);
1623 wxSize
size( GetClientSize() );
1624 dc
.DrawRectangle( 0,0,size
.x
,size
.y
);
1628 GetOwner()->PrepareDC( dc
);
1629 dc
.SetFont( GetFont() );
1631 wxRect update
= GetUpdateRegion().GetBox();
1632 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1634 // compute which items needs to be redrawn
1635 unsigned int item_start
= GetLineAt( wxMax(0,update
.y
) );
1636 unsigned int item_count
=
1637 wxMin( (int)( GetLineAt( wxMax(0,update
.y
+update
.height
) ) - item_start
+ 1),
1638 (int)(GetRowCount( ) - item_start
));
1639 unsigned int item_last
= item_start
+ item_count
;
1641 // compute which columns needs to be redrawn
1642 unsigned int cols
= GetOwner()->GetColumnCount();
1643 unsigned int col_start
= 0;
1644 unsigned int x_start
;
1645 for (x_start
= 0; col_start
< cols
; col_start
++)
1647 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_start
);
1648 if (col
->IsHidden())
1649 continue; // skip it!
1651 unsigned int w
= col
->GetWidth();
1652 if (x_start
+w
>= (unsigned int)update
.x
)
1658 unsigned int col_last
= col_start
;
1659 unsigned int x_last
= x_start
;
1660 for (; col_last
< cols
; col_last
++)
1662 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(col_last
);
1663 if (col
->IsHidden())
1664 continue; // skip it!
1666 if (x_last
> (unsigned int)update
.GetRight())
1669 x_last
+= col
->GetWidth();
1672 // Draw horizontal rules if required
1673 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
1675 dc
.SetPen(m_penRule
);
1676 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1678 for (unsigned int i
= item_start
; i
<= item_last
; i
++)
1680 int y
= GetLineStart( i
);
1681 dc
.DrawLine(x_start
, y
, x_last
, y
);
1685 // Draw vertical rules if required
1686 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
1688 dc
.SetPen(m_penRule
);
1689 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
1692 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1694 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(i
);
1695 if (col
->IsHidden())
1696 continue; // skip it
1698 dc
.DrawLine(x
, GetLineStart( item_start
),
1699 x
, GetLineStart( item_last
) );
1701 x
+= col
->GetWidth();
1704 // Draw last vertical rule
1705 dc
.DrawLine(x
, GetLineStart( item_start
),
1706 x
, GetLineStart( item_last
) );
1709 // redraw the background for the items which are selected/current
1710 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1712 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
1713 if (selected
|| item
== m_currentRow
)
1715 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
1716 if (item
== m_currentRow
)
1717 flags
|= wxCONTROL_CURRENT
;
1719 flags
|= wxCONTROL_FOCUSED
;
1721 wxRect
rect( x_start
, GetLineStart( item
), x_last
, GetLineHeight( item
) );
1722 wxRendererNative::Get().DrawItemSelectionRect
1732 #if wxUSE_DRAG_AND_DROP
1735 wxRect
rect( x_start
, GetLineStart( m_dropHintLine
),
1736 x_last
, GetLineHeight( m_dropHintLine
) );
1737 dc
.SetPen( *wxBLACK_PEN
);
1738 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1739 dc
.DrawRectangle( rect
);
1741 #endif // wxUSE_DRAG_AND_DROP
1743 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
1746 // TODO-RTL: last column for RTL support
1747 expander
= GetOwner()->GetColumnAt( 0 );
1748 GetOwner()->SetExpanderColumn(expander
);
1751 // redraw all cells for all rows which must be repainted and all columns
1753 cell_rect
.x
= x_start
;
1754 for (unsigned int i
= col_start
; i
< col_last
; i
++)
1757 wxDataViewColumn
*col
= GetOwner()->GetColumnAt( i
);
1758 wxDataViewRenderer
*cell
= col
->GetRenderer();
1759 cell_rect
.width
= col
->GetWidth();
1761 if (col
->IsHidden())
1762 continue; // skip it!
1764 for (unsigned int item
= item_start
; item
< item_last
; item
++)
1766 // get the cell value and set it into the renderer
1768 wxDataViewTreeNode
*node
= NULL
;
1769 wxDataViewItem dataitem
;
1771 if (!IsVirtualList())
1773 node
= GetTreeNodeByRow(item
);
1777 dataitem
= node
->GetItem();
1779 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
1784 dataitem
= wxDataViewItem( wxUIntToPtr(item
) );
1787 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
1788 cell
->SetValue( value
);
1790 if (cell
->GetWantsAttr())
1792 wxDataViewItemAttr attr
;
1793 bool ret
= model
->GetAttr( dataitem
, col
->GetModelColumn(), attr
);
1795 cell
->SetAttr( attr
);
1796 cell
->SetHasAttr( ret
);
1800 cell_rect
.y
= GetLineStart( item
);
1801 cell_rect
.height
= GetLineHeight( item
); // -1 is for the horizontal rules (?)
1803 //Draw the expander here.
1805 if ((!IsVirtualList()) && (col
== expander
))
1807 indent
= node
->GetIndentLevel();
1809 //Calculate the indent first
1810 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
1812 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
1813 // change the cell_rect.x to the appropriate pos
1814 int expander_x
= indent
+ EXPANDER_MARGIN
;
1815 int expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
+ (GetLineHeight(item
) / 2) - (expander_width
/2) - EXPANDER_OFFSET
;
1816 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
1817 dc
.SetPen( m_penExpander
);
1818 dc
.SetBrush( wxNullBrush
);
1819 if( node
->HasChildren() )
1821 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
1823 if (m_underMouse
== node
)
1825 flag
|= wxCONTROL_CURRENT
;
1827 if( node
->IsOpen() )
1828 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
1830 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
1832 //force the expander column to left-center align
1833 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
1835 if (node
&& !node
->HasChildren())
1837 // Yes, if the node does not have any child, it must be a leaf which
1838 // mean that it is a temporarily created by GetTreeNodeByRow
1842 // cannot be bigger than allocated space
1843 wxSize size
= cell
->GetSize();
1844 // Because of the tree structure indent, here we should minus the width of the cell for drawing
1845 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
1846 // size.y = wxMin( size.y, cell_rect.height );
1847 size
.y
= cell_rect
.height
;
1849 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
1850 int align
= cell
->CalculateAlignment();
1852 // horizontal alignment:
1853 item_rect
.x
= cell_rect
.x
;
1854 if (align
& wxALIGN_CENTER_HORIZONTAL
)
1855 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1856 else if (align
& wxALIGN_RIGHT
)
1857 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
1858 //else: wxALIGN_LEFT is the default
1860 // vertical alignment:
1861 item_rect
.y
= cell_rect
.y
;
1862 if (align
& wxALIGN_CENTER_VERTICAL
)
1863 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1864 else if (align
& wxALIGN_BOTTOM
)
1865 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
1866 //else: wxALIGN_TOP is the default
1869 item_rect
.x
+= PADDING_RIGHTLEFT
;
1870 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
1872 //Here we add the tree indent
1873 item_rect
.x
+= indent
;
1876 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
1877 state
|= wxDATAVIEW_CELL_SELECTED
;
1879 // TODO: it would be much more efficient to create a clipping
1880 // region for the entire column being rendered (in the OnPaint
1881 // of wxDataViewMainWindow) instead of a single clip region for
1882 // each cell. However it would mean that each renderer should
1883 // respect the given wxRect's top & bottom coords, eventually
1884 // violating only the left & right coords - however the user can
1885 // make its own renderer and thus we cannot be sure of that.
1886 dc
.SetClippingRegion( item_rect
);
1887 cell
->Render( item_rect
, &dc
, state
);
1888 dc
.DestroyClippingRegion();
1891 cell_rect
.x
+= cell_rect
.width
;
1895 void wxDataViewMainWindow::OnRenameTimer()
1897 // We have to call this here because changes may just have
1898 // been made and no screen update taken place.
1901 // TODO: use wxTheApp->SafeYieldFor(NULL, wxEVT_CATEGORY_UI) instead
1902 // (needs to be tested!)
1907 unsigned int cols
= GetOwner()->GetColumnCount();
1909 for (i
= 0; i
< cols
; i
++)
1911 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
1913 continue; // skip it!
1915 if (c
== m_currentCol
)
1917 xpos
+= c
->GetWidth();
1920 // we have to take an expander column into account and compute its indentation
1921 // to get the editor at the correct x position where the actual text is
1923 if (!IsVirtualList() && GetOwner()->GetExpanderColumn() == m_currentCol
)
1925 wxDataViewTreeNode
* node
= GetTreeNodeByRow(m_currentRow
);
1926 indent
= GetOwner()->GetIndent() * node
->GetIndentLevel();
1927 indent
= indent
+ m_lineHeight
;
1929 if(!node
->HasChildren())
1933 wxRect
labelRect( xpos
+ indent
,
1934 GetLineStart( m_currentRow
),
1935 m_currentCol
->GetWidth() - indent
,
1936 GetLineHeight( m_currentRow
) );
1938 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1939 &labelRect
.x
, &labelRect
.y
);
1941 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1942 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1945 //------------------------------------------------------------------
1946 // Helper class for do operation on the tree node
1947 //------------------------------------------------------------------
1952 virtual ~DoJob() { }
1954 //The return value control how the tree-walker tranverse the tree
1955 // 0: Job done, stop tranverse and return
1956 // 1: Ignore the current node's subtree and continue
1957 // 2: Job not done, continue
1958 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1959 virtual int operator() ( wxDataViewTreeNode
* node
) = 0;
1960 virtual int operator() ( void * n
) = 0;
1963 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1968 switch( func( node
) )
1979 wxDataViewTreeNodes nodes
= node
->GetNodes();
1980 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1982 int len_nodes
= nodes
.GetCount();
1983 int len
= leaves
.GetCount();
1984 int i
= 0, nodes_i
= 0;
1986 for(; i
< len
; i
++ )
1988 void * n
= leaves
[i
];
1989 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1991 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1994 if( Walker( nd
, func
) )
2013 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
2024 wxDataViewTreeNode
* node
;
2025 node
= FindNode(parent
);
2030 node
->SetHasChildren( true );
2032 if( g_model
->IsContainer( item
) )
2034 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
2035 newnode
->SetItem(item
);
2036 newnode
->SetHasChildren( true );
2037 node
->AddNode( newnode
);
2040 node
->AddLeaf( item
.GetID() );
2042 node
->ChangeSubTreeCount(1);
2050 static void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2052 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2053 const wxDataViewItem
& item
)
2058 if( m_currentRow
> GetRowCount() )
2059 m_currentRow
= m_count
- 1;
2061 m_selection
.Empty();
2068 wxDataViewTreeNode
* node
= FindNode(parent
);
2070 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2071 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2074 node
->GetChildren().Remove( item
.GetID() );
2075 //Manuplate selection
2076 if( m_selection
.GetCount() > 1 )
2078 m_selection
.Empty();
2080 bool isContainer
= false;
2081 wxDataViewTreeNodes nds
= node
->GetNodes();
2082 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2084 if (nds
[i
]->GetItem() == item
)
2092 wxDataViewTreeNode
* n
= NULL
;
2093 wxDataViewTreeNodes nodes
= node
->GetNodes();
2094 int len
= nodes
.GetCount();
2095 for( int i
= 0; i
< len
; i
++)
2097 if( nodes
[i
]->GetItem() == item
)
2104 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2106 node
->GetNodes().Remove( n
);
2107 sub
-= n
->GetSubTreeCount();
2108 ::DestroyTreeHelper(n
);
2110 //Make the row number invalid and get a new valid one when user call GetRowCount
2112 node
->ChangeSubTreeCount(sub
);
2114 //Change the current row to the last row if the current exceed the max row number
2115 if( m_currentRow
> GetRowCount() )
2116 m_currentRow
= m_count
- 1;
2123 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2129 wxWindow
*parent
= GetParent();
2130 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2131 le
.SetEventObject(parent
);
2132 le
.SetModel(GetOwner()->GetModel());
2134 parent
->GetEventHandler()->ProcessEvent(le
);
2139 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2141 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2142 /*#define MAX_VIRTUAL_WIDTH 100000
2144 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2145 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2146 Refresh( true, &rect );
2154 wxWindow
*parent
= GetParent();
2155 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2156 le
.SetEventObject(parent
);
2157 le
.SetModel(GetOwner()->GetModel());
2160 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2161 parent
->GetEventHandler()->ProcessEvent(le
);
2166 bool wxDataViewMainWindow::Cleared()
2171 BuildTree( GetOwner()->GetModel() );
2178 void wxDataViewMainWindow::UpdateDisplay()
2183 void wxDataViewMainWindow::OnInternalIdle()
2185 wxWindow::OnInternalIdle();
2189 RecalculateDisplay();
2194 void wxDataViewMainWindow::RecalculateDisplay()
2196 wxDataViewModel
*model
= GetOwner()->GetModel();
2203 int width
= GetEndOfLastCol();
2204 int height
= GetLineStart( GetRowCount() );
2206 SetVirtualSize( width
, height
);
2207 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2212 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2214 wxWindow::ScrollWindow( dx
, dy
, rect
);
2216 if (GetOwner()->m_headerArea
)
2217 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2220 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2223 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2224 int sy
= GetLineStart( rows
)/y
;
2228 wxRect rect
= GetClientRect();
2232 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2233 for (x_start
= 0; colnum
< column
; colnum
++)
2235 wxDataViewColumn
*col
= GetOwner()->GetColumnAt(colnum
);
2236 if (col
->IsHidden())
2237 continue; // skip it!
2239 w
= col
->GetWidth();
2243 int x_end
= x_start
+ w
;
2244 xe
= xx
+ rect
.width
;
2247 sx
= ( xx
+ x_end
- xe
)/x
;
2254 m_owner
->Scroll( sx
, sy
);
2257 int wxDataViewMainWindow::GetCountPerPage() const
2259 wxSize size
= GetClientSize();
2260 return size
.y
/ m_lineHeight
;
2263 int wxDataViewMainWindow::GetEndOfLastCol() const
2267 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2269 const wxDataViewColumn
*c
=
2270 const_cast<wxDataViewCtrl
*>(GetOwner())->GetColumnAt( i
);
2273 width
+= c
->GetWidth();
2278 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2282 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2284 return GetLineAt( y
);
2287 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2289 wxSize client_size
= GetClientSize();
2290 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2291 &client_size
.x
, &client_size
.y
);
2293 //we should deal with the pixel here
2294 unsigned int row
= GetLineAt(client_size
.y
) - 1;
2296 return wxMin( GetRowCount()-1, row
);
2299 unsigned int wxDataViewMainWindow::GetRowCount()
2301 if ( m_count
== -1 )
2303 m_count
= RecalculateCount();
2309 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2316 void wxDataViewMainWindow::SelectAllRows( bool on
)
2323 m_selection
.Clear();
2324 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2325 m_selection
.Add( i
);
2330 unsigned int first_visible
= GetFirstVisibleRow();
2331 unsigned int last_visible
= GetLastVisibleRow();
2333 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2335 unsigned int row
= m_selection
[i
];
2336 if ((row
>= first_visible
) && (row
<= last_visible
))
2339 m_selection
.Clear();
2343 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2345 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2349 m_selection
.Add( row
);
2357 m_selection
.Remove( row
);
2363 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2367 unsigned int tmp
= from
;
2373 for (i
= from
; i
<= to
; i
++)
2375 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2378 m_selection
.Add( i
);
2383 m_selection
.Remove( i
);
2386 RefreshRows( from
, to
);
2389 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2391 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2393 int n
= aSelections
[i
];
2395 m_selection
.Add( n
);
2400 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2402 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2403 m_selection
.Add( row
);
2405 m_selection
.Remove( row
);
2409 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2411 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2414 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2416 wxWindow
*parent
= GetParent();
2417 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2419 le
.SetEventObject(parent
);
2420 le
.SetModel(GetOwner()->GetModel());
2423 parent
->GetEventHandler()->ProcessEvent(le
);
2426 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2428 wxRect
rect( 0, GetLineStart( row
), GetEndOfLastCol(), GetLineHeight( row
) );
2429 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2431 wxSize client_size
= GetClientSize();
2432 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2433 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2434 if (intersect_rect
.width
> 0)
2435 Refresh( true, &intersect_rect
);
2438 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2442 unsigned int tmp
= to
;
2447 wxRect
rect( 0, GetLineStart( from
), GetEndOfLastCol(), GetLineStart( (to
-from
+1) ) );
2448 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2450 wxSize client_size
= GetClientSize();
2451 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2452 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2453 if (intersect_rect
.width
> 0)
2454 Refresh( true, &intersect_rect
);
2457 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2459 wxSize client_size
= GetClientSize();
2460 int start
= GetLineStart( firstRow
);
2461 m_owner
->CalcScrolledPosition( start
, 0, &start
, NULL
);
2462 if (start
> client_size
.y
) return;
2464 wxRect
rect( 0, start
, client_size
.x
, client_size
.y
- start
);
2466 Refresh( true, &rect
);
2469 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2471 wxCHECK_RET( newCurrent
< GetRowCount(),
2472 _T("invalid item index in OnArrowChar()") );
2474 // if there is no selection, we cannot move it anywhere
2475 if (!HasCurrentRow())
2478 unsigned int oldCurrent
= m_currentRow
;
2480 // in single selection we just ignore Shift as we can't select several
2482 if ( event
.ShiftDown() && !IsSingleSel() )
2484 RefreshRow( oldCurrent
);
2486 ChangeCurrentRow( newCurrent
);
2488 // select all the items between the old and the new one
2489 if ( oldCurrent
> newCurrent
)
2491 newCurrent
= oldCurrent
;
2492 oldCurrent
= m_currentRow
;
2495 SelectRows( oldCurrent
, newCurrent
, true );
2496 if (oldCurrent
!=newCurrent
)
2497 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2501 RefreshRow( oldCurrent
);
2503 // all previously selected items are unselected unless ctrl is held
2504 if ( !event
.ControlDown() )
2505 SelectAllRows(false);
2507 ChangeCurrentRow( newCurrent
);
2509 if ( !event
.ControlDown() )
2511 SelectRow( m_currentRow
, true );
2512 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2515 RefreshRow( m_currentRow
);
2518 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2521 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2525 rect
.y
= GetLineStart( row
);
2526 rect
.width
= GetEndOfLastCol();
2527 rect
.height
= GetLineHeight( row
);
2532 int wxDataViewMainWindow::GetLineStart( unsigned int row
) const
2534 const wxDataViewModel
*model
= GetOwner()->GetModel();
2536 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2538 // TODO make more efficient
2543 for (r
= 0; r
< row
; r
++)
2545 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(r
);
2546 if (!node
) return start
;
2548 wxDataViewItem item
= node
->GetItem();
2550 if (node
&& !node
->HasChildren())
2552 // Yes, if the node does not have any child, it must be a leaf which
2553 // mean that it is a temporarily created by GetTreeNodeByRow
2557 unsigned int cols
= GetOwner()->GetColumnCount();
2559 int height
= m_lineHeight
;
2560 for (col
= 0; col
< cols
; col
++)
2562 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2563 if (column
->IsHidden())
2564 continue; // skip it!
2566 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2567 continue; // skip it!
2569 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2571 model
->GetValue( value
, item
, column
->GetModelColumn() );
2572 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2573 renderer2
->SetValue( value
);
2574 height
= wxMax( height
, renderer
->GetSize().y
);
2585 return row
* m_lineHeight
;
2589 int wxDataViewMainWindow::GetLineAt( unsigned int y
) const
2591 const wxDataViewModel
*model
= GetOwner()->GetModel();
2593 // check for the easy case first
2594 if ( !GetOwner()->HasFlag(wxDV_VARIABLE_LINE_HEIGHT
) )
2595 return y
/ m_lineHeight
;
2597 // TODO make more efficient
2598 unsigned int row
= 0;
2599 unsigned int yy
= 0;
2602 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2605 // not really correct...
2606 return row
+ ((y
-yy
) / m_lineHeight
);
2609 wxDataViewItem item
= node
->GetItem();
2611 if (node
&& !node
->HasChildren())
2613 // Yes, if the node does not have any child, it must be a leaf which
2614 // mean that it is a temporarily created by GetTreeNodeByRow
2618 unsigned int cols
= GetOwner()->GetColumnCount();
2620 int height
= m_lineHeight
;
2621 for (col
= 0; col
< cols
; col
++)
2623 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2624 if (column
->IsHidden())
2625 continue; // skip it!
2627 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2628 continue; // skip it!
2630 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2632 model
->GetValue( value
, item
, column
->GetModelColumn() );
2633 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2634 renderer2
->SetValue( value
);
2635 height
= wxMax( height
, renderer
->GetSize().y
);
2646 int wxDataViewMainWindow::GetLineHeight( unsigned int row
) const
2648 const wxDataViewModel
*model
= GetOwner()->GetModel();
2650 if (GetOwner()->GetWindowStyle() & wxDV_VARIABLE_LINE_HEIGHT
)
2652 wxASSERT( !IsVirtualList() );
2654 const wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2655 // wxASSERT( node );
2656 if (!node
) return m_lineHeight
;
2658 wxDataViewItem item
= node
->GetItem();
2660 if (node
&& !node
->HasChildren())
2662 // Yes, if the node does not have any child, it must be a leaf which
2663 // mean that it is a temporarily created by GetTreeNodeByRow
2667 int height
= m_lineHeight
;
2669 unsigned int cols
= GetOwner()->GetColumnCount();
2671 for (col
= 0; col
< cols
; col
++)
2673 const wxDataViewColumn
*column
= GetOwner()->GetColumn(col
);
2674 if (column
->IsHidden())
2675 continue; // skip it!
2677 if ((col
!= 0) && model
->IsContainer(item
) && !model
->HasContainerColumns(item
))
2678 continue; // skip it!
2680 const wxDataViewRenderer
*renderer
= column
->GetRenderer();
2682 model
->GetValue( value
, item
, column
->GetModelColumn() );
2683 wxDataViewRenderer
*renderer2
= const_cast<wxDataViewRenderer
*>(renderer
);
2684 renderer2
->SetValue( value
);
2685 height
= wxMax( height
, renderer
->GetSize().y
);
2692 return m_lineHeight
;
2696 class RowToItemJob
: public DoJob
2699 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2700 virtual ~RowToItemJob() { }
2702 virtual int operator() ( wxDataViewTreeNode
* node
)
2705 if( current
== static_cast<int>(row
))
2707 ret
= node
->GetItem();
2711 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2713 current
+= node
->GetSubTreeCount();
2718 //If the current has no child node, we can find the desired item of the row number directly.
2719 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2720 if( node
->GetNodes().GetCount() == 0)
2722 int index
= static_cast<int>(row
) - current
- 1;
2723 ret
= node
->GetChildren().Item( index
);
2730 virtual int operator() ( void * n
)
2733 if( current
== static_cast<int>(row
))
2735 ret
= wxDataViewItem( n
);
2740 wxDataViewItem
GetResult(){ return ret
; }
2747 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2751 return wxDataViewItem( wxUIntToPtr(row
) );
2755 RowToItemJob
job( row
, -2 );
2756 Walker( m_root
, job
);
2757 return job
.GetResult();
2761 class RowToTreeNodeJob
: public DoJob
2764 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2767 this->current
= current
;
2771 virtual ~RowToTreeNodeJob(){ }
2773 virtual int operator() ( wxDataViewTreeNode
* node
)
2776 if( current
== static_cast<int>(row
))
2782 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2784 current
+= node
->GetSubTreeCount();
2790 //If the current has no child node, we can find the desired item of the row number directly.
2791 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2792 if( node
->GetNodes().GetCount() == 0)
2794 int index
= static_cast<int>(row
) - current
- 1;
2795 void * n
= node
->GetChildren().Item( index
);
2796 ret
= new wxDataViewTreeNode( parent
);
2797 ret
->SetItem( wxDataViewItem( n
));
2798 ret
->SetHasChildren(false);
2807 virtual int operator() ( void * n
)
2810 if( current
== static_cast<int>(row
))
2812 ret
= new wxDataViewTreeNode( parent
);
2813 ret
->SetItem( wxDataViewItem( n
));
2814 ret
->SetHasChildren(false);
2820 wxDataViewTreeNode
* GetResult(){ return ret
; }
2824 wxDataViewTreeNode
* ret
;
2825 wxDataViewTreeNode
* parent
;
2829 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
) const
2831 wxASSERT( !IsVirtualList() );
2833 RowToTreeNodeJob
job( row
, -2, m_root
);
2834 Walker( m_root
, job
);
2835 return job
.GetResult();
2838 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2840 wxWindow
*parent
= GetParent();
2841 wxDataViewEvent
le(type
, parent
->GetId());
2843 le
.SetEventObject(parent
);
2844 le
.SetModel(GetOwner()->GetModel());
2847 parent
->GetEventHandler()->ProcessEvent(le
);
2852 bool wxDataViewMainWindow::IsExpanded( unsigned int row
) const
2854 if (IsVirtualList())
2857 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2861 if (!node
->HasChildren())
2867 return node
->IsOpen();
2871 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2873 if (IsVirtualList())
2876 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2879 if( node
->HasChildren())
2881 if( !node
->IsOpen())
2883 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2884 //Check if the user prevent expanding
2885 if( e
.GetSkipped() )
2889 //Here I build the children of current node
2890 if( node
->GetChildrenNumber() == 0 )
2893 ::BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2896 // By expanding the node all row indices that are currently in the selection list
2897 // and are greater than our node have become invalid. So we have to correct that now.
2898 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2899 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2901 const unsigned testRow
= m_selection
[i
];
2902 // all rows above us are not affected, so skip them
2906 m_selection
[i
] += rowAdjustment
;
2909 if(m_currentRow
> row
)
2910 ChangeCurrentRow(m_currentRow
+ rowAdjustment
);
2914 //Send the expanded event
2915 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2919 SelectRow( row
, false );
2920 SelectRow( row
+ 1, true );
2921 ChangeCurrentRow( row
+ 1 );
2922 SendSelectionChangedEvent( GetItemByRow(row
+1));
2930 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2932 if (IsVirtualList())
2935 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2938 wxDataViewTreeNode
* nd
= node
;
2940 if( node
->HasChildren() && node
->IsOpen() )
2942 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2943 if( e
.GetSkipped() )
2946 // Find out if there are selected items below the current node.
2947 bool selectCollapsingRow
= false;
2948 const unsigned rowAdjustment
= node
->GetSubTreeCount();
2949 unsigned maxRowToBeTested
= row
+ rowAdjustment
;
2950 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2952 const unsigned testRow
= m_selection
[i
];
2953 if(testRow
> row
&& testRow
<= maxRowToBeTested
)
2955 selectCollapsingRow
= true;
2956 // get out as soon as we have found a node that is selected
2963 // If the node to be closed has selected items the user won't see those any longer.
2964 // We select the collapsing node in this case.
2965 if(selectCollapsingRow
)
2967 SelectAllRows(false);
2968 ChangeCurrentRow(row
);
2969 SelectRow(row
, true);
2970 SendSelectionChangedEvent(GetItemByRow(row
));
2974 // if there were no selected items below our node we still need to "fix" the
2975 // selection list to adjust for the changing of the row indices.
2976 // We actually do the opposite of what we are doing in OnExpanding().
2977 for(unsigned i
=0; i
<m_selection
.size(); ++i
)
2979 const unsigned testRow
= m_selection
[i
];
2980 // all rows above us are not affected, so skip them
2984 m_selection
[i
] -= rowAdjustment
;
2987 // if the "current row" is being collapsed away we change it to the current row ;-)
2988 if(m_currentRow
> row
&& m_currentRow
<= maxRowToBeTested
)
2989 ChangeCurrentRow(row
);
2990 else if(m_currentRow
> row
)
2991 ChangeCurrentRow(m_currentRow
- rowAdjustment
);
2996 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
3000 node
= node
->GetParent();
3003 int parent
= GetRowByItem( node
->GetItem() );
3006 SelectRow( row
, false);
3007 SelectRow(parent
, true );
3008 ChangeCurrentRow( parent
);
3009 SendSelectionChangedEvent( node
->GetItem() );
3013 if( !nd
->HasChildren())
3018 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
3020 wxDataViewModel
* model
= GetOwner()->GetModel();
3027 //Compose the a parent-chain of the finding item
3029 list
.DeleteContents( true );
3030 wxDataViewItem
it( item
);
3033 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
3034 list
.Insert( pItem
);
3035 it
= model
->GetParent( it
);
3038 //Find the item along the parent-chain.
3039 //This algorithm is designed to speed up the node-finding method
3040 wxDataViewTreeNode
* node
= m_root
;
3041 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end(); iter
++ )
3043 if( node
->HasChildren() )
3045 if( node
->GetChildrenNumber() == 0 )
3048 ::BuildTreeHelper(model
, node
->GetItem(), node
);
3051 wxDataViewTreeNodes nodes
= node
->GetNodes();
3055 for (i
= 0; i
< nodes
.GetCount(); i
++)
3057 if (nodes
[i
]->GetItem() == (**iter
))
3059 if (nodes
[i
]->GetItem() == item
)
3076 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3078 wxDataViewColumn
*col
= NULL
;
3079 unsigned int cols
= GetOwner()->GetColumnCount();
3080 unsigned int colnum
= 0;
3082 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3083 for (unsigned x_start
= 0; colnum
< cols
; colnum
++)
3085 col
= GetOwner()->GetColumnAt(colnum
);
3086 if (col
->IsHidden())
3087 continue; // skip it!
3089 unsigned int w
= col
->GetWidth();
3090 if (x_start
+w
>= (unsigned int)x
)
3097 item
= GetItemByRow( GetLineAt( y
) );
3100 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3102 int row
= GetRowByItem(item
);
3103 int y
= GetLineStart( row
);
3104 int h
= GetLineHeight( m_lineHeight
);
3106 wxDataViewColumn
*col
= NULL
;
3107 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3109 col
= GetOwner()->GetColumnAt( i
);
3110 x
+= col
->GetWidth();
3111 if( GetOwner()->GetColumnAt(i
+1) == column
)
3114 int w
= col
->GetWidth();
3115 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3116 return wxRect(x
, y
, w
, h
);
3119 int wxDataViewMainWindow::RecalculateCount()
3123 wxDataViewIndexListModel
*list_model
= (wxDataViewIndexListModel
*) GetOwner()->GetModel();
3125 return list_model
->GetLastIndex() + 1;
3127 return list_model
->GetLastIndex() - 1;
3132 return m_root
->GetSubTreeCount();
3136 class ItemToRowJob
: public DoJob
3139 ItemToRowJob(const wxDataViewItem
& item_
, ItemList::const_iterator iter
)
3146 //Maybe binary search will help to speed up this process
3147 virtual int operator() ( wxDataViewTreeNode
* node
)
3150 if( node
->GetItem() == item
)
3155 if( node
->GetItem() == **m_iter
)
3162 ret
+= node
->GetSubTreeCount();
3168 virtual int operator() ( void * n
)
3171 if( n
== item
.GetID() )
3175 //the row number is begin from zero
3176 int GetResult() { return ret
-1; }
3179 ItemList::const_iterator m_iter
;
3180 wxDataViewItem item
;
3185 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
) const
3187 const wxDataViewModel
* model
= GetOwner()->GetModel();
3193 return wxPtrToUInt( item
.GetID() );
3200 //Compose the a parent-chain of the finding item
3202 wxDataViewItem
* pItem
;
3203 list
.DeleteContents( true );
3204 wxDataViewItem
it( item
);
3207 pItem
= new wxDataViewItem( it
);
3208 list
.Insert( pItem
);
3209 it
= model
->GetParent( it
);
3211 pItem
= new wxDataViewItem( );
3212 list
.Insert( pItem
);
3214 ItemToRowJob
job( item
, list
.begin() );
3215 Walker(m_root
, job
);
3216 return job
.GetResult();
3220 static void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3222 if( !model
->IsContainer( item
) )
3225 wxDataViewItemArray children
;
3226 unsigned int num
= model
->GetChildren( item
, children
);
3228 unsigned int index
= 0;
3229 while( index
< num
)
3231 if( model
->IsContainer( children
[index
] ) )
3233 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3234 n
->SetItem(children
[index
]);
3235 n
->SetHasChildren( true );
3240 node
->AddLeaf( children
[index
].GetID() );
3244 node
->SetSubTreeCount( num
);
3245 wxDataViewTreeNode
* n
= node
->GetParent();
3247 n
->ChangeSubTreeCount(num
);
3251 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3255 if (GetOwner()->GetModel()->IsVirtualListModel())
3261 m_root
= new wxDataViewTreeNode( NULL
);
3262 m_root
->SetHasChildren(true);
3264 //First we define a invalid item to fetch the top-level elements
3265 wxDataViewItem item
;
3267 BuildTreeHelper( model
, item
, m_root
);
3271 static void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3273 if( node
->GetNodeNumber() != 0 )
3275 int len
= node
->GetNodeNumber();
3277 wxDataViewTreeNodes
& nodes
= node
->GetNodes();
3278 for(; i
< len
; i
++ )
3280 DestroyTreeHelper(nodes
[i
]);
3286 void wxDataViewMainWindow::DestroyTree()
3288 if (!IsVirtualList())
3290 ::DestroyTreeHelper(m_root
);
3296 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3298 if ( GetParent()->HandleAsNavigationKey(event
) )
3301 // no item -> nothing to do
3302 if (!HasCurrentRow())
3308 // don't use m_linesPerPage directly as it might not be computed yet
3309 const int pageSize
= GetCountPerPage();
3310 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3312 switch ( event
.GetKeyCode() )
3316 if (m_currentRow
>= 0)
3318 wxWindow
*parent
= GetParent();
3319 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3320 le
.SetItem( GetItemByRow(m_currentRow
) );
3321 le
.SetEventObject(parent
);
3322 le
.SetModel(GetOwner()->GetModel());
3324 parent
->GetEventHandler()->ProcessEvent(le
);
3329 if ( m_currentRow
> 0 )
3330 OnArrowChar( m_currentRow
- 1, event
);
3334 if ( m_currentRow
< GetRowCount() - 1 )
3335 OnArrowChar( m_currentRow
+ 1, event
);
3337 //Add the process for tree expanding/collapsing
3339 OnCollapsing(m_currentRow
);
3342 OnExpanding( m_currentRow
);
3346 OnArrowChar( GetRowCount() - 1, event
);
3351 OnArrowChar( 0, event
);
3356 int steps
= pageSize
- 1;
3357 int index
= m_currentRow
- steps
;
3361 OnArrowChar( index
, event
);
3367 int steps
= pageSize
- 1;
3368 unsigned int index
= m_currentRow
+ steps
;
3369 unsigned int count
= GetRowCount();
3370 if ( index
>= count
)
3373 OnArrowChar( index
, event
);
3382 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3384 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3386 // let the base handle mouse wheel events.
3391 int x
= event
.GetX();
3392 int y
= event
.GetY();
3393 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3394 wxDataViewColumn
*col
= NULL
;
3397 unsigned int cols
= GetOwner()->GetColumnCount();
3399 for (i
= 0; i
< cols
; i
++)
3401 wxDataViewColumn
*c
= GetOwner()->GetColumnAt( i
);
3403 continue; // skip it!
3405 if (x
< xpos
+ c
->GetWidth())
3410 xpos
+= c
->GetWidth();
3415 wxDataViewRenderer
*cell
= col
->GetRenderer();
3416 unsigned int current
= GetLineAt( y
);
3417 if ((current
>= GetRowCount()) || (x
> GetEndOfLastCol()))
3419 // Unselect all if below the last row ?
3423 //Test whether the mouse is hovered on the tree item button
3424 bool hoverOverExpander
= false;
3425 if ((!IsVirtualList()) && (GetOwner()->GetExpanderColumn() == col
))
3427 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3428 if( node
!=NULL
&& node
->HasChildren() )
3430 int indent
= node
->GetIndentLevel();
3431 indent
= GetOwner()->GetIndent()*indent
;
3433 // we make the rectangle we are looking in a bit bigger than the actual
3434 // visual expander so the user can hit that little thing reliably
3435 wxRect
rect( xpos
+ indent
,
3436 GetLineStart( current
) + (GetLineHeight(current
) - m_lineHeight
)/2,
3437 m_lineHeight
, m_lineHeight
);
3438 if( rect
.Contains(x
, y
) )
3440 //So the mouse is over the expander
3441 hoverOverExpander
= true;
3442 if (m_underMouse
&& m_underMouse
!= node
)
3444 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3445 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3447 if (m_underMouse
!= node
)
3449 //wxLogMessage("Do the row: %d", current);
3450 RefreshRow(current
);
3452 m_underMouse
= node
;
3455 if (node
!=NULL
&& !node
->HasChildren())
3458 if (!hoverOverExpander
)
3460 if (m_underMouse
!= NULL
)
3462 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3463 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3464 m_underMouse
= NULL
;
3468 wxDataViewModel
*model
= GetOwner()->GetModel();
3470 #if wxUSE_DRAG_AND_DROP
3471 if (event
.Dragging())
3473 if (m_dragCount
== 0)
3475 // we have to report the raw, physical coords as we want to be
3476 // able to call HitTest(event.m_pointDrag) from the user code to
3477 // get the item being dragged
3478 m_dragStart
= event
.GetPosition();
3483 if (m_dragCount
!= 3)
3486 if (event
.LeftIsDown())
3488 m_owner
->CalcUnscrolledPosition( m_dragStart
.x
, m_dragStart
.y
, &m_dragStart
.x
, &m_dragStart
.y
);
3489 unsigned int drag_item_row
= GetLineAt( m_dragStart
.y
);
3490 wxDataViewItem item
= GetItemByRow( drag_item_row
);
3492 // Notify cell about drag
3493 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, m_owner
->GetId() );
3494 event
.SetEventObject( m_owner
);
3495 event
.SetItem( item
);
3496 event
.SetModel( model
);
3497 if (!m_owner
->HandleWindowEvent( event
))
3500 if (!event
.IsAllowed())
3503 wxDataObject
*obj
= event
.GetDataObject();
3507 wxDataViewDropSource
drag( this, drag_item_row
);
3508 drag
.SetData( *obj
);
3509 /* wxDragResult res = */ drag
.DoDragDrop();
3518 #endif // wxUSE_DRAG_AND_DROP
3520 bool simulateClick
= false;
3522 if (event
.ButtonDClick())
3524 m_renameTimer
->Stop();
3525 m_lastOnSame
= false;
3528 wxDataViewItem item
= GetItemByRow(current
);
3529 bool ignore_other_columns
=
3530 ((GetOwner()->GetExpanderColumn() != col
) &&
3531 (model
->IsContainer(item
)) &&
3532 (!model
->HasContainerColumns(item
)));
3534 if (event
.LeftDClick())
3536 if(hoverOverExpander
)
3538 // a double click on the expander will be converted into a "simulated" normal click
3539 simulateClick
= true;
3541 else if ( current
== m_lineLastClicked
)
3543 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3546 model
->GetValue( value
, item
, col
->GetModelColumn() );
3547 cell
->SetValue( value
);
3548 wxRect
cell_rect( xpos
, GetLineStart( current
),
3549 col
->GetWidth(), GetLineHeight( current
) );
3550 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3555 wxWindow
*parent
= GetParent();
3556 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3558 le
.SetEventObject(parent
);
3559 le
.SetModel(GetOwner()->GetModel());
3561 parent
->GetEventHandler()->ProcessEvent(le
);
3567 // The first click was on another item, so don't interpret this as
3568 // a double click, but as a simple click instead
3569 simulateClick
= true;
3573 if (event
.LeftUp() && !hoverOverExpander
)
3575 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3577 // select single line
3578 SelectAllRows( false );
3579 SelectRow( m_lineSelectSingleOnUp
, true );
3580 SendSelectionChangedEvent( GetItemByRow(m_lineSelectSingleOnUp
) );
3583 //If the user click the expander, we do not do editing even if the column with expander are editable
3584 if (m_lastOnSame
&& !ignore_other_columns
)
3586 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3587 (cell
->GetMode() & wxDATAVIEW_CELL_EDITABLE
) )
3589 m_renameTimer
->Start( 100, true );
3593 m_lastOnSame
= false;
3594 m_lineSelectSingleOnUp
= (unsigned int)-1;
3596 else if(!event
.LeftUp())
3598 // This is necessary, because after a DnD operation in
3599 // from and to ourself, the up event is swallowed by the
3600 // DnD code. So on next non-up event (which means here and
3601 // now) m_lineSelectSingleOnUp should be reset.
3602 m_lineSelectSingleOnUp
= (unsigned int)-1;
3605 if (event
.RightDown())
3607 m_lineBeforeLastClicked
= m_lineLastClicked
;
3608 m_lineLastClicked
= current
;
3610 // If the item is already selected, do not update the selection.
3611 // Multi-selections should not be cleared if a selected item is clicked.
3612 if (!IsRowSelected(current
))
3614 SelectAllRows(false);
3615 ChangeCurrentRow(current
);
3616 SelectRow(m_currentRow
,true);
3617 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3621 model
->GetValue( value
, item
, col
->GetModelColumn() );
3622 wxWindow
*parent
= GetParent();
3623 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, parent
->GetId());
3625 le
.SetEventObject(parent
);
3626 le
.SetModel(GetOwner()->GetModel());
3628 parent
->GetEventHandler()->ProcessEvent(le
);
3630 else if (event
.MiddleDown())
3634 if((event
.LeftDown() || simulateClick
) && hoverOverExpander
)
3636 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3637 // hoverOverExpander being true tells us that our node must be valid and have children.
3638 // So we don't need any extra checks.
3639 if( node
->IsOpen() )
3640 OnCollapsing(current
);
3642 OnExpanding(current
);
3644 else if ((event
.LeftDown() || simulateClick
) && !hoverOverExpander
)
3648 m_lineBeforeLastClicked
= m_lineLastClicked
;
3649 m_lineLastClicked
= current
;
3651 unsigned int oldCurrentRow
= m_currentRow
;
3652 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3654 bool cmdModifierDown
= event
.CmdDown();
3655 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3657 if ( IsSingleSel() || !IsRowSelected(current
) )
3659 SelectAllRows( false );
3660 ChangeCurrentRow(current
);
3661 SelectRow(m_currentRow
,true);
3662 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3664 else // multi sel & current is highlighted & no mod keys
3666 m_lineSelectSingleOnUp
= current
;
3667 ChangeCurrentRow(current
); // change focus
3670 else // multi sel & either ctrl or shift is down
3672 if (cmdModifierDown
)
3674 ChangeCurrentRow(current
);
3675 ReverseRowSelection(m_currentRow
);
3676 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3678 else if (event
.ShiftDown())
3680 ChangeCurrentRow(current
);
3682 unsigned int lineFrom
= oldCurrentRow
,
3685 if ( lineTo
< lineFrom
)
3688 lineFrom
= m_currentRow
;
3691 SelectRows(lineFrom
, lineTo
, true);
3692 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3694 else // !ctrl, !shift
3696 // test in the enclosing if should make it impossible
3697 wxFAIL_MSG( _T("how did we get here?") );
3701 if (m_currentRow
!= oldCurrentRow
)
3702 RefreshRow( oldCurrentRow
);
3704 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3706 // Update selection here...
3709 m_lastOnSame
= !simulateClick
&& ((col
== oldCurrentCol
) &&
3710 (current
== oldCurrentRow
)) && oldWasSelected
;
3712 // Call LeftClick after everything else as under GTK+
3713 if (cell
->GetMode() & wxDATAVIEW_CELL_ACTIVATABLE
)
3715 // notify cell about right click
3717 model
->GetValue( value
, item
, col
->GetModelColumn() );
3718 cell
->SetValue( value
);
3719 wxRect
cell_rect( xpos
, GetLineStart( current
),
3720 col
->GetWidth(), GetLineHeight( current
) );
3721 /* ignore ret */ cell
->LeftClick( event
.GetPosition(), cell_rect
, model
, item
, col
->GetModelColumn());
3726 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3730 if (HasCurrentRow())
3736 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3740 if (HasCurrentRow())
3746 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3748 if( m_selection
.GetCount() != 1 )
3749 return wxDataViewItem();
3751 return GetItemByRow( m_selection
.Item(0));
3754 //-----------------------------------------------------------------------------
3756 //-----------------------------------------------------------------------------
3757 WX_DEFINE_LIST(wxDataViewColumnList
)
3759 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3761 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3762 EVT_SIZE(wxDataViewCtrl::OnSize
)
3765 wxDataViewCtrl::~wxDataViewCtrl()
3768 GetModel()->RemoveNotifier( m_notifier
);
3773 void wxDataViewCtrl::Init()
3775 m_cols
.DeleteContents(true);
3778 // No sorting column at start
3779 m_sortingColumnIdx
= wxNOT_FOUND
;
3781 m_headerArea
= NULL
;
3784 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3785 const wxPoint
& pos
, const wxSize
& size
,
3786 long style
, const wxValidator
& validator
)
3788 // if ( (style & wxBORDER_MASK) == 0)
3789 // style |= wxBORDER_SUNKEN;
3793 if (!wxControl::Create( parent
, id
, pos
, size
,
3794 style
| wxScrolledWindowStyle
, validator
))
3797 SetInitialSize(size
);
3800 MacSetClipChildren( true );
3803 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3805 if (HasFlag(wxDV_NO_HEADER
))
3806 m_headerArea
= NULL
;
3808 m_headerArea
= new wxDataViewHeaderWindow(this);
3810 SetTargetWindow( m_clientArea
);
3812 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3814 sizer
->Add( m_headerArea
, 0, wxGROW
);
3815 sizer
->Add( m_clientArea
, 1, wxGROW
);
3821 wxBorder
wxDataViewCtrl::GetDefaultBorder() const
3823 return wxBORDER_THEME
;
3827 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3831 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3834 // we need to process arrows ourselves for scrolling
3835 if ( nMsg
== WM_GETDLGCODE
)
3837 rc
|= DLGC_WANTARROWS
;
3845 wxSize
wxDataViewCtrl::GetSizeAvailableForScrollTarget(const wxSize
& size
)
3847 wxSize newsize
= size
;
3848 if (!HasFlag(wxDV_NO_HEADER
) && (m_headerArea
))
3849 newsize
.y
-= m_headerArea
->GetSize().y
;
3854 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3856 // We need to override OnSize so that our scrolled
3857 // window a) does call Layout() to use sizers for
3858 // positioning the controls but b) does not query
3859 // the sizer for their size and use that for setting
3860 // the scrollable area as set that ourselves by
3861 // calling SetScrollbar() further down.
3868 void wxDataViewCtrl::SetFocus()
3871 m_clientArea
->SetFocus();
3874 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3876 if (!wxDataViewCtrlBase::AssociateModel( model
))
3879 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3881 model
->AddNotifier( m_notifier
);
3883 m_clientArea
->DestroyTree();
3885 m_clientArea
->BuildTree(model
);
3887 m_clientArea
->UpdateDisplay();
3892 #if wxUSE_DRAG_AND_DROP
3894 bool wxDataViewCtrl::EnableDragSource( const wxDataFormat
&format
)
3896 return m_clientArea
->EnableDragSource( format
);
3899 bool wxDataViewCtrl::EnableDropTarget( const wxDataFormat
&format
)
3901 return m_clientArea
->EnableDropTarget( format
);
3904 #endif // wxUSE_DRAG_AND_DROP
3906 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3908 if (!wxDataViewCtrlBase::AppendColumn(col
))
3911 m_cols
.Append( col
);
3912 OnColumnsCountChanged();
3916 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3918 if (!wxDataViewCtrlBase::PrependColumn(col
))
3921 m_cols
.Insert( col
);
3922 OnColumnsCountChanged();
3926 bool wxDataViewCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
3928 if (!wxDataViewCtrlBase::InsertColumn(pos
,col
))
3931 m_cols
.Insert( pos
, col
);
3932 OnColumnsCountChanged();
3936 void wxDataViewCtrl::OnColumnChange(unsigned int idx
)
3939 m_headerArea
->UpdateColumn(idx
);
3941 m_clientArea
->UpdateDisplay();
3944 void wxDataViewCtrl::OnColumnsCountChanged()
3947 m_headerArea
->SetColumnCount(GetColumnCount());
3949 m_clientArea
->UpdateDisplay();
3952 void wxDataViewCtrl::DoSetExpanderColumn()
3954 m_clientArea
->UpdateDisplay();
3957 void wxDataViewCtrl::DoSetIndent()
3959 m_clientArea
->UpdateDisplay();
3962 unsigned int wxDataViewCtrl::GetColumnCount() const
3964 return m_cols
.GetCount();
3967 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int idx
) const
3972 wxDataViewColumn
*wxDataViewCtrl::GetColumnAt(unsigned int pos
) const
3974 // columns can't be reordered if there is no header window which allows
3976 const unsigned idx
= m_headerArea
? m_headerArea
->GetColumnsOrder()[pos
]
3979 return GetColumn(idx
);
3982 int wxDataViewCtrl::GetColumnIndex(const wxDataViewColumn
*column
) const
3984 const unsigned count
= m_cols
.size();
3985 for ( unsigned n
= 0; n
< count
; n
++ )
3987 if ( m_cols
[n
] == column
)
3994 void wxDataViewCtrl::ColumnMoved(wxDataViewColumn
* WXUNUSED(col
),
3995 unsigned int WXUNUSED(new_pos
))
3997 // do _not_ reorder m_cols elements here, they should always be in the
3998 // order in which columns were added, we only display the columns in
4000 m_clientArea
->UpdateDisplay();
4003 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
4005 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
4010 OnColumnsCountChanged();
4015 bool wxDataViewCtrl::ClearColumns()
4018 OnColumnsCountChanged();
4022 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
4026 unsigned int len
= GetColumnCount();
4027 for ( unsigned int i
= 0; i
< len
; i
++ )
4029 wxDataViewColumn
* col
= GetColumnAt(i
);
4030 if (col
->IsHidden())
4032 ret
+= col
->GetWidth();
4035 CalcScrolledPosition( ret
, dummy
, &ret
, &dummy
);
4042 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
4044 return m_sortingColumnIdx
== wxNOT_FOUND
? NULL
4045 : GetColumn(m_sortingColumnIdx
);
4048 //Selection code with wxDataViewItem as parameters
4049 wxDataViewItem
wxDataViewCtrl::GetSelection() const
4051 return m_clientArea
->GetSelection();
4054 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
4057 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4058 int len
= selection
.GetCount();
4059 for( int i
= 0; i
< len
; i
++)
4061 unsigned int row
= selection
[i
];
4062 sel
.Add( m_clientArea
->GetItemByRow( row
) );
4067 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
4069 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4071 wxDataViewItem last_parent
;
4073 int len
= sel
.GetCount();
4074 for( int i
= 0; i
< len
; i
++ )
4076 wxDataViewItem item
= sel
[i
];
4077 wxDataViewItem parent
= GetModel()->GetParent( item
);
4080 if (parent
!= last_parent
)
4081 ExpandAncestors(item
);
4084 last_parent
= parent
;
4085 int row
= m_clientArea
->GetRowByItem( item
);
4087 selection
.Add( static_cast<unsigned int>(row
) );
4090 m_clientArea
->SetSelections( selection
);
4093 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
4095 ExpandAncestors( item
);
4097 int row
= m_clientArea
->GetRowByItem( item
);
4100 //Unselect all rows before select another in the single select mode
4101 if (m_clientArea
->IsSingleSel())
4102 m_clientArea
->SelectAllRows(false);
4103 m_clientArea
->SelectRow(row
, true);
4107 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
4109 int row
= m_clientArea
->GetRowByItem( item
);
4111 m_clientArea
->SelectRow(row
, false);
4114 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
4116 int row
= m_clientArea
->GetRowByItem( item
);
4119 return m_clientArea
->IsRowSelected(row
);
4124 //Selection code with row number as parameter
4125 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
4128 wxDataViewSelection selection
= m_clientArea
->GetSelections();
4129 int len
= selection
.GetCount();
4130 for( int i
= 0; i
< len
; i
++)
4132 unsigned int row
= selection
[i
];
4138 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
4140 wxDataViewSelection
selection(wxDataViewSelectionCmp
);
4141 int len
= sel
.GetCount();
4142 for( int i
= 0; i
< len
; i
++ )
4146 selection
.Add( static_cast<unsigned int>(row
) );
4148 m_clientArea
->SetSelections( selection
);
4151 void wxDataViewCtrl::Select( int row
)
4155 if (m_clientArea
->IsSingleSel())
4156 m_clientArea
->SelectAllRows(false);
4157 m_clientArea
->SelectRow( row
, true );
4161 void wxDataViewCtrl::Unselect( int row
)
4164 m_clientArea
->SelectRow(row
, false);
4167 bool wxDataViewCtrl::IsSelected( int row
) const
4170 return m_clientArea
->IsRowSelected(row
);
4174 void wxDataViewCtrl::SelectRange( int from
, int to
)
4177 for( int i
= from
; i
< to
; i
++ )
4179 m_clientArea
->Select(sel
);
4182 void wxDataViewCtrl::UnselectRange( int from
, int to
)
4184 wxDataViewSelection sel
= m_clientArea
->GetSelections();
4185 for( int i
= from
; i
< to
; i
++ )
4186 if( sel
.Index( i
) != wxNOT_FOUND
)
4188 m_clientArea
->SetSelections(sel
);
4191 void wxDataViewCtrl::SelectAll()
4193 m_clientArea
->SelectAllRows(true);
4196 void wxDataViewCtrl::UnselectAll()
4198 m_clientArea
->SelectAllRows(false);
4201 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
4205 if( row
> (int) m_clientArea
->GetRowCount() )
4206 row
= m_clientArea
->GetRowCount();
4208 int first
= m_clientArea
->GetFirstVisibleRow();
4209 int last
= m_clientArea
->GetLastVisibleRow();
4211 m_clientArea
->ScrollTo( row
, column
);
4212 else if( row
> last
)
4213 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
4215 m_clientArea
->ScrollTo( first
, column
);
4218 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
4220 ExpandAncestors( item
);
4222 m_clientArea
->RecalculateDisplay();
4224 int row
= m_clientArea
->GetRowByItem(item
);
4227 if( column
== NULL
)
4228 EnsureVisible(row
, -1);
4230 EnsureVisible( row
, GetColumnIndex(column
) );
4235 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4237 m_clientArea
->HitTest(point
, item
, column
);
4240 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4242 return m_clientArea
->GetItemRect(item
, column
);
4245 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4247 return m_clientArea
->GetItemByRow( row
);
4250 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4252 return m_clientArea
->GetRowByItem( item
);
4255 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4257 int row
= m_clientArea
->GetRowByItem( item
);
4259 m_clientArea
->Expand(row
);
4262 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4264 int row
= m_clientArea
->GetRowByItem( item
);
4266 m_clientArea
->Collapse(row
);
4269 bool wxDataViewCtrl::IsExpanded( const wxDataViewItem
& item
) const
4271 int row
= m_clientArea
->GetRowByItem( item
);
4273 return m_clientArea
->IsExpanded(row
);
4279 // !wxUSE_GENERICDATAVIEWCTRL
4282 // wxUSE_DATAVIEWCTRL