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"
39 #include "wx/stockitem.h"
40 #include "wx/calctrl.h"
41 #include "wx/popupwin.h"
42 #include "wx/renderer.h"
43 #include "wx/dcbuffer.h"
46 #include "wx/listimpl.cpp"
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
54 static const int SCROLL_UNIT_X
= 15;
56 // the cell padding on the left/right
57 static const int PADDING_RIGHTLEFT
= 3;
59 // the expander space margin
60 static const int EXPANDER_MARGIN
= 4;
62 //-----------------------------------------------------------------------------
63 // wxDataViewHeaderWindow
64 //-----------------------------------------------------------------------------
66 #define USE_NATIVE_HEADER_WINDOW 1
68 //Below is the compare stuff
69 //For the generic implements, both the leaf nodes and the nodes are sorted for fast search when needed
70 static wxDataViewModel
* g_model
;
71 static int g_column
= -2;
72 static bool g_asending
= true;
74 // NB: for some reason, this class must be dllexport'ed or we get warnings from
76 class WXDLLIMPEXP_ADV wxDataViewHeaderWindowBase
: public wxControl
79 wxDataViewHeaderWindowBase()
82 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
83 const wxPoint
&pos
, const wxSize
&size
,
86 return wxWindow::Create(parent
, id
, pos
, size
, wxNO_BORDER
, name
);
89 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
90 wxDataViewCtrl
*GetOwner() { return m_owner
; }
92 // called on column addition/removal
93 virtual void UpdateDisplay() { /* by default, do nothing */ }
95 // returns the n-th column
96 virtual wxDataViewColumn
*GetColumn(unsigned int n
)
99 wxDataViewColumn
*ret
= m_owner
->GetColumn(n
);
106 wxDataViewCtrl
*m_owner
;
108 // sends an event generated from the n-th wxDataViewColumn
109 void SendEvent(wxEventType type
, unsigned int n
);
112 // on wxMSW the header window (only that part however) can be made native!
113 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
115 #define COLUMN_WIDTH_OFFSET 2
116 #define wxDataViewHeaderWindowMSW wxDataViewHeaderWindow
118 class wxDataViewHeaderWindowMSW
: public wxDataViewHeaderWindowBase
122 wxDataViewHeaderWindowMSW( wxDataViewCtrl
*parent
,
124 const wxPoint
&pos
= wxDefaultPosition
,
125 const wxSize
&size
= wxDefaultSize
,
126 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
128 Create(parent
, id
, pos
, size
, name
);
131 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
132 const wxPoint
&pos
, const wxSize
&size
,
133 const wxString
&name
);
135 ~wxDataViewHeaderWindowMSW();
137 // called when any column setting is changed and/or changed
139 virtual void UpdateDisplay();
141 // called Refresh afterwards
142 virtual void ScrollWindow(int dx
, int dy
, const wxRect
*rect
= NULL
);
145 virtual bool MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
);
147 virtual void DoSetSize(int x
, int y
, int width
, int height
, int sizeFlags
);
149 wxSize
DoGetBestSize() const;
151 unsigned int GetColumnIdxFromHeader(NMHEADER
*nmHDR
);
153 wxDataViewColumn
*GetColumnFromHeader(NMHEADER
*nmHDR
)
154 { return GetColumn(GetColumnIdxFromHeader(nmHDR
)); }
160 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindowMSW
)
163 #else // !defined(__WXMSW__)
165 #define HEADER_WINDOW_HEIGHT 25
166 #define HEADER_HORIZ_BORDER 5
167 #define HEADER_VERT_BORDER 3
168 #define wxGenericDataViewHeaderWindow wxDataViewHeaderWindow
170 class wxGenericDataViewHeaderWindow
: public wxDataViewHeaderWindowBase
173 wxGenericDataViewHeaderWindow( wxDataViewCtrl
*parent
,
175 const wxPoint
&pos
= wxDefaultPosition
,
176 const wxSize
&size
= wxDefaultSize
,
177 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") )
180 Create(parent
, id
, pos
, size
, name
);
183 bool Create(wxDataViewCtrl
*parent
, wxWindowID id
,
184 const wxPoint
&pos
, const wxSize
&size
,
185 const wxString
&name
);
187 ~wxGenericDataViewHeaderWindow()
189 delete m_resizeCursor
;
192 virtual void UpdateDisplay() { Refresh(); }
196 void OnPaint( wxPaintEvent
&event
);
197 void OnMouse( wxMouseEvent
&event
);
198 void OnSetFocus( wxFocusEvent
&event
);
203 // vars used for column resizing:
205 wxCursor
*m_resizeCursor
;
206 const wxCursor
*m_currentCursor
;
209 bool m_dirty
; // needs refresh?
210 int m_hover
; // index of the column under the mouse
211 int m_column
; // index of the column being resized
212 int m_currentX
; // divider line position in logical (unscrolled) coords
213 int m_minX
; // minimal position beyond which the divider line
214 // can't be dragged in logical coords
216 // the pen used to draw the current column width drag line
217 // when resizing the columsn
221 // internal utilities:
225 m_currentCursor
= (wxCursor
*) NULL
;
226 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
228 m_isDragging
= false;
231 m_hover
= wxNOT_FOUND
;
232 m_column
= wxNOT_FOUND
;
236 wxColour col
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
237 m_penCurrent
= wxPen(col
, 1, wxSOLID
);
240 void AdjustDC(wxDC
& dc
);
243 DECLARE_DYNAMIC_CLASS(wxGenericDataViewHeaderWindow
)
244 DECLARE_EVENT_TABLE()
247 #endif // defined(__WXMSW__)
249 //-----------------------------------------------------------------------------
250 // wxDataViewRenameTimer
251 //-----------------------------------------------------------------------------
253 class wxDataViewRenameTimer
: public wxTimer
256 wxDataViewMainWindow
*m_owner
;
259 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
263 //-----------------------------------------------------------------------------
264 // wxDataViewTreeNode
265 //-----------------------------------------------------------------------------
266 class wxDataViewTreeNode
;
267 WX_DEFINE_ARRAY( wxDataViewTreeNode
*, wxDataViewTreeNodes
);
268 WX_DEFINE_ARRAY( void* , wxDataViewTreeLeaves
);
270 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
);
271 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
);
273 class wxDataViewTreeNode
276 wxDataViewTreeNode( wxDataViewTreeNode
* parent
= NULL
)
283 m_hasChildren
= false;
287 ~wxDataViewTreeNode()
291 wxDataViewTreeNode
* GetParent() { return m_parent
; }
292 void SetParent( wxDataViewTreeNode
* parent
) { m_parent
= parent
; }
293 wxDataViewTreeNodes
& GetNodes() { return m_nodes
; }
294 wxDataViewTreeLeaves
& GetChildren() { return m_leaves
; }
296 void AddNode( wxDataViewTreeNode
* node
)
298 m_leaves
.Add( node
->GetItem().GetID() );
300 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
303 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
305 void AddLeaf( void * leaf
)
307 m_leaves
.Add( leaf
);
309 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
312 wxDataViewItem
& GetItem() { return m_item
; }
313 void SetItem( const wxDataViewItem
& item
) { m_item
= item
; }
315 unsigned int GetChildrenNumber() { return m_leaves
.GetCount(); }
316 unsigned int GetNodeNumber() { return m_nodes
.GetCount(); }
320 wxDataViewTreeNode
* node
= this;
321 while( node
->GetParent()->GetParent() != NULL
)
323 node
= node
->GetParent();
336 int len
= m_nodes
.GetCount();
338 for ( int i
= 0 ;i
< len
; i
++)
339 sum
+= m_nodes
[i
]->GetSubTreeCount();
341 sum
+= m_leaves
.GetCount();
344 ChangeSubTreeCount(-sum
);
350 ChangeSubTreeCount(sum
);
353 bool HasChildren() { return m_hasChildren
; }
354 void SetHasChildren( bool has
){ m_hasChildren
= has
; }
356 void SetSubTreeCount( int num
) { m_subTreeCount
= num
; }
357 int GetSubTreeCount() { return m_subTreeCount
; }
358 void ChangeSubTreeCount( int num
)
362 m_subTreeCount
+= num
;
364 m_parent
->ChangeSubTreeCount(num
);
371 m_nodes
.Sort( &wxGenericTreeModelNodeCmp
);
372 int len
= m_nodes
.GetCount();
373 for (int i
= 0; i
< len
; i
++)
374 m_nodes
[i
]->Resort();
375 m_leaves
.Sort( &wxGenericTreeModelItemCmp
);
380 wxDataViewTreeNode
*m_parent
;
381 wxDataViewTreeNodes m_nodes
;
382 wxDataViewTreeLeaves m_leaves
;
383 wxDataViewItem m_item
;
389 int LINKAGEMODE
wxGenericTreeModelNodeCmp( wxDataViewTreeNode
** node1
, wxDataViewTreeNode
** node2
)
391 return g_model
->Compare( (*node1
)->GetItem(), (*node2
)->GetItem(), g_column
, g_asending
);
394 int LINKAGEMODE
wxGenericTreeModelItemCmp( void ** id1
, void ** id2
)
396 return g_model
->Compare( *id1
, *id2
, g_column
, g_asending
);
401 //-----------------------------------------------------------------------------
402 // wxDataViewMainWindow
403 //-----------------------------------------------------------------------------
405 WX_DEFINE_SORTED_USER_EXPORTED_ARRAY_SIZE_T(unsigned int, wxDataViewSelection
,
407 WX_DECLARE_LIST(wxDataViewItem
, ItemList
);
408 WX_DEFINE_LIST(ItemList
)
410 class wxDataViewMainWindow
: public wxWindow
413 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
415 const wxPoint
&pos
= wxDefaultPosition
,
416 const wxSize
&size
= wxDefaultSize
,
417 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
418 virtual ~wxDataViewMainWindow();
420 // notifications from wxDataViewModel
421 bool ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
422 bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
);
423 bool ItemChanged( const wxDataViewItem
&item
);
424 bool ValueChanged( const wxDataViewItem
&item
, unsigned int col
);
435 g_model
= GetOwner()->GetModel();
436 wxDataViewColumn
* col
= GetOwner()->GetSortingColumn();
439 if (g_model
->HasDefaultCompare())
447 g_column
= col
->GetModelColumn();
448 g_asending
= col
->IsSortOrderAscending();
451 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
452 wxDataViewCtrl
*GetOwner() { return m_owner
; }
453 const wxDataViewCtrl
*GetOwner() const { return m_owner
; }
455 void OnPaint( wxPaintEvent
&event
);
456 void OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
);
457 void OnChar( wxKeyEvent
&event
);
458 void OnMouse( wxMouseEvent
&event
);
459 void OnSetFocus( wxFocusEvent
&event
);
460 void OnKillFocus( wxFocusEvent
&event
);
462 void UpdateDisplay();
463 void RecalculateDisplay();
464 void OnInternalIdle();
466 void OnRenameTimer();
468 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
= NULL
);
469 void ScrollTo( int rows
, int column
);
471 bool HasCurrentRow() { return m_currentRow
!= (unsigned int)-1; }
472 void ChangeCurrentRow( unsigned int row
);
474 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); }
475 bool IsEmpty() { return GetRowCount() == 0; }
477 int GetCountPerPage() const;
478 int GetEndOfLastCol() const;
479 unsigned int GetFirstVisibleRow() const;
480 //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
481 unsigned int GetLastVisibleRow();
482 unsigned int GetRowCount() ;
484 wxDataViewItem
GetSelection() const;
485 wxDataViewSelection
GetSelections(){ return m_selection
; }
486 void SetSelections( const wxDataViewSelection
& sel
) { m_selection
= sel
; UpdateDisplay(); }
487 void Select( const wxArrayInt
& aSelections
);
488 void SelectAllRows( bool on
);
489 void SelectRow( unsigned int row
, bool on
);
490 void SelectRows( unsigned int from
, unsigned int to
, bool on
);
491 void ReverseRowSelection( unsigned int row
);
492 bool IsRowSelected( unsigned int row
);
493 void SendSelectionChangedEvent( const wxDataViewItem
& item
);
495 void RefreshRow( unsigned int row
);
496 void RefreshRows( unsigned int from
, unsigned int to
);
497 void RefreshRowsAfter( unsigned int firstRow
);
499 // returns the colour to be used for drawing the rules
500 wxColour
GetRuleColour() const
502 return wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
);
505 wxRect
GetLineRect( unsigned int row
) const;
507 //Some useful functions for row and item mapping
508 wxDataViewItem
GetItemByRow( unsigned int row
) const;
509 int GetRowByItem( const wxDataViewItem
& item
);
511 //Methods for building the mapping tree
512 void BuildTree( wxDataViewModel
* model
);
514 void HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
);
515 wxRect
GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
);
517 void Expand( unsigned int row
) { OnExpanding( row
); }
518 void Collapse( unsigned int row
) { OnCollapsing( row
); }
520 wxDataViewTreeNode
* GetTreeNodeByRow( unsigned int row
);
521 //We did not need this temporarily
522 //wxDataViewTreeNode * GetTreeNodeByItem( const wxDataViewItem & item );
524 int RecalculateCount() ;
526 wxDataViewEvent
SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
);
527 void OnExpanding( unsigned int row
);
528 void OnCollapsing( unsigned int row
);
530 wxDataViewTreeNode
* FindNode( const wxDataViewItem
& item
);
533 wxDataViewCtrl
*m_owner
;
537 wxDataViewColumn
*m_currentCol
;
538 unsigned int m_currentRow
;
539 wxDataViewSelection m_selection
;
541 wxDataViewRenameTimer
*m_renameTimer
;
549 // for double click logic
550 unsigned int m_lineLastClicked
,
551 m_lineBeforeLastClicked
,
552 m_lineSelectSingleOnUp
;
554 // the pen used to draw horiz/vertical rules
557 // the pen used to draw the expander and the lines
560 //This is the tree structure of the model
561 wxDataViewTreeNode
* m_root
;
563 //This is the tree node under the cursor
564 wxDataViewTreeNode
* m_underMouse
;
566 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
567 DECLARE_EVENT_TABLE()
570 // ---------------------------------------------------------
571 // wxGenericDataViewModelNotifier
572 // ---------------------------------------------------------
574 class wxGenericDataViewModelNotifier
: public wxDataViewModelNotifier
577 wxGenericDataViewModelNotifier( wxDataViewMainWindow
*mainWindow
)
578 { m_mainWindow
= mainWindow
; }
580 virtual bool ItemAdded( const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
581 { return m_mainWindow
->ItemAdded( parent
, item
); }
582 virtual bool ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
583 { return m_mainWindow
->ItemDeleted( parent
, item
); }
584 virtual bool ItemChanged( const wxDataViewItem
& item
)
585 { return m_mainWindow
->ItemChanged(item
); }
586 virtual bool ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
587 { return m_mainWindow
->ValueChanged( item
, col
); }
588 virtual bool Cleared()
589 { return m_mainWindow
->Cleared(); }
590 virtual void Resort()
591 { m_mainWindow
->Resort(); }
593 wxDataViewMainWindow
*m_mainWindow
;
596 // ---------------------------------------------------------
597 // wxDataViewRenderer
598 // ---------------------------------------------------------
600 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer
, wxDataViewRendererBase
)
602 wxDataViewRenderer::wxDataViewRenderer( const wxString
&varianttype
,
603 wxDataViewCellMode mode
,
605 wxDataViewRendererBase( varianttype
, mode
, align
)
612 wxDataViewRenderer::~wxDataViewRenderer()
618 wxDC
*wxDataViewRenderer::GetDC()
622 if (GetOwner() == NULL
)
624 if (GetOwner()->GetOwner() == NULL
)
626 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
632 // ---------------------------------------------------------
633 // wxDataViewCustomRenderer
634 // ---------------------------------------------------------
636 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomRenderer
, wxDataViewRenderer
)
638 wxDataViewCustomRenderer::wxDataViewCustomRenderer( const wxString
&varianttype
,
639 wxDataViewCellMode mode
, int align
) :
640 wxDataViewRenderer( varianttype
, mode
, align
)
644 void wxDataViewCustomRenderer::RenderText( const wxString
&text
, int xoffset
, wxRect cell
, wxDC
*dc
, int state
)
646 wxDataViewCtrl
*view
= GetOwner()->GetOwner();
647 wxColour col
= (state
& wxDATAVIEW_CELL_SELECTED
) ?
648 wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
) :
649 view
->GetForegroundColour();
650 dc
->SetTextForeground(col
);
651 dc
->DrawText( text
, cell
.x
+ xoffset
, cell
.y
+ ((cell
.height
- dc
->GetCharHeight()) / 2));
654 // ---------------------------------------------------------
655 // wxDataViewTextRenderer
656 // ---------------------------------------------------------
658 IMPLEMENT_CLASS(wxDataViewTextRenderer
, wxDataViewCustomRenderer
)
660 wxDataViewTextRenderer::wxDataViewTextRenderer( const wxString
&varianttype
,
661 wxDataViewCellMode mode
, int align
) :
662 wxDataViewCustomRenderer( varianttype
, mode
, align
)
666 bool wxDataViewTextRenderer::SetValue( const wxVariant
&value
)
668 m_text
= value
.GetString();
673 bool wxDataViewTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
678 bool wxDataViewTextRenderer::HasEditorCtrl()
683 wxControl
* wxDataViewTextRenderer::CreateEditorCtrl( wxWindow
*parent
,
684 wxRect labelRect
, const wxVariant
&value
)
686 return new wxTextCtrl( parent
, wxID_ANY
, value
,
687 wxPoint(labelRect
.x
,labelRect
.y
),
688 wxSize(labelRect
.width
,labelRect
.height
) );
691 bool wxDataViewTextRenderer::GetValueFromEditorCtrl( wxControl
*editor
, wxVariant
&value
)
693 wxTextCtrl
*text
= (wxTextCtrl
*) editor
;
694 value
= text
->GetValue();
698 bool wxDataViewTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
700 RenderText( m_text
, 0, cell
, dc
, state
);
704 wxSize
wxDataViewTextRenderer::GetSize() const
706 const wxDataViewCtrl
*view
= GetView();
710 view
->GetTextExtent( m_text
, &x
, &y
);
711 return wxSize( x
, y
);
713 return wxSize(80,20);
716 // ---------------------------------------------------------
717 // wxDataViewBitmapRenderer
718 // ---------------------------------------------------------
720 IMPLEMENT_CLASS(wxDataViewBitmapRenderer
, wxDataViewCustomRenderer
)
722 wxDataViewBitmapRenderer::wxDataViewBitmapRenderer( const wxString
&varianttype
,
723 wxDataViewCellMode mode
, int align
) :
724 wxDataViewCustomRenderer( varianttype
, mode
, align
)
728 bool wxDataViewBitmapRenderer::SetValue( const wxVariant
&value
)
730 if (value
.GetType() == wxT("wxBitmap"))
732 if (value
.GetType() == wxT("wxIcon"))
738 bool wxDataViewBitmapRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
743 bool wxDataViewBitmapRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
746 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
747 else if (m_icon
.Ok())
748 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
753 wxSize
wxDataViewBitmapRenderer::GetSize() const
756 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
757 else if (m_icon
.Ok())
758 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
760 return wxSize(16,16);
763 // ---------------------------------------------------------
764 // wxDataViewToggleRenderer
765 // ---------------------------------------------------------
767 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleRenderer
, wxDataViewCustomRenderer
)
769 wxDataViewToggleRenderer::wxDataViewToggleRenderer( const wxString
&varianttype
,
770 wxDataViewCellMode mode
, int align
) :
771 wxDataViewCustomRenderer( varianttype
, mode
, align
)
776 bool wxDataViewToggleRenderer::SetValue( const wxVariant
&value
)
778 m_toggle
= value
.GetBool();
783 bool wxDataViewToggleRenderer::GetValue( wxVariant
&WXUNUSED(value
) ) const
788 bool wxDataViewToggleRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
790 // User wxRenderer here
793 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
795 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
800 flags
|= wxCONTROL_CHECKED
;
801 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
802 flags
|= wxCONTROL_DISABLED
;
804 wxRendererNative::Get().DrawCheckBox(
805 GetOwner()->GetOwner(),
813 bool wxDataViewToggleRenderer::Activate( wxRect
WXUNUSED(cell
),
814 wxDataViewModel
*model
,
815 const wxDataViewItem
& item
, unsigned int col
)
817 bool value
= !m_toggle
;
818 wxVariant variant
= value
;
819 model
->SetValue( variant
, item
, col
);
820 model
->ValueChanged( item
, col
);
824 wxSize
wxDataViewToggleRenderer::GetSize() const
826 return wxSize(20,20);
829 // ---------------------------------------------------------
830 // wxDataViewProgressRenderer
831 // ---------------------------------------------------------
833 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressRenderer
, wxDataViewCustomRenderer
)
835 wxDataViewProgressRenderer::wxDataViewProgressRenderer( const wxString
&label
,
836 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
837 wxDataViewCustomRenderer( varianttype
, mode
, align
)
843 wxDataViewProgressRenderer::~wxDataViewProgressRenderer()
847 bool wxDataViewProgressRenderer::SetValue( const wxVariant
&value
)
849 m_value
= (long) value
;
851 if (m_value
< 0) m_value
= 0;
852 if (m_value
> 100) m_value
= 100;
857 bool wxDataViewProgressRenderer::GetValue( wxVariant
&value
) const
859 value
= (long) m_value
;
863 bool wxDataViewProgressRenderer::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
865 double pct
= (double)m_value
/ 100.0;
867 bar
.width
= (int)(cell
.width
* pct
);
868 dc
->SetPen( *wxTRANSPARENT_PEN
);
869 dc
->SetBrush( *wxBLUE_BRUSH
);
870 dc
->DrawRectangle( bar
);
872 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
873 dc
->SetPen( *wxBLACK_PEN
);
874 dc
->DrawRectangle( cell
);
879 wxSize
wxDataViewProgressRenderer::GetSize() const
881 return wxSize(40,12);
884 // ---------------------------------------------------------
885 // wxDataViewDateRenderer
886 // ---------------------------------------------------------
888 #define wxUSE_DATE_RENDERER_POPUP (wxUSE_CALENDARCTRL && wxUSE_POPUPWIN)
890 #if wxUSE_DATE_RENDERER_POPUP
892 class wxDataViewDateRendererPopupTransient
: public wxPopupTransientWindow
895 wxDataViewDateRendererPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
896 wxDataViewModel
*model
, const wxDataViewItem
& item
, unsigned int col
) :
897 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
),
902 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
903 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
904 sizer
->Add( m_cal
, 1, wxGROW
);
909 void OnCalendar( wxCalendarEvent
&event
);
911 wxCalendarCtrl
*m_cal
;
912 wxDataViewModel
*m_model
;
914 const wxDataViewItem
& m_item
;
917 virtual void OnDismiss()
922 DECLARE_EVENT_TABLE()
925 BEGIN_EVENT_TABLE(wxDataViewDateRendererPopupTransient
,wxPopupTransientWindow
)
926 EVT_CALENDAR( wxID_ANY
, wxDataViewDateRendererPopupTransient::OnCalendar
)
929 void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent
&event
)
931 wxDateTime date
= event
.GetDate();
932 wxVariant value
= date
;
933 m_model
->SetValue( value
, m_item
, m_col
);
934 m_model
->ValueChanged( m_item
, m_col
);
938 #endif // wxUSE_DATE_RENDERER_POPUP
940 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateRenderer
, wxDataViewCustomRenderer
)
942 wxDataViewDateRenderer::wxDataViewDateRenderer( const wxString
&varianttype
,
943 wxDataViewCellMode mode
, int align
) :
944 wxDataViewCustomRenderer( varianttype
, mode
, align
)
948 bool wxDataViewDateRenderer::SetValue( const wxVariant
&value
)
950 m_date
= value
.GetDateTime();
955 bool wxDataViewDateRenderer::GetValue( wxVariant
&value
) const
961 bool wxDataViewDateRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
963 wxString tmp
= m_date
.FormatDate();
964 RenderText( tmp
, 0, cell
, dc
, state
);
968 wxSize
wxDataViewDateRenderer::GetSize() const
970 const wxDataViewCtrl
* view
= GetView();
971 wxString tmp
= m_date
.FormatDate();
973 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
974 return wxSize(x
,y
+d
);
977 bool wxDataViewDateRenderer::Activate( wxRect
WXUNUSED(cell
), wxDataViewModel
*model
,
978 const wxDataViewItem
& item
, unsigned int col
)
981 model
->GetValue( variant
, item
, col
);
982 wxDateTime value
= variant
.GetDateTime();
984 #if wxUSE_DATE_RENDERER_POPUP
985 wxDataViewDateRendererPopupTransient
*popup
= new wxDataViewDateRendererPopupTransient(
986 GetOwner()->GetOwner()->GetParent(), &value
, model
, item
, col
);
987 wxPoint pos
= wxGetMousePosition();
990 popup
->Popup( popup
->m_cal
);
991 #else // !wxUSE_DATE_RENDERER_POPUP
992 wxMessageBox(value
.Format());
993 #endif // wxUSE_DATE_RENDERER_POPUP/!wxUSE_DATE_RENDERER_POPUP
997 // ---------------------------------------------------------
998 // wxDataViewIconTextRenderer
999 // ---------------------------------------------------------
1001 IMPLEMENT_CLASS(wxDataViewIconTextRenderer
, wxDataViewCustomRenderer
)
1003 wxDataViewIconTextRenderer::wxDataViewIconTextRenderer(
1004 const wxString
&varianttype
, wxDataViewCellMode mode
, int align
) :
1005 wxDataViewCustomRenderer( varianttype
, mode
, align
)
1008 SetAlignment(align
);
1011 wxDataViewIconTextRenderer::~wxDataViewIconTextRenderer()
1015 bool wxDataViewIconTextRenderer::SetValue( const wxVariant
&value
)
1021 bool wxDataViewIconTextRenderer::GetValue( wxVariant
& WXUNUSED(value
) ) const
1026 bool wxDataViewIconTextRenderer::Render( wxRect cell
, wxDC
*dc
, int state
)
1029 const wxIcon
&icon
= m_value
.GetIcon();
1032 dc
->DrawIcon( icon
, cell
.x
, cell
.y
+ ((cell
.height
- icon
.GetHeight()) / 2));
1033 xoffset
= icon
.GetWidth()+4;
1036 RenderText( m_value
.GetText(), xoffset
, cell
, dc
, state
);
1041 wxSize
wxDataViewIconTextRenderer::GetSize() const
1043 const wxDataViewCtrl
*view
= GetView();
1044 if (!m_value
.GetText().empty())
1047 view
->GetTextExtent( m_value
.GetText(), &x
, &y
);
1049 if (m_value
.GetIcon().IsOk())
1050 x
+= m_value
.GetIcon().GetWidth() + 4;
1051 return wxSize( x
, y
);
1053 return wxSize(80,20);
1057 wxDataViewIconTextRenderer::CreateEditorCtrl(wxWindow
* WXUNUSED(parent
),
1058 wxRect
WXUNUSED(labelRect
),
1059 const wxVariant
& WXUNUSED(value
))
1065 wxDataViewIconTextRenderer::GetValueFromEditorCtrl(wxControl
* WXUNUSED(editor
),
1066 wxVariant
& WXUNUSED(value
))
1071 // ---------------------------------------------------------
1073 // ---------------------------------------------------------
1075 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
1077 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewRenderer
*cell
,
1078 unsigned int model_column
,
1079 int width
, wxAlignment align
, int flags
) :
1080 wxDataViewColumnBase( title
, cell
, model_column
, width
, align
, flags
)
1082 SetAlignment(align
);
1086 Init(width
< 0 ? wxDVC_DEFAULT_WIDTH
: width
);
1089 wxDataViewColumn::wxDataViewColumn( const wxBitmap
&bitmap
, wxDataViewRenderer
*cell
,
1090 unsigned int model_column
,
1091 int width
, wxAlignment align
, int flags
) :
1092 wxDataViewColumnBase( bitmap
, cell
, model_column
, width
, align
, flags
)
1094 SetAlignment(align
);
1097 Init(width
< 0 ? wxDVC_TOGGLE_DEFAULT_WIDTH
: width
);
1100 wxDataViewColumn::~wxDataViewColumn()
1104 void wxDataViewColumn::Init( int width
)
1107 m_minWidth
= wxDVC_DEFAULT_MINWIDTH
;
1111 void wxDataViewColumn::SetResizeable( bool resizeable
)
1114 m_flags
|= wxDATAVIEW_COL_RESIZABLE
;
1116 m_flags
&= ~wxDATAVIEW_COL_RESIZABLE
;
1119 void wxDataViewColumn::SetHidden( bool hidden
)
1122 m_flags
|= wxDATAVIEW_COL_HIDDEN
;
1124 m_flags
&= ~wxDATAVIEW_COL_HIDDEN
;
1126 // tell our owner to e.g. update its scrollbars:
1128 GetOwner()->OnColumnChange();
1131 void wxDataViewColumn::SetSortable( bool sortable
)
1134 m_flags
|= wxDATAVIEW_COL_SORTABLE
;
1136 m_flags
&= ~wxDATAVIEW_COL_SORTABLE
;
1138 // Update header button
1140 GetOwner()->OnColumnChange();
1143 void wxDataViewColumn::SetSortOrder( bool ascending
)
1145 m_ascending
= ascending
;
1147 // Update header button
1149 GetOwner()->OnColumnChange();
1152 bool wxDataViewColumn::IsSortOrderAscending() const
1157 void wxDataViewColumn::SetInternalWidth( int width
)
1161 // the scrollbars of the wxDataViewCtrl needs to be recalculated!
1162 if (m_owner
&& m_owner
->m_clientArea
)
1163 m_owner
->m_clientArea
->RecalculateDisplay();
1166 void wxDataViewColumn::SetWidth( int width
)
1168 m_owner
->m_headerArea
->UpdateDisplay();
1170 SetInternalWidth(width
);
1174 //-----------------------------------------------------------------------------
1175 // wxDataViewHeaderWindowBase
1176 //-----------------------------------------------------------------------------
1178 void wxDataViewHeaderWindowBase::SendEvent(wxEventType type
, unsigned int n
)
1180 wxWindow
*parent
= GetParent();
1181 wxDataViewEvent
le(type
, parent
->GetId());
1183 le
.SetEventObject(parent
);
1185 le
.SetDataViewColumn(GetColumn(n
));
1186 le
.SetModel(GetOwner()->GetModel());
1188 // for events created by wxDataViewHeaderWindow the
1189 // row / value fields are not valid
1191 parent
->GetEventHandler()->ProcessEvent(le
);
1194 #if defined(__WXMSW__) && USE_NATIVE_HEADER_WINDOW
1196 // implemented in msw/listctrl.cpp:
1197 int WXDLLIMPEXP_CORE
wxMSWGetColumnClicked(NMHDR
*nmhdr
, POINT
*ptClick
);
1199 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindowMSW
, wxWindow
)
1201 bool wxDataViewHeaderWindowMSW::Create( wxDataViewCtrl
*parent
, wxWindowID id
,
1202 const wxPoint
&pos
, const wxSize
&size
,
1203 const wxString
&name
)
1207 m_scrollOffsetX
= 0;
1208 m_buttonHeight
= wxRendererNative::Get().GetHeaderButtonHeight( this ) + 10;
1210 int x
= pos
.x
== wxDefaultCoord
? 0 : pos
.x
,
1211 y
= pos
.y
== wxDefaultCoord
? 0 : pos
.y
,
1212 w
= size
.x
== wxDefaultCoord
? 1 : size
.x
,
1213 h
= size
.y
== wxDefaultCoord
? m_buttonHeight
: size
.y
;
1215 if ( !CreateControl(parent
, id
, pos
, size
, 0, wxDefaultValidator
, name
) )
1218 // create the native WC_HEADER window:
1219 WXHWND hwndParent
= (HWND
)parent
->GetHandle();
1220 WXDWORD msStyle
= WS_CHILD
| HDS_DRAGDROP
| HDS_BUTTONS
| HDS_HORZ
| HDS_HOTTRACK
| HDS_FULLDRAG
;
1223 msStyle
|= WS_VISIBLE
;
1225 m_hWnd
= CreateWindowEx(0,
1236 wxLogLastError(_T("CreateWindowEx"));
1240 // we need to subclass the m_hWnd to force wxWindow::HandleNotify
1241 // to call wxDataViewHeaderWindow::MSWOnNotify
1242 SubclassWin(m_hWnd
);
1244 // the following is required to get the default win's font for
1245 // header windows and must be done befor sending the HDM_LAYOUT msg
1251 wxDataViewHeaderWindowMSW::~wxDataViewHeaderWindow()
1256 wxSize
wxDataViewHeaderWindowMSW::DoGetBestSize() const
1258 return wxSize(80, m_buttonHeight
);
1261 void wxDataViewHeaderWindowMSW::UpdateDisplay()
1263 // remove old columns
1264 for (int j
=0, max
=Header_GetItemCount((HWND
)m_hWnd
); j
< max
; j
++)
1265 Header_DeleteItem((HWND
)m_hWnd
, 0);
1267 // add the updated array of columns to the header control
1268 unsigned int cols
= GetOwner()->GetColumnCount();
1269 unsigned int added
= 0;
1270 for (unsigned int i
= 0; i
< cols
; i
++)
1272 wxDataViewColumn
*col
= GetColumn( i
);
1273 if (col
->IsHidden())
1274 continue; // don't add it!
1277 hdi
.mask
= HDI_TEXT
| HDI_FORMAT
| HDI_WIDTH
;
1278 hdi
.pszText
= (wxChar
*) col
->GetTitle().wx_str();
1279 hdi
.cxy
= col
->GetWidth();
1280 hdi
.cchTextMax
= sizeof(hdi
.pszText
)/sizeof(hdi
.pszText
[0]);
1281 hdi
.fmt
= HDF_LEFT
| HDF_STRING
;
1282 //hdi.fmt &= ~(HDF_SORTDOWN|HDF_SORTUP);
1284 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1286 //The Microsoft Comctrl32.dll 6.0 support SORTUP/SORTDOWN, but they are not default
1287 //see http://msdn2.microsoft.com/en-us/library/ms649534.aspx for more detail
1288 // VZ: works with 5.81
1289 hdi
.fmt
|= col
->IsSortOrderAscending() ? HDF_SORTUP
: HDF_SORTDOWN
;
1292 // lParam is reserved for application's use:
1293 // we store there the column index to use it later in MSWOnNotify
1294 // (since columns may have been hidden)
1295 hdi
.lParam
= (LPARAM
)i
;
1297 // the native wxMSW implementation of the header window
1298 // draws the column separator COLUMN_WIDTH_OFFSET pixels
1299 // on the right: to correct this effect we make the column
1300 // exactly COLUMN_WIDTH_OFFSET wider (for the first column):
1302 hdi
.cxy
+= COLUMN_WIDTH_OFFSET
;
1304 switch (col
->GetAlignment())
1307 hdi
.fmt
|= HDF_LEFT
;
1309 case wxALIGN_CENTER
:
1310 case wxALIGN_CENTER_HORIZONTAL
:
1311 hdi
.fmt
|= HDF_CENTER
;
1314 hdi
.fmt
|= HDF_RIGHT
;
1318 // such alignment is not allowed for the column header!
1322 SendMessage((HWND
)m_hWnd
, HDM_INSERTITEM
,
1323 (WPARAM
)added
, (LPARAM
)&hdi
);
1328 unsigned int wxDataViewHeaderWindowMSW::GetColumnIdxFromHeader(NMHEADER
*nmHDR
)
1332 // NOTE: we don't just return nmHDR->iItem because when there are
1333 // hidden columns, nmHDR->iItem may be different from
1334 // nmHDR->pitem->lParam
1336 if (nmHDR
->pitem
&& nmHDR
->pitem
->mask
& HDI_LPARAM
)
1338 idx
= (unsigned int)nmHDR
->pitem
->lParam
;
1343 item
.mask
= HDI_LPARAM
;
1344 Header_GetItem((HWND
)m_hWnd
, nmHDR
->iItem
, &item
);
1346 return (unsigned int)item
.lParam
;
1349 bool wxDataViewHeaderWindowMSW::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1351 NMHDR
*nmhdr
= (NMHDR
*)lParam
;
1353 // is it a message from the header?
1354 if ( nmhdr
->hwndFrom
!= (HWND
)m_hWnd
)
1355 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1357 NMHEADER
*nmHDR
= (NMHEADER
*)nmhdr
;
1358 switch ( nmhdr
->code
)
1360 case HDN_BEGINTRACK
:
1361 // user has started to resize a column:
1362 // do we need to veto it?
1363 if (!GetColumn(nmHDR
->iItem
)->IsResizeable())
1371 // user has started to reorder a column
1374 case HDN_ITEMCHANGING
:
1375 if (nmHDR
->pitem
!= NULL
&&
1376 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1378 int minWidth
= GetColumnFromHeader(nmHDR
)->GetMinWidth();
1379 if (nmHDR
->pitem
->cxy
< minWidth
)
1381 // do not allow the user to resize this column under
1382 // its minimal width:
1388 case HDN_ITEMCHANGED
: // user is resizing a column
1389 case HDN_ENDTRACK
: // user has finished resizing a column
1390 case HDN_ENDDRAG
: // user has finished reordering a column
1392 // update the width of the modified column:
1393 if (nmHDR
->pitem
!= NULL
&&
1394 (nmHDR
->pitem
->mask
& HDI_WIDTH
) != 0)
1396 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1397 unsigned int w
= nmHDR
->pitem
->cxy
;
1398 wxDataViewColumn
*col
= GetColumn(idx
);
1400 // see UpdateDisplay() for more info about COLUMN_WIDTH_OFFSET
1401 if (idx
== 0 && w
> COLUMN_WIDTH_OFFSET
)
1402 w
-= COLUMN_WIDTH_OFFSET
;
1404 if (w
>= (unsigned)col
->GetMinWidth())
1405 col
->SetInternalWidth(w
);
1411 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1412 wxDataViewModel
* model
= GetOwner()->GetModel();
1414 if(nmHDR
->iButton
== 0)
1416 wxDataViewColumn
*col
= GetColumn(idx
);
1417 if(col
->IsSortable())
1419 if(model
&& m_owner
->GetSortingColumn() == col
)
1421 bool order
= col
->IsSortOrderAscending();
1422 col
->SetSortOrder(!order
);
1426 m_owner
->SetSortingColumn(col
);
1434 wxEventType evt
= nmHDR
->iButton
== 0 ?
1435 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1436 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1437 SendEvent(evt
, idx
);
1443 // NOTE: for some reason (i.e. for a bug in Windows)
1444 // the HDN_ITEMCLICK notification is not sent on
1445 // right clicks, so we need to handle NM_RCLICK
1448 int column
= wxMSWGetColumnClicked(nmhdr
, &ptClick
);
1449 if (column
!= wxNOT_FOUND
)
1452 item
.mask
= HDI_LPARAM
;
1453 Header_GetItem((HWND
)m_hWnd
, column
, &item
);
1455 // 'idx' may be different from 'column' if there are
1456 // hidden columns...
1457 unsigned int idx
= (unsigned int)item
.lParam
;
1458 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
,
1464 case HDN_GETDISPINFOW
:
1465 // see wxListCtrl::MSWOnNotify for more info!
1468 case HDN_ITEMDBLCLICK
:
1470 unsigned int idx
= GetColumnIdxFromHeader(nmHDR
);
1471 int w
= GetOwner()->GetBestColumnWidth(idx
);
1473 // update the native control:
1475 ZeroMemory(&hd
, sizeof(hd
));
1476 hd
.mask
= HDI_WIDTH
;
1478 Header_SetItem(GetHwnd(),
1479 nmHDR
->iItem
, // NOTE: we don't want 'idx' here!
1482 // update the wxDataViewColumn class:
1483 GetColumn(idx
)->SetInternalWidth(w
);
1488 return wxWindow::MSWOnNotify(idCtrl
, lParam
, result
);
1494 void wxDataViewHeaderWindowMSW::ScrollWindow(int dx
, int WXUNUSED(dy
),
1495 const wxRect
* WXUNUSED(rect
))
1497 m_scrollOffsetX
+= dx
;
1499 GetParent()->Layout();
1502 void wxDataViewHeaderWindowMSW::DoSetSize(int x
, int y
,
1506 // TODO: why is there a border + 2px around it?
1507 wxControl::DoSetSize( x
+m_scrollOffsetX
+1, y
+1, w
-m_scrollOffsetX
-2, h
-2, f
);
1510 #else // !defined(__WXMSW__)
1512 IMPLEMENT_ABSTRACT_CLASS(wxGenericDataViewHeaderWindow
, wxWindow
)
1513 BEGIN_EVENT_TABLE(wxGenericDataViewHeaderWindow
, wxWindow
)
1514 EVT_PAINT (wxGenericDataViewHeaderWindow::OnPaint
)
1515 EVT_MOUSE_EVENTS (wxGenericDataViewHeaderWindow::OnMouse
)
1516 EVT_SET_FOCUS (wxGenericDataViewHeaderWindow::OnSetFocus
)
1519 bool wxGenericDataViewHeaderWindow::Create(wxDataViewCtrl
*parent
, wxWindowID id
,
1520 const wxPoint
&pos
, const wxSize
&size
,
1521 const wxString
&name
)
1525 if (!wxDataViewHeaderWindowBase::Create(parent
, id
, pos
, size
, name
))
1528 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
1529 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1530 SetOwnForegroundColour( attr
.colFg
);
1531 SetOwnBackgroundColour( attr
.colBg
);
1533 SetOwnFont( attr
.font
);
1535 // set our size hints: wxDataViewCtrl will put this wxWindow inside
1536 // a wxBoxSizer and in order to avoid super-big header windows,
1537 // we need to set our height as fixed
1538 SetMinSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1539 SetMaxSize(wxSize(-1, HEADER_WINDOW_HEIGHT
));
1544 void wxGenericDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1547 GetClientSize( &w
, &h
);
1549 wxAutoBufferedPaintDC
dc( this );
1551 dc
.SetBackground(GetBackgroundColour());
1555 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1558 m_owner
->GetViewStart( &x
, NULL
);
1560 // account for the horz scrollbar offset
1561 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1563 dc
.SetFont( GetFont() );
1565 unsigned int cols
= GetOwner()->GetColumnCount();
1568 for (i
= 0; i
< cols
; i
++)
1570 wxDataViewColumn
*col
= GetColumn( i
);
1571 if (col
->IsHidden())
1572 continue; // skip it!
1574 int cw
= col
->GetWidth();
1577 wxHeaderSortIconType sortArrow
= wxHDR_SORT_ICON_NONE
;
1578 if (col
->IsSortable() && GetOwner()->GetSortingColumn() == col
)
1580 if (col
->IsSortOrderAscending())
1581 sortArrow
= wxHDR_SORT_ICON_UP
;
1583 sortArrow
= wxHDR_SORT_ICON_DOWN
;
1587 if (m_parent
->IsEnabled())
1589 if ((int) i
== m_hover
)
1590 state
= wxCONTROL_CURRENT
;
1594 state
= (int) wxCONTROL_DISABLED
;
1597 wxRendererNative::Get().DrawHeaderButton
1601 wxRect(xpos
, 0, cw
, ch
-1),
1606 // align as required the column title:
1608 wxSize titleSz
= dc
.GetTextExtent(col
->GetTitle());
1609 switch (col
->GetAlignment())
1612 x
+= HEADER_HORIZ_BORDER
;
1615 x
+= cw
- titleSz
.GetWidth() - HEADER_HORIZ_BORDER
;
1618 case wxALIGN_CENTER
:
1619 case wxALIGN_CENTER_HORIZONTAL
:
1620 x
+= (cw
- titleSz
.GetWidth() - 2 * HEADER_HORIZ_BORDER
)/2;
1624 // always center the title vertically:
1625 int y
= wxMax((ch
- titleSz
.GetHeight()) / 2, HEADER_VERT_BORDER
);
1627 dc
.SetClippingRegion( xpos
+HEADER_HORIZ_BORDER
,
1629 wxMax(cw
- 2 * HEADER_HORIZ_BORDER
, 1), // width
1630 wxMax(ch
- 2 * HEADER_VERT_BORDER
, 1)); // height
1631 dc
.DrawText( col
->GetTitle(), x
, y
);
1632 dc
.DestroyClippingRegion();
1638 void wxGenericDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
1640 GetParent()->SetFocus();
1644 void wxGenericDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
1646 // we want to work with logical coords
1648 m_owner
->CalcUnscrolledPosition(event
.GetX(), 0, &x
, NULL
);
1649 int y
= event
.GetY();
1653 // we don't draw the line beyond our window,
1654 // but we allow dragging it there
1656 GetClientSize( &w
, NULL
);
1657 m_owner
->CalcUnscrolledPosition(w
, 0, &w
, NULL
);
1660 if (event
.ButtonUp())
1662 m_isDragging
= false;
1668 m_currentX
= wxMax(m_minX
+ 7, x
);
1672 GetColumn(m_column
)->SetWidth(m_currentX
- m_minX
);
1674 GetOwner()->Refresh();
1678 else // not dragging
1681 m_column
= wxNOT_FOUND
;
1683 bool hit_border
= false;
1685 // end of the current column
1688 // find the column where this event occured
1689 int countCol
= m_owner
->GetColumnCount();
1690 for (int column
= 0; column
< countCol
; column
++)
1692 wxDataViewColumn
*p
= GetColumn(column
);
1695 continue; // skip if not shown
1697 xpos
+= p
->GetWidth();
1699 if ((abs(x
-xpos
) < 3) && (y
< 22))
1707 // inside the column
1714 int old_hover
= m_hover
;
1716 if (event
.Leaving())
1717 m_hover
= wxNOT_FOUND
;
1718 if (old_hover
!= m_hover
)
1721 if (m_column
== wxNOT_FOUND
)
1724 bool resizeable
= GetColumn(m_column
)->IsResizeable();
1725 if (event
.LeftDClick() && resizeable
)
1727 GetColumn(m_column
)->SetWidth(GetOwner()->GetBestColumnWidth(m_column
));
1730 else if (event
.LeftDown() || event
.RightUp())
1732 if (hit_border
&& event
.LeftDown() && resizeable
)
1734 m_isDragging
= true;
1738 else // click on a column
1740 wxDataViewModel
* model
= GetOwner()->GetModel();
1741 wxEventType evt
= event
.LeftDown() ?
1742 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
:
1743 wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
;
1744 SendEvent(evt
, m_column
);
1746 //Left click the header
1747 if(event
.LeftDown())
1749 wxDataViewColumn
*col
= GetColumn(m_column
);
1750 if(col
->IsSortable())
1752 wxDataViewColumn
* sortCol
= m_owner
->GetSortingColumn();
1753 if(model
&& sortCol
== col
)
1755 bool order
= col
->IsSortOrderAscending();
1756 col
->SetSortOrder(!order
);
1760 m_owner
->SetSortingColumn(col
);
1766 //Send the column sorted event
1767 SendEvent(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, m_column
);
1771 else if (event
.Moving())
1773 if (hit_border
&& resizeable
)
1774 m_currentCursor
= m_resizeCursor
;
1776 m_currentCursor
= wxSTANDARD_CURSOR
;
1778 SetCursor(*m_currentCursor
);
1783 void wxGenericDataViewHeaderWindow::AdjustDC(wxDC
& dc
)
1787 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
1788 m_owner
->GetViewStart( &x
, NULL
);
1790 // shift the DC origin to match the position of the main window horizontal
1791 // scrollbar: this allows us to always use logical coords
1792 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
1795 #endif // defined(__WXMSW__)
1797 //-----------------------------------------------------------------------------
1798 // wxDataViewRenameTimer
1799 //-----------------------------------------------------------------------------
1801 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
1806 void wxDataViewRenameTimer::Notify()
1808 m_owner
->OnRenameTimer();
1811 //-----------------------------------------------------------------------------
1812 // wxDataViewMainWindow
1813 //-----------------------------------------------------------------------------
1815 //The tree building helper, declared firstly
1816 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
);
1818 int LINKAGEMODE
wxDataViewSelectionCmp( unsigned int row1
, unsigned int row2
)
1820 if (row1
> row2
) return 1;
1821 if (row1
== row2
) return 0;
1826 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
1828 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
1829 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
1830 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
1831 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
1832 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
1833 EVT_CHAR (wxDataViewMainWindow::OnChar
)
1836 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
1837 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
1838 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
|wxBORDER_NONE
, name
),
1839 m_selection( wxDataViewSelectionCmp
)
1844 m_lastOnSame
= false;
1845 m_renameTimer
= new wxDataViewRenameTimer( this );
1847 // TODO: user better initial values/nothing selected
1848 m_currentCol
= NULL
;
1851 m_lineHeight
= wxMax( 17, GetCharHeight() + 2 ); // 17 = mini icon height + 1
1854 m_dragStart
= wxPoint(0,0);
1855 m_lineLastClicked
= (unsigned int) -1;
1856 m_lineBeforeLastClicked
= (unsigned int) -1;
1857 m_lineSelectSingleOnUp
= (unsigned int) -1;
1861 SetBackgroundStyle( wxBG_STYLE_CUSTOM
);
1862 SetBackgroundColour( *wxWHITE
);
1864 m_penRule
= wxPen(GetRuleColour(), 1, wxSOLID
);
1866 //Here I compose a pen can draw black lines, maybe there are something system colour to use
1867 m_penExpander
= wxPen( wxColour(0,0,0), 1, wxSOLID
);
1868 //Some new added code to deal with the tree structure
1869 m_root
= new wxDataViewTreeNode( NULL
);
1870 m_root
->SetHasChildren(true);
1872 //Make m_count = -1 will cause the class recaculate the real displaying number of rows.
1874 m_underMouse
= NULL
;
1878 wxDataViewMainWindow::~wxDataViewMainWindow()
1881 delete m_renameTimer
;
1884 void wxDataViewMainWindow::OnRenameTimer()
1886 // We have to call this here because changes may just have
1887 // been made and no screen update taken place.
1892 unsigned int cols
= GetOwner()->GetColumnCount();
1894 for (i
= 0; i
< cols
; i
++)
1896 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1898 continue; // skip it!
1900 if (c
== m_currentCol
)
1902 xpos
+= c
->GetWidth();
1904 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
,
1905 m_currentCol
->GetWidth(), m_lineHeight
);
1907 GetOwner()->CalcScrolledPosition( labelRect
.x
, labelRect
.y
,
1908 &labelRect
.x
, &labelRect
.y
);
1910 wxDataViewItem item
= GetItemByRow( m_currentRow
);
1911 m_currentCol
->GetRenderer()->StartEditing( item
, labelRect
);
1915 //------------------------------------------------------------------
1916 // Helper class for do operation on the tree node
1917 //------------------------------------------------------------------
1924 //The return value control how the tree-walker tranverse the tree
1925 // 0: Job done, stop tranverse and return
1926 // 1: Ignore the current node's subtree and continue
1927 // 2: Job not done, continue
1928 enum { OK
= 0 , IGR
= 1, CONT
= 2 };
1929 virtual int operator() ( wxDataViewTreeNode
* node
) = 0 ;
1930 virtual int operator() ( void * n
) = 0;
1933 bool Walker( wxDataViewTreeNode
* node
, DoJob
& func
)
1938 switch( func( node
) )
1949 wxDataViewTreeNodes nodes
= node
->GetNodes();
1950 wxDataViewTreeLeaves leaves
= node
->GetChildren();
1952 int len_nodes
= nodes
.GetCount();
1953 int len
= leaves
.GetCount();
1954 int i
= 0, nodes_i
= 0;
1956 for( ; i
< len
; i
++ )
1958 void * n
= leaves
[i
];
1959 if( nodes_i
< len_nodes
&& n
== nodes
[nodes_i
]->GetItem().GetID() )
1961 wxDataViewTreeNode
* nd
= nodes
[nodes_i
];
1964 if( Walker( nd
, func
) )
1983 bool wxDataViewMainWindow::ItemAdded(const wxDataViewItem
& parent
, const wxDataViewItem
& item
)
1987 wxDataViewTreeNode
* node
;
1988 node
= FindNode(parent
);
1993 node
->SetHasChildren( true );
1995 if( g_model
->IsContainer( item
) )
1997 wxDataViewTreeNode
* newnode
= new wxDataViewTreeNode( node
);
1998 newnode
->SetItem(item
);
1999 newnode
->SetHasChildren( true );
2000 node
->AddNode( newnode
);
2003 node
->AddLeaf( item
.GetID() );
2005 node
->ChangeSubTreeCount(1);
2013 void DestroyTreeHelper( wxDataViewTreeNode
* node
);
2015 bool wxDataViewMainWindow::ItemDeleted(const wxDataViewItem
& parent
,
2016 const wxDataViewItem
& item
)
2018 wxDataViewTreeNode
* node
= FindNode(parent
);
2020 wxCHECK_MSG( node
!= NULL
, false, "item not found" );
2021 wxCHECK_MSG( node
->GetChildren().Index( item
.GetID() ) != wxNOT_FOUND
, false, "item not found" );
2024 node
->GetChildren().Remove( item
.GetID() );
2025 //Manuplate selection
2026 if( m_selection
.GetCount() > 1 )
2028 m_selection
.Empty();
2030 bool isContainer
= false;
2031 wxDataViewTreeNodes nds
= node
->GetNodes();
2032 for (size_t i
= 0; i
< nds
.GetCount(); i
++)
2034 if (nds
[i
]->GetItem() == item
)
2042 wxDataViewTreeNode
* n
= NULL
;
2043 wxDataViewTreeNodes nodes
= node
->GetNodes();
2044 int len
= nodes
.GetCount();
2045 for( int i
= 0 ; i
< len
; i
++)
2047 if( nodes
[i
]->GetItem() == item
)
2054 wxCHECK_MSG( n
!= NULL
, false, "item not found" );
2056 node
->GetNodes().Remove( n
);
2057 sub
-= n
->GetSubTreeCount();
2058 DestroyTreeHelper(n
);
2060 //Make the row number invalid and get a new valid one when user call GetRowCount
2062 node
->ChangeSubTreeCount(sub
);
2063 if( node
->GetChildrenNumber() == 0)
2065 node
->GetParent()->GetNodes().Remove( node
);
2069 //Change the current row to the last row if the current exceed the max row number
2070 if( m_currentRow
> GetRowCount() )
2071 m_currentRow
= m_count
- 1;
2078 bool wxDataViewMainWindow::ItemChanged(const wxDataViewItem
& item
)
2084 wxWindow
*parent
= GetParent();
2085 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2086 le
.SetEventObject(parent
);
2087 le
.SetModel(GetOwner()->GetModel());
2089 parent
->GetEventHandler()->ProcessEvent(le
);
2094 bool wxDataViewMainWindow::ValueChanged( const wxDataViewItem
& item
, unsigned int col
)
2096 // NOTE: to be valid, we cannot use e.g. INT_MAX - 1
2097 /*#define MAX_VIRTUAL_WIDTH 100000
2099 wxRect rect( 0, row*m_lineHeight, MAX_VIRTUAL_WIDTH, m_lineHeight );
2100 m_owner->CalcScrolledPosition( rect.x, rect.y, &rect.x, &rect.y );
2101 Refresh( true, &rect );
2109 wxWindow
*parent
= GetParent();
2110 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, parent
->GetId());
2111 le
.SetEventObject(parent
);
2112 le
.SetModel(GetOwner()->GetModel());
2115 le
.SetDataViewColumn(GetOwner()->GetColumn(col
));
2116 parent
->GetEventHandler()->ProcessEvent(le
);
2121 bool wxDataViewMainWindow::Cleared()
2131 void wxDataViewMainWindow::UpdateDisplay()
2136 void wxDataViewMainWindow::OnInternalIdle()
2138 wxWindow::OnInternalIdle();
2142 RecalculateDisplay();
2147 void wxDataViewMainWindow::RecalculateDisplay()
2149 wxDataViewModel
*model
= GetOwner()->GetModel();
2156 int width
= GetEndOfLastCol();
2157 int height
= GetRowCount() * m_lineHeight
;
2159 SetVirtualSize( width
, height
);
2160 GetOwner()->SetScrollRate( 10, m_lineHeight
);
2165 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
2167 wxWindow::ScrollWindow( dx
, dy
, rect
);
2169 if (GetOwner()->m_headerArea
)
2170 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
2173 void wxDataViewMainWindow::ScrollTo( int rows
, int column
)
2176 m_owner
->GetScrollPixelsPerUnit( &x
, &y
);
2177 int sy
= rows
*m_lineHeight
/y
;
2181 wxRect rect
= GetClientRect();
2183 int x_start
= 0, x_end
= 0, w
= 0;
2185 m_owner
->CalcUnscrolledPosition( rect
.x
, rect
.y
, &xx
, &yy
);
2186 for (x_start
= 0; colnum
< column
; colnum
++)
2188 wxDataViewColumn
*col
= GetOwner()->GetColumn(colnum
);
2189 if (col
->IsHidden())
2190 continue; // skip it!
2192 w
= col
->GetWidth();
2196 x_end
= x_start
+ w
;
2197 xe
= xx
+ rect
.width
;
2200 sx
= ( xx
+ x_end
- xe
)/x
;
2207 m_owner
->Scroll( sx
, sy
);
2210 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
2212 wxDataViewModel
*model
= GetOwner()->GetModel();
2213 wxAutoBufferedPaintDC
dc( this );
2216 dc
.SetBackground(GetBackgroundColour());
2218 GetOwner()->PrepareDC( dc
);
2219 dc
.SetFont( GetFont() );
2221 wxRect update
= GetUpdateRegion().GetBox();
2222 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
2224 // compute which items needs to be redrawn
2225 unsigned int item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
2226 unsigned int item_count
=
2227 wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
2228 (int)(GetRowCount( )- item_start
) );
2229 unsigned int item_last
= item_start
+ item_count
;
2231 // compute which columns needs to be redrawn
2232 unsigned int cols
= GetOwner()->GetColumnCount();
2233 unsigned int col_start
= 0;
2234 unsigned int x_start
= 0;
2235 for (x_start
= 0; col_start
< cols
; col_start
++)
2237 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_start
);
2238 if (col
->IsHidden())
2239 continue; // skip it!
2241 unsigned int w
= col
->GetWidth();
2242 if (x_start
+w
>= (unsigned int)update
.x
)
2248 unsigned int col_last
= col_start
;
2249 unsigned int x_last
= x_start
;
2250 for (; col_last
< cols
; col_last
++)
2252 wxDataViewColumn
*col
= GetOwner()->GetColumn(col_last
);
2253 if (col
->IsHidden())
2254 continue; // skip it!
2256 if (x_last
> (unsigned int)update
.GetRight())
2259 x_last
+= col
->GetWidth();
2262 // Draw horizontal rules if required
2263 if ( m_owner
->HasFlag(wxDV_HORIZ_RULES
) )
2265 dc
.SetPen(m_penRule
);
2266 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2268 for (unsigned int i
= item_start
; i
<= item_last
+1; i
++)
2270 int y
= i
* m_lineHeight
;
2271 dc
.DrawLine(x_start
, y
, x_last
, y
);
2275 // Draw vertical rules if required
2276 if ( m_owner
->HasFlag(wxDV_VERT_RULES
) )
2278 dc
.SetPen(m_penRule
);
2279 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
2282 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2284 wxDataViewColumn
*col
= GetOwner()->GetColumn(i
);
2285 if (col
->IsHidden())
2286 continue; // skip it
2288 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2289 x
, item_last
* m_lineHeight
);
2291 x
+= col
->GetWidth();
2294 // Draw last vertical rule
2295 dc
.DrawLine(x
, item_start
* m_lineHeight
,
2296 x
, item_last
* m_lineHeight
);
2299 // redraw the background for the items which are selected/current
2300 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2302 bool selected
= m_selection
.Index( item
) != wxNOT_FOUND
;
2303 if (selected
|| item
== m_currentRow
)
2305 int flags
= selected
? (int)wxCONTROL_SELECTED
: 0;
2306 if (item
== m_currentRow
)
2307 flags
|= wxCONTROL_CURRENT
;
2309 flags
|= wxCONTROL_FOCUSED
;
2311 wxRect
rect( x_start
, item
*m_lineHeight
, x_last
, m_lineHeight
);
2312 wxRendererNative::Get().DrawItemSelectionRect
2322 wxDataViewColumn
*expander
= GetOwner()->GetExpanderColumn();
2325 // TODO: last column for RTL support
2326 expander
= GetOwner()->GetColumn( 0 );
2327 GetOwner()->SetExpanderColumn(expander
);
2330 // redraw all cells for all rows which must be repainted and for all columns
2332 cell_rect
.x
= x_start
;
2333 cell_rect
.height
= m_lineHeight
; // -1 is for the horizontal rules
2334 for (unsigned int i
= col_start
; i
< col_last
; i
++)
2336 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
2337 wxDataViewRenderer
*cell
= col
->GetRenderer();
2338 cell_rect
.width
= col
->GetWidth();
2340 if (col
->IsHidden())
2341 continue; // skipt it!
2344 for (unsigned int item
= item_start
; item
< item_last
; item
++)
2346 // get the cell value and set it into the renderer
2348 wxDataViewTreeNode
* node
= GetTreeNodeByRow(item
);
2352 wxDataViewItem dataitem
= node
->GetItem();
2354 if ((i
> 0) && model
->IsContainer(dataitem
) && !model
->HasContainerColumns(dataitem
))
2357 model
->GetValue( value
, dataitem
, col
->GetModelColumn());
2358 cell
->SetValue( value
);
2360 // update the y offset
2361 cell_rect
.y
= item
* m_lineHeight
;
2363 //Draw the expander here.
2364 int indent
= node
->GetIndentLevel();
2365 if( col
== expander
)
2367 //Calculate the indent first
2368 indent
= cell_rect
.x
+ GetOwner()->GetIndent() * indent
;
2370 int expander_width
= m_lineHeight
- 2*EXPANDER_MARGIN
;
2371 // change the cell_rect.x to the appropriate pos
2372 int expander_x
= indent
+ EXPANDER_MARGIN
, expander_y
= cell_rect
.y
+ EXPANDER_MARGIN
;
2373 indent
= indent
+ m_lineHeight
; //try to use the m_lineHeight as the expander space
2374 dc
.SetPen( m_penExpander
);
2375 dc
.SetBrush( wxNullBrush
);
2376 if( node
->HasChildren() )
2378 wxRect
rect( expander_x
, expander_y
, expander_width
, expander_width
);
2380 if (m_underMouse
== node
)
2382 flag
|= wxCONTROL_CURRENT
;
2384 if( node
->IsOpen() )
2385 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
|wxCONTROL_EXPANDED
);
2387 wxRendererNative::Get().DrawTreeItemButton( this, dc
, rect
, flag
);
2391 // I am wandering whether we should draw dot lines between tree nodes
2393 //Yes, if the node does not have any child, it must be a leaf which mean that it is a temporarily created by GetTreeNodeByRow
2396 //force the expander column to left-center align
2397 cell
->SetAlignment( wxALIGN_CENTER_VERTICAL
);
2401 // cannot be bigger than allocated space
2402 wxSize size
= cell
->GetSize();
2403 // Because of the tree structure indent, here we should minus the width of the cell for drawing
2404 size
.x
= wxMin( size
.x
+ 2*PADDING_RIGHTLEFT
, cell_rect
.width
- indent
);
2405 // size.y = wxMin( size.y, cell_rect.height );
2406 size
.y
= cell_rect
.height
;
2408 wxRect
item_rect(cell_rect
.GetTopLeft(), size
);
2409 int align
= cell
->GetAlignment();
2411 // horizontal alignment:
2412 item_rect
.x
= cell_rect
.x
;
2413 if (align
& wxALIGN_CENTER_HORIZONTAL
)
2414 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
2415 else if (align
& wxALIGN_RIGHT
)
2416 item_rect
.x
= cell_rect
.x
+ cell_rect
.width
- size
.x
;
2417 //else: wxALIGN_LEFT is the default
2419 // vertical alignment:
2420 item_rect
.y
= cell_rect
.y
;
2421 if (align
& wxALIGN_CENTER_VERTICAL
)
2422 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
2423 else if (align
& wxALIGN_BOTTOM
)
2424 item_rect
.y
= cell_rect
.y
+ cell_rect
.height
- size
.y
;
2425 //else: wxALIGN_TOP is the default
2428 item_rect
.x
+= PADDING_RIGHTLEFT
;
2429 item_rect
.width
= size
.x
- 2 * PADDING_RIGHTLEFT
;
2431 //Here we add the tree indent
2432 item_rect
.x
+= indent
;
2435 if (m_hasFocus
&& (m_selection
.Index(item
) != wxNOT_FOUND
))
2436 state
|= wxDATAVIEW_CELL_SELECTED
;
2438 // TODO: it would be much more efficient to create a clipping
2439 // region for the entire column being rendered (in the OnPaint
2440 // of wxDataViewMainWindow) instead of a single clip region for
2441 // each cell. However it would mean that each renderer should
2442 // respect the given wxRect's top & bottom coords, eventually
2443 // violating only the left & right coords - however the user can
2444 // make its own renderer and thus we cannot be sure of that.
2445 dc
.SetClippingRegion( item_rect
);
2446 cell
->Render( item_rect
, &dc
, state
);
2447 dc
.DestroyClippingRegion();
2450 cell_rect
.x
+= cell_rect
.width
;
2454 int wxDataViewMainWindow::GetCountPerPage() const
2456 wxSize size
= GetClientSize();
2457 return size
.y
/ m_lineHeight
;
2460 int wxDataViewMainWindow::GetEndOfLastCol() const
2464 for (i
= 0; i
< GetOwner()->GetColumnCount(); i
++)
2466 const wxDataViewColumn
*c
=
2467 wx_const_cast(wxDataViewCtrl
*, GetOwner())->GetColumn( i
);
2470 width
+= c
->GetWidth();
2475 unsigned int wxDataViewMainWindow::GetFirstVisibleRow() const
2479 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
2481 return y
/ m_lineHeight
;
2484 unsigned int wxDataViewMainWindow::GetLastVisibleRow()
2486 wxSize client_size
= GetClientSize();
2487 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
,
2488 &client_size
.x
, &client_size
.y
);
2490 //we should deal with the pixel here
2491 unsigned int row
= ((client_size
.y
)/m_lineHeight
) - 1;
2493 return wxMin( GetRowCount()-1, row
);
2496 unsigned int wxDataViewMainWindow::GetRowCount()
2498 if ( m_count
== -1 )
2500 m_count
= RecalculateCount();
2502 GetVirtualSize( &width
, &height
);
2503 height
= m_count
* m_lineHeight
;
2505 SetVirtualSize( width
, height
);
2510 void wxDataViewMainWindow::ChangeCurrentRow( unsigned int row
)
2517 void wxDataViewMainWindow::SelectAllRows( bool on
)
2524 m_selection
.Clear();
2525 for (unsigned int i
= 0; i
< GetRowCount(); i
++)
2526 m_selection
.Add( i
);
2531 unsigned int first_visible
= GetFirstVisibleRow();
2532 unsigned int last_visible
= GetLastVisibleRow();
2534 for (i
= 0; i
< m_selection
.GetCount(); i
++)
2536 unsigned int row
= m_selection
[i
];
2537 if ((row
>= first_visible
) && (row
<= last_visible
))
2540 m_selection
.Clear();
2544 void wxDataViewMainWindow::SelectRow( unsigned int row
, bool on
)
2546 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2550 m_selection
.Add( row
);
2558 m_selection
.Remove( row
);
2564 void wxDataViewMainWindow::SelectRows( unsigned int from
, unsigned int to
, bool on
)
2568 unsigned int tmp
= from
;
2574 for (i
= from
; i
<= to
; i
++)
2576 if (m_selection
.Index( i
) == wxNOT_FOUND
)
2579 m_selection
.Add( i
);
2584 m_selection
.Remove( i
);
2587 RefreshRows( from
, to
);
2590 void wxDataViewMainWindow::Select( const wxArrayInt
& aSelections
)
2592 for (size_t i
=0; i
< aSelections
.GetCount(); i
++)
2594 int n
= aSelections
[i
];
2596 m_selection
.Add( n
);
2601 void wxDataViewMainWindow::ReverseRowSelection( unsigned int row
)
2603 if (m_selection
.Index( row
) == wxNOT_FOUND
)
2604 m_selection
.Add( row
);
2606 m_selection
.Remove( row
);
2610 bool wxDataViewMainWindow::IsRowSelected( unsigned int row
)
2612 return (m_selection
.Index( row
) != wxNOT_FOUND
);
2615 void wxDataViewMainWindow::SendSelectionChangedEvent( const wxDataViewItem
& item
)
2617 wxWindow
*parent
= GetParent();
2618 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, parent
->GetId());
2620 le
.SetEventObject(parent
);
2621 le
.SetModel(GetOwner()->GetModel());
2624 parent
->GetEventHandler()->ProcessEvent(le
);
2627 void wxDataViewMainWindow::RefreshRow( unsigned int row
)
2629 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
2630 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2632 wxSize client_size
= GetClientSize();
2633 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2634 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2635 if (intersect_rect
.width
> 0)
2636 Refresh( true, &intersect_rect
);
2639 void wxDataViewMainWindow::RefreshRows( unsigned int from
, unsigned int to
)
2643 unsigned int tmp
= to
;
2648 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
2649 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2651 wxSize client_size
= GetClientSize();
2652 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2653 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2654 if (intersect_rect
.width
> 0)
2655 Refresh( true, &intersect_rect
);
2658 void wxDataViewMainWindow::RefreshRowsAfter( unsigned int firstRow
)
2660 unsigned int count
= GetRowCount();
2661 if (firstRow
> count
)
2664 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
2665 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
2667 wxSize client_size
= GetClientSize();
2668 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
2669 wxRect intersect_rect
= client_rect
.Intersect( rect
);
2670 if (intersect_rect
.width
> 0)
2671 Refresh( true, &intersect_rect
);
2674 void wxDataViewMainWindow::OnArrowChar(unsigned int newCurrent
, const wxKeyEvent
& event
)
2676 wxCHECK_RET( newCurrent
< GetRowCount(),
2677 _T("invalid item index in OnArrowChar()") );
2679 // if there is no selection, we cannot move it anywhere
2680 if (!HasCurrentRow())
2683 unsigned int oldCurrent
= m_currentRow
;
2685 // in single selection we just ignore Shift as we can't select several
2687 if ( event
.ShiftDown() && !IsSingleSel() )
2689 RefreshRow( oldCurrent
);
2691 ChangeCurrentRow( newCurrent
);
2693 // select all the items between the old and the new one
2694 if ( oldCurrent
> newCurrent
)
2696 newCurrent
= oldCurrent
;
2697 oldCurrent
= m_currentRow
;
2700 SelectRows( oldCurrent
, newCurrent
, true );
2701 if (oldCurrent
!=newCurrent
)
2702 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]));
2706 RefreshRow( oldCurrent
);
2708 // all previously selected items are unselected unless ctrl is held
2709 if ( !event
.ControlDown() )
2710 SelectAllRows(false);
2712 ChangeCurrentRow( newCurrent
);
2714 if ( !event
.ControlDown() )
2716 SelectRow( m_currentRow
, true );
2717 SendSelectionChangedEvent(GetItemByRow(m_currentRow
));
2720 RefreshRow( m_currentRow
);
2723 GetOwner()->EnsureVisible( m_currentRow
, -1 );
2726 wxRect
wxDataViewMainWindow::GetLineRect( unsigned int row
) const
2730 rect
.y
= m_lineHeight
* row
;
2731 rect
.width
= GetEndOfLastCol();
2732 rect
.height
= m_lineHeight
;
2737 class RowToItemJob
: public DoJob
2740 RowToItemJob( unsigned int row
, int current
) { this->row
= row
; this->current
= current
;}
2741 virtual ~RowToItemJob(){};
2743 virtual int operator() ( wxDataViewTreeNode
* node
)
2746 if( current
== static_cast<int>(row
))
2748 ret
= node
->GetItem() ;
2752 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2754 current
+= node
->GetSubTreeCount();
2759 //If the current has no child node, we can find the desired item of the row number directly.
2760 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2761 if( node
->GetNodes().GetCount() == 0)
2763 int index
= static_cast<int>(row
) - current
- 1;
2764 ret
= node
->GetChildren().Item( index
);
2771 virtual int operator() ( void * n
)
2774 if( current
== static_cast<int>(row
))
2776 ret
= wxDataViewItem( n
) ;
2781 wxDataViewItem
GetResult(){ return ret
; }
2788 wxDataViewItem
wxDataViewMainWindow::GetItemByRow(unsigned int row
) const
2790 RowToItemJob
job( row
, -2 );
2791 Walker( m_root
, job
);
2792 return job
.GetResult();
2795 class RowToTreeNodeJob
: public DoJob
2798 RowToTreeNodeJob( unsigned int row
, int current
, wxDataViewTreeNode
* node
)
2801 this->current
= current
;
2805 virtual ~RowToTreeNodeJob(){};
2807 virtual int operator() ( wxDataViewTreeNode
* node
)
2810 if( current
== static_cast<int>(row
))
2816 if( node
->GetSubTreeCount() + current
< static_cast<int>(row
) )
2818 current
+= node
->GetSubTreeCount();
2824 //If the current has no child node, we can find the desired item of the row number directly.
2825 //This if can speed up finding in some case, and will has a very good effect when it comes to list view
2826 if( node
->GetNodes().GetCount() == 0)
2828 int index
= static_cast<int>(row
) - current
- 1;
2829 void * n
= node
->GetChildren().Item( index
);
2830 ret
= new wxDataViewTreeNode( parent
) ;
2831 ret
->SetItem( wxDataViewItem( n
));
2832 ret
->SetHasChildren(false);
2841 virtual int operator() ( void * n
)
2844 if( current
== static_cast<int>(row
))
2846 ret
= new wxDataViewTreeNode( parent
) ;
2847 ret
->SetItem( wxDataViewItem( n
));
2848 ret
->SetHasChildren(false);
2854 wxDataViewTreeNode
* GetResult(){ return ret
; }
2858 wxDataViewTreeNode
* ret
;
2859 wxDataViewTreeNode
* parent
;
2863 wxDataViewTreeNode
* wxDataViewMainWindow::GetTreeNodeByRow(unsigned int row
)
2865 RowToTreeNodeJob
job( row
, -2, m_root
);
2866 Walker( m_root
, job
);
2867 return job
.GetResult();
2870 wxDataViewEvent
wxDataViewMainWindow::SendExpanderEvent( wxEventType type
, const wxDataViewItem
& item
)
2872 wxWindow
*parent
= GetParent();
2873 wxDataViewEvent
le(type
, parent
->GetId());
2875 le
.SetEventObject(parent
);
2876 le
.SetModel(GetOwner()->GetModel());
2879 parent
->GetEventHandler()->ProcessEvent(le
);
2883 void wxDataViewMainWindow::OnExpanding( unsigned int row
)
2885 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2888 if( node
->HasChildren())
2890 if( !node
->IsOpen())
2892 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
,node
->GetItem());
2893 //Check if the user prevent expanding
2894 if( e
.GetSkipped() )
2898 //Here I build the children of current node
2899 if( node
->GetChildrenNumber() == 0 )
2902 BuildTreeHelper(GetOwner()->GetModel(), node
->GetItem(), node
);
2906 //Send the expanded event
2907 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
,node
->GetItem());
2911 SelectRow( row
, false );
2912 SelectRow( row
+ 1, true );
2913 ChangeCurrentRow( row
+ 1 );
2914 SendSelectionChangedEvent( GetItemByRow(row
+1));
2922 void wxDataViewMainWindow::OnCollapsing(unsigned int row
)
2924 wxDataViewTreeNode
* node
= GetTreeNodeByRow(row
);
2927 wxDataViewTreeNode
* nd
= node
;
2929 if( node
->HasChildren() && node
->IsOpen() )
2931 wxDataViewEvent e
= SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
,node
->GetItem());
2932 if( e
.GetSkipped() )
2937 SendExpanderEvent(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
,nd
->GetItem());
2941 node
= node
->GetParent();
2944 int parent
= GetRowByItem( node
->GetItem() ) ;
2947 SelectRow( row
, false);
2948 SelectRow(parent
, true );
2949 ChangeCurrentRow( parent
);
2950 SendSelectionChangedEvent( node
->GetItem() );
2954 if( !nd
->HasChildren())
2959 wxDataViewTreeNode
* wxDataViewMainWindow::FindNode( const wxDataViewItem
& item
)
2961 wxDataViewModel
* model
= GetOwner()->GetModel();
2965 //Compose the a parent-chain of the finding item
2967 list
.DeleteContents( true );
2968 wxDataViewItem
it( item
);
2971 wxDataViewItem
* pItem
= new wxDataViewItem( it
);
2972 list
.Insert( pItem
);
2973 it
= model
->GetParent( it
);
2976 //Find the item along the parent-chain.
2977 //This algorithm is designed to speed up the node-finding method
2978 wxDataViewTreeNode
* node
= m_root
;
2979 for( ItemList::const_iterator iter
= list
.begin(); iter
!=list
.end() ; iter
++ )
2981 if( node
->HasChildren() )
2983 if( node
->GetChildrenNumber() == 0 )
2986 BuildTreeHelper(model
, node
->GetItem(), node
);
2989 wxDataViewTreeNodes nodes
= node
->GetNodes();
2991 for (; i
< nodes
.GetCount(); i
++)
2993 if (nodes
[i
]->GetItem() == (**iter
))
2999 if (i
== nodes
.GetCount())
3008 void wxDataViewMainWindow::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
)
3010 wxDataViewColumn
*col
= NULL
;
3011 unsigned int cols
= GetOwner()->GetColumnCount();
3012 unsigned int colnum
= 0;
3013 unsigned int x_start
= 0;
3015 m_owner
->CalcUnscrolledPosition( point
.x
, point
.y
, &x
, &y
);
3016 for (x_start
= 0; colnum
< cols
; colnum
++)
3018 col
= GetOwner()->GetColumn(colnum
);
3019 if (col
->IsHidden())
3020 continue; // skip it!
3022 unsigned int w
= col
->GetWidth();
3023 if (x_start
+w
>= (unsigned int)x
)
3030 item
= GetItemByRow( y
/m_lineHeight
);
3033 wxRect
wxDataViewMainWindow::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3035 int row
= GetRowByItem(item
);
3036 int y
= row
*m_lineHeight
;
3037 int h
= m_lineHeight
;
3039 wxDataViewColumn
*col
= NULL
;
3040 for( int i
= 0, cols
= GetOwner()->GetColumnCount(); i
< cols
; i
++ )
3042 col
= GetOwner()->GetColumn( i
);
3043 x
+= col
->GetWidth();
3044 if( GetOwner()->GetColumn(i
+1) == column
)
3047 int w
= col
->GetWidth();
3048 m_owner
->CalcScrolledPosition( x
, y
, &x
, &y
);
3049 return wxRect(x
, y
, w
, h
);
3052 int wxDataViewMainWindow::RecalculateCount()
3054 return m_root
->GetSubTreeCount();
3057 class ItemToRowJob
: public DoJob
3060 ItemToRowJob(const wxDataViewItem
& item
, ItemList::const_iterator iter
)
3061 { this->item
= item
; ret
= -1 ; m_iter
= iter
; }
3062 virtual ~ItemToRowJob(){};
3064 //Maybe binary search will help to speed up this process
3065 virtual int operator() ( wxDataViewTreeNode
* node
)
3068 if( node
->GetItem() == item
)
3073 if( node
->GetItem() == **m_iter
)
3080 ret
+= node
->GetSubTreeCount();
3086 virtual int operator() ( void * n
)
3089 if( n
== item
.GetID() )
3093 //the row number is begin from zero
3094 int GetResult(){ return ret
-1 ; }
3096 ItemList::const_iterator m_iter
;
3097 wxDataViewItem item
;
3102 int wxDataViewMainWindow::GetRowByItem(const wxDataViewItem
& item
)
3104 wxDataViewModel
* model
= GetOwner()->GetModel();
3111 //Compose the a parent-chain of the finding item
3113 wxDataViewItem
* pItem
= NULL
;
3114 list
.DeleteContents( true );
3115 wxDataViewItem
it( item
);
3118 pItem
= new wxDataViewItem( it
);
3119 list
.Insert( pItem
);
3120 it
= model
->GetParent( it
);
3122 pItem
= new wxDataViewItem( );
3123 list
.Insert( pItem
);
3125 ItemToRowJob
job( item
, list
.begin() );
3126 Walker(m_root
, job
);
3127 return job
.GetResult();
3130 void BuildTreeHelper( wxDataViewModel
* model
, wxDataViewItem
& item
, wxDataViewTreeNode
* node
)
3132 if( !model
->IsContainer( item
) )
3135 wxDataViewItemArray children
;
3136 unsigned int num
= model
->GetChildren( item
, children
);
3137 unsigned int index
= 0;
3138 while( index
< num
)
3140 if( model
->IsContainer( children
[index
] ) )
3142 wxDataViewTreeNode
* n
= new wxDataViewTreeNode( node
);
3143 n
->SetItem(children
[index
]);
3144 n
->SetHasChildren( true ) ;
3149 node
->AddLeaf( children
[index
].GetID() );
3153 node
->SetSubTreeCount( num
);
3154 wxDataViewTreeNode
* n
= node
->GetParent();
3156 n
->ChangeSubTreeCount(num
);
3160 void wxDataViewMainWindow::BuildTree(wxDataViewModel
* model
)
3162 //First we define a invalid item to fetch the top-level elements
3163 wxDataViewItem item
;
3165 BuildTreeHelper( model
, item
, m_root
);
3169 void DestroyTreeHelper( wxDataViewTreeNode
* node
)
3171 if( node
->GetNodeNumber() != 0 )
3173 int len
= node
->GetNodeNumber();
3175 wxDataViewTreeNodes nodes
= node
->GetNodes();
3176 for( ; i
< len
; i
++ )
3178 DestroyTreeHelper(nodes
[i
]);
3184 void wxDataViewMainWindow::DestroyTree()
3186 DestroyTreeHelper(m_root
);
3187 m_root
->SetSubTreeCount(0);
3191 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
3193 if (event
.GetKeyCode() == WXK_TAB
)
3195 wxNavigationKeyEvent nevent
;
3196 nevent
.SetWindowChange( event
.ControlDown() );
3197 nevent
.SetDirection( !event
.ShiftDown() );
3198 nevent
.SetEventObject( GetParent()->GetParent() );
3199 nevent
.SetCurrentFocus( m_parent
);
3200 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
3204 // no item -> nothing to do
3205 if (!HasCurrentRow())
3211 // don't use m_linesPerPage directly as it might not be computed yet
3212 const int pageSize
= GetCountPerPage();
3213 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
3215 switch ( event
.GetKeyCode() )
3219 if (m_currentRow
> 0)
3221 wxWindow
*parent
= GetParent();
3222 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3223 le
.SetItem( GetItemByRow(m_currentRow
) );
3224 le
.SetEventObject(parent
);
3225 le
.SetModel(GetOwner()->GetModel());
3227 parent
->GetEventHandler()->ProcessEvent(le
);
3232 if ( m_currentRow
> 0 )
3233 OnArrowChar( m_currentRow
- 1, event
);
3237 if ( m_currentRow
< GetRowCount() - 1 )
3238 OnArrowChar( m_currentRow
+ 1, event
);
3240 //Add the process for tree expanding/collapsing
3242 OnCollapsing(m_currentRow
);
3245 OnExpanding( m_currentRow
);
3249 OnArrowChar( GetRowCount() - 1, event
);
3254 OnArrowChar( 0, event
);
3259 int steps
= pageSize
- 1;
3260 int index
= m_currentRow
- steps
;
3264 OnArrowChar( index
, event
);
3270 int steps
= pageSize
- 1;
3271 unsigned int index
= m_currentRow
+ steps
;
3272 unsigned int count
= GetRowCount();
3273 if ( index
>= count
)
3276 OnArrowChar( index
, event
);
3285 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
3287 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
3289 // let the base handle mouse wheel events.
3294 int x
= event
.GetX();
3295 int y
= event
.GetY();
3296 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
3297 wxDataViewColumn
*col
= NULL
;
3300 unsigned int cols
= GetOwner()->GetColumnCount();
3302 for (i
= 0; i
< cols
; i
++)
3304 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
3306 continue; // skip it!
3308 if (x
< xpos
+ c
->GetWidth())
3313 xpos
+= c
->GetWidth();
3318 wxDataViewRenderer
*cell
= col
->GetRenderer();
3319 unsigned int current
= y
/ m_lineHeight
;
3320 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
3322 // Unselect all if below the last row ?
3326 //Test whether the mouse is hovered on the tree item button
3328 if (GetOwner()->GetExpanderColumn() == col
)
3330 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3331 if( node
!=NULL
&& node
->HasChildren() )
3333 int indent
= node
->GetIndentLevel();
3334 indent
= GetOwner()->GetIndent()*indent
;
3335 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3336 if( rect
.Contains( x
, y
) )
3338 //So the mouse is over the expander
3340 if (m_underMouse
&& m_underMouse
!= node
)
3342 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3343 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3345 if (m_underMouse
!= node
)
3347 //wxLogMessage("Do the row: %d", current);
3348 RefreshRow(current
);
3350 m_underMouse
= node
;
3353 if (node
!=NULL
&& !node
->HasChildren())
3358 if (m_underMouse
!= NULL
)
3360 //wxLogMessage("Undo the row: %d", GetRowByItem(m_underMouse->GetItem()));
3361 RefreshRow(GetRowByItem(m_underMouse
->GetItem()));
3362 m_underMouse
= NULL
;
3366 wxDataViewModel
*model
= GetOwner()->GetModel();
3368 if (event
.Dragging())
3370 if (m_dragCount
== 0)
3372 // we have to report the raw, physical coords as we want to be
3373 // able to call HitTest(event.m_pointDrag) from the user code to
3374 // get the item being dragged
3375 m_dragStart
= event
.GetPosition();
3380 if (m_dragCount
!= 3)
3383 if (event
.LeftIsDown())
3385 // Notify cell about drag
3394 bool forceClick
= false;
3396 if (event
.ButtonDClick())
3398 m_renameTimer
->Stop();
3399 m_lastOnSame
= false;
3402 wxDataViewItem item
= GetItemByRow(current
);
3403 bool ignore_other_columns
=
3404 ((GetOwner()->GetExpanderColumn() != col
) &&
3405 (model
->IsContainer(item
)) &&
3406 (!model
->HasContainerColumns(item
)));
3408 if (event
.LeftDClick())
3410 if ( current
== m_lineLastClicked
)
3412 if ((!ignore_other_columns
) && (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
))
3415 model
->GetValue( value
, item
, col
->GetModelColumn() );
3416 cell
->SetValue( value
);
3417 wxRect
cell_rect( xpos
, current
* m_lineHeight
,
3418 col
->GetWidth(), m_lineHeight
);
3419 cell
->Activate( cell_rect
, model
, item
, col
->GetModelColumn() );
3424 wxWindow
*parent
= GetParent();
3425 wxDataViewEvent
le(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, parent
->GetId());
3427 le
.SetEventObject(parent
);
3428 le
.SetModel(GetOwner()->GetModel());
3430 parent
->GetEventHandler()->ProcessEvent(le
);
3436 // The first click was on another item, so don't interpret this as
3437 // a double click, but as a simple click instead
3444 if (m_lineSelectSingleOnUp
!= (unsigned int)-1)
3446 // select single line
3447 SelectAllRows( false );
3448 SelectRow( m_lineSelectSingleOnUp
, true );
3451 //Process the event of user clicking the expander
3452 bool expander
= false;
3453 if (GetOwner()->GetExpanderColumn() == col
)
3455 wxDataViewTreeNode
* node
= GetTreeNodeByRow(current
);
3456 if( node
!=NULL
&& node
->HasChildren() )
3458 int indent
= node
->GetIndentLevel();
3459 indent
= GetOwner()->GetIndent()*indent
;
3460 wxRect
rect( xpos
+ indent
+ EXPANDER_MARGIN
, current
* m_lineHeight
+ EXPANDER_MARGIN
, m_lineHeight
-2*EXPANDER_MARGIN
,m_lineHeight
-2*EXPANDER_MARGIN
);
3461 if( rect
.Contains( x
, y
) )
3464 if( node
->IsOpen() )
3465 OnCollapsing(current
);
3467 OnExpanding( current
);
3471 //If the user click the expander, we do not do editing even if the column with expander are editable
3472 if (m_lastOnSame
&& !expander
&& !ignore_other_columns
)
3474 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
3475 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
3477 m_renameTimer
->Start( 100, true );
3481 m_lastOnSame
= false;
3482 m_lineSelectSingleOnUp
= (unsigned int)-1;
3486 // This is necessary, because after a DnD operation in
3487 // from and to ourself, the up event is swallowed by the
3488 // DnD code. So on next non-up event (which means here and
3489 // now) m_lineSelectSingleOnUp should be reset.
3490 m_lineSelectSingleOnUp
= (unsigned int)-1;
3493 if (event
.RightDown())
3495 m_lineBeforeLastClicked
= m_lineLastClicked
;
3496 m_lineLastClicked
= current
;
3498 // If the item is already selected, do not update the selection.
3499 // Multi-selections should not be cleared if a selected item is clicked.
3500 if (!IsRowSelected(current
))
3502 SelectAllRows(false);
3503 ChangeCurrentRow(current
);
3504 SelectRow(m_currentRow
,true);
3505 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3508 // notify cell about right click
3511 // Allow generation of context menu event
3514 else if (event
.MiddleDown())
3516 // notify cell about middle click
3519 if (event
.LeftDown() || forceClick
)
3523 m_lineBeforeLastClicked
= m_lineLastClicked
;
3524 m_lineLastClicked
= current
;
3526 unsigned int oldCurrentRow
= m_currentRow
;
3527 bool oldWasSelected
= IsRowSelected(m_currentRow
);
3529 bool cmdModifierDown
= event
.CmdDown();
3530 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
3532 if ( IsSingleSel() || !IsRowSelected(current
) )
3534 SelectAllRows( false );
3535 ChangeCurrentRow(current
);
3536 SelectRow(m_currentRow
,true);
3537 SendSelectionChangedEvent(GetItemByRow( m_currentRow
) );
3539 else // multi sel & current is highlighted & no mod keys
3541 m_lineSelectSingleOnUp
= current
;
3542 ChangeCurrentRow(current
); // change focus
3545 else // multi sel & either ctrl or shift is down
3547 if (cmdModifierDown
)
3549 ChangeCurrentRow(current
);
3550 ReverseRowSelection(m_currentRow
);
3551 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3553 else if (event
.ShiftDown())
3555 ChangeCurrentRow(current
);
3557 unsigned int lineFrom
= oldCurrentRow
,
3560 if ( lineTo
< lineFrom
)
3563 lineFrom
= m_currentRow
;
3566 SelectRows(lineFrom
, lineTo
, true);
3567 SendSelectionChangedEvent(GetItemByRow(m_selection
[0]) );
3569 else // !ctrl, !shift
3571 // test in the enclosing if should make it impossible
3572 wxFAIL_MSG( _T("how did we get here?") );
3576 if (m_currentRow
!= oldCurrentRow
)
3577 RefreshRow( oldCurrentRow
);
3579 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
3581 // Update selection here...
3584 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) &&
3585 (current
== oldCurrentRow
)) && oldWasSelected
;
3589 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
3593 if (HasCurrentRow())
3599 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
3603 if (HasCurrentRow())
3609 wxDataViewItem
wxDataViewMainWindow::GetSelection() const
3611 if( m_selection
.GetCount() != 1 )
3612 return wxDataViewItem();
3614 return GetItemByRow( m_selection
.Item(0));
3617 //-----------------------------------------------------------------------------
3619 //-----------------------------------------------------------------------------
3620 WX_DEFINE_LIST(wxDataViewColumnList
)
3622 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
3624 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
3625 EVT_SIZE(wxDataViewCtrl::OnSize
)
3628 wxDataViewCtrl::~wxDataViewCtrl()
3631 GetModel()->RemoveNotifier( m_notifier
);
3634 void wxDataViewCtrl::Init()
3639 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
3640 const wxPoint
& pos
, const wxSize
& size
,
3641 long style
, const wxValidator
& validator
)
3643 if (!wxControl::Create( parent
, id
, pos
, size
,
3644 style
| wxScrolledWindowStyle
|wxBORDER_SUNKEN
, validator
))
3647 SetInitialSize(size
);
3652 MacSetClipChildren( true ) ;
3655 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
3657 if (HasFlag(wxDV_NO_HEADER
))
3658 m_headerArea
= NULL
;
3660 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
);
3662 SetTargetWindow( m_clientArea
);
3664 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
3666 sizer
->Add( m_headerArea
, 0, wxGROW
);
3667 sizer
->Add( m_clientArea
, 1, wxGROW
);
3674 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
3678 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
3681 // we need to process arrows ourselves for scrolling
3682 if ( nMsg
== WM_GETDLGCODE
)
3684 rc
|= DLGC_WANTARROWS
;
3692 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
3694 // We need to override OnSize so that our scrolled
3695 // window a) does call Layout() to use sizers for
3696 // positioning the controls but b) does not query
3697 // the sizer for their size and use that for setting
3698 // the scrollable area as set that ourselves by
3699 // calling SetScrollbar() further down.
3706 bool wxDataViewCtrl::AssociateModel( wxDataViewModel
*model
)
3708 if (!wxDataViewCtrlBase::AssociateModel( model
))
3711 m_notifier
= new wxGenericDataViewModelNotifier( m_clientArea
);
3713 model
->AddNotifier( m_notifier
);
3715 m_clientArea
->BuildTree(model
);
3717 m_clientArea
->UpdateDisplay();
3722 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
3724 if (!wxDataViewCtrlBase::AppendColumn(col
))
3727 m_cols
.Append( col
);
3732 bool wxDataViewCtrl::PrependColumn( wxDataViewColumn
*col
)
3734 if (!wxDataViewCtrlBase::PrependColumn(col
))
3737 m_cols
.Insert( col
);
3742 void wxDataViewCtrl::OnColumnChange()
3745 m_headerArea
->UpdateDisplay();
3747 m_clientArea
->UpdateDisplay();
3750 void wxDataViewCtrl::DoSetExpanderColumn()
3752 m_clientArea
->UpdateDisplay();
3755 void wxDataViewCtrl::DoSetIndent()
3757 m_clientArea
->UpdateDisplay();
3760 unsigned int wxDataViewCtrl::GetColumnCount() const
3762 return m_cols
.GetCount();
3765 wxDataViewColumn
* wxDataViewCtrl::GetColumn( unsigned int pos
) const
3767 wxDataViewColumnList::const_iterator iter
;
3769 for (iter
= m_cols
.begin(); iter
!=m_cols
.end(); iter
++)
3774 if ((*iter
)->IsHidden())
3781 bool wxDataViewCtrl::DeleteColumn( wxDataViewColumn
*column
)
3783 wxDataViewColumnList::compatibility_iterator ret
= m_cols
.Find( column
);
3794 bool wxDataViewCtrl::ClearColumns()
3801 int wxDataViewCtrl::GetColumnPosition( const wxDataViewColumn
*column
) const
3803 int ret
= 0, dead
= 0;
3804 int len
= GetColumnCount();
3805 for (int i
=0; i
<len
; i
++)
3807 wxDataViewColumn
* col
= GetColumn(i
);
3808 if (col
->IsHidden())
3810 ret
+= col
->GetWidth();
3813 CalcScrolledPosition( ret
, dead
, &ret
, &dead
);
3820 wxDataViewColumn
*wxDataViewCtrl::GetSortingColumn() const
3825 //Selection code with wxDataViewItem as parameters
3826 wxDataViewItem
wxDataViewCtrl::GetSelection() const
3828 return m_clientArea
->GetSelection();
3831 int wxDataViewCtrl::GetSelections( wxDataViewItemArray
& sel
) const
3834 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3835 int len
= selection
.GetCount();
3836 for( int i
= 0; i
< len
; i
++)
3838 unsigned int row
= selection
[i
];
3839 sel
.Add( m_clientArea
->GetItemByRow( row
) );
3844 void wxDataViewCtrl::SetSelections( const wxDataViewItemArray
& sel
)
3846 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3847 int len
= sel
.GetCount();
3848 for( int i
= 0; i
< len
; i
++ )
3850 int row
= m_clientArea
->GetRowByItem( sel
[i
] );
3852 selection
.Add( static_cast<unsigned int>(row
) );
3854 m_clientArea
->SetSelections( selection
);
3857 void wxDataViewCtrl::Select( const wxDataViewItem
& item
)
3859 int row
= m_clientArea
->GetRowByItem( item
);
3862 //Unselect all rows before select another in the single select mode
3863 if (m_clientArea
->IsSingleSel())
3864 m_clientArea
->SelectAllRows(false);
3865 m_clientArea
->SelectRow(row
, true);
3869 void wxDataViewCtrl::Unselect( const wxDataViewItem
& item
)
3871 int row
= m_clientArea
->GetRowByItem( item
);
3873 m_clientArea
->SelectRow(row
, false);
3876 bool wxDataViewCtrl::IsSelected( const wxDataViewItem
& item
) const
3878 int row
= m_clientArea
->GetRowByItem( item
);
3881 return m_clientArea
->IsRowSelected(row
);
3886 //Selection code with row number as parameter
3887 int wxDataViewCtrl::GetSelections( wxArrayInt
& sel
) const
3890 wxDataViewSelection selection
= m_clientArea
->GetSelections();
3891 int len
= selection
.GetCount();
3892 for( int i
= 0; i
< len
; i
++)
3894 unsigned int row
= selection
[i
];
3900 void wxDataViewCtrl::SetSelections( const wxArrayInt
& sel
)
3902 wxDataViewSelection
selection(wxDataViewSelectionCmp
) ;
3903 int len
= sel
.GetCount();
3904 for( int i
= 0; i
< len
; i
++ )
3908 selection
.Add( static_cast<unsigned int>(row
) );
3910 m_clientArea
->SetSelections( selection
);
3913 void wxDataViewCtrl::Select( int row
)
3917 if (m_clientArea
->IsSingleSel())
3918 m_clientArea
->SelectAllRows(false);
3919 m_clientArea
->SelectRow( row
, true );
3923 void wxDataViewCtrl::Unselect( int row
)
3926 m_clientArea
->SelectRow(row
, false);
3929 bool wxDataViewCtrl::IsSelected( int row
) const
3932 return m_clientArea
->IsRowSelected(row
);
3936 void wxDataViewCtrl::SelectRange( int from
, int to
)
3939 for( int i
= from
; i
< to
; i
++ )
3941 m_clientArea
->Select(sel
);
3944 void wxDataViewCtrl::UnselectRange( int from
, int to
)
3946 wxDataViewSelection sel
= m_clientArea
->GetSelections();
3947 for( int i
= from
; i
< to
; i
++ )
3948 if( sel
.Index( i
) != wxNOT_FOUND
)
3950 m_clientArea
->SetSelections(sel
);
3953 void wxDataViewCtrl::SelectAll()
3955 m_clientArea
->SelectAllRows(true);
3958 void wxDataViewCtrl::UnselectAll()
3960 m_clientArea
->SelectAllRows(false);
3963 void wxDataViewCtrl::EnsureVisible( int row
, int column
)
3967 if( row
> (int) m_clientArea
->GetRowCount() )
3968 row
= m_clientArea
->GetRowCount();
3970 int first
= m_clientArea
->GetFirstVisibleRow();
3971 int last
= m_clientArea
->GetLastVisibleRow();
3973 m_clientArea
->ScrollTo( row
, column
);
3974 else if( row
> last
)
3975 m_clientArea
->ScrollTo( row
- last
+ first
, column
);
3977 m_clientArea
->ScrollTo( first
, column
);
3980 void wxDataViewCtrl::EnsureVisible( const wxDataViewItem
& item
, const wxDataViewColumn
* column
)
3982 int row
= m_clientArea
->GetRowByItem(item
);
3985 if( column
== NULL
)
3986 EnsureVisible(row
, -1);
3990 int len
= GetColumnCount();
3991 for( int i
= 0; i
< len
; i
++ )
3992 if( GetColumn(i
) == column
)
3997 EnsureVisible( row
, col
);
4003 void wxDataViewCtrl::HitTest( const wxPoint
& point
, wxDataViewItem
& item
, wxDataViewColumn
* &column
) const
4005 m_clientArea
->HitTest(point
, item
, column
);
4008 wxRect
wxDataViewCtrl::GetItemRect( const wxDataViewItem
& item
, const wxDataViewColumn
* column
) const
4010 return m_clientArea
->GetItemRect(item
, column
);
4013 wxDataViewItem
wxDataViewCtrl::GetItemByRow( unsigned int row
) const
4015 return m_clientArea
->GetItemByRow( row
);
4018 int wxDataViewCtrl::GetRowByItem( const wxDataViewItem
& item
) const
4020 return m_clientArea
->GetRowByItem( item
);
4023 void wxDataViewCtrl::Expand( const wxDataViewItem
& item
)
4025 int row
= m_clientArea
->GetRowByItem( item
);
4027 m_clientArea
->Expand(row
);
4030 void wxDataViewCtrl::Collapse( const wxDataViewItem
& item
)
4032 int row
= m_clientArea
->GetRowByItem( item
);
4034 m_clientArea
->Collapse(row
);
4038 // !wxUSE_GENERICDATAVIEWCTRL
4041 // wxUSE_DATAVIEWCTRL