1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/datavgen.cpp
3 // Purpose: wxDataViewCtrl generic implementation
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 #if wxUSE_DATAVIEWCTRL
19 #include "wx/dataview.h"
21 #ifdef wxUSE_GENERICDATAVIEWCTRL
25 #include "wx/msw/wrapwin.h"
29 #include "wx/dcclient.h"
31 #include "wx/settings.h"
34 #include "wx/stockitem.h"
35 #include "wx/calctrl.h"
36 #include "wx/popupwin.h"
37 #include "wx/renderer.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 // wxDataViewHeaderWindow
48 //-----------------------------------------------------------------------------
50 class wxDataViewHeaderWindow
: public wxWindow
53 wxDataViewHeaderWindow( wxDataViewCtrl
*parent
,
55 const wxPoint
&pos
= wxDefaultPosition
,
56 const wxSize
&size
= wxDefaultSize
,
57 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") );
58 virtual ~wxDataViewHeaderWindow();
60 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
61 wxDataViewCtrl
*GetOwner() { return m_owner
; }
63 void OnPaint( wxPaintEvent
&event
);
64 void OnMouse( wxMouseEvent
&event
);
65 void OnSetFocus( wxFocusEvent
&event
);
68 wxDataViewCtrl
*m_owner
;
69 wxCursor
*m_resizeCursor
;
72 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow
)
76 //-----------------------------------------------------------------------------
77 // wxDataViewRenameTimer
78 //-----------------------------------------------------------------------------
80 class wxDataViewRenameTimer
: public wxTimer
83 wxDataViewMainWindow
*m_owner
;
86 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
90 //-----------------------------------------------------------------------------
91 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
92 //-----------------------------------------------------------------------------
94 class wxDataViewTextCtrlWrapper
: public wxEvtHandler
97 // NB: text must be a valid object but not Create()d yet
98 wxDataViewTextCtrlWrapper( wxDataViewMainWindow
*owner
,
100 wxDataViewListModel
*model
,
101 size_t col
, size_t row
,
104 wxTextCtrl
*GetText() const { return m_text
; }
106 void AcceptChangesAndFinish();
109 void OnChar( wxKeyEvent
&event
);
110 void OnKeyUp( wxKeyEvent
&event
);
111 void OnKillFocus( wxFocusEvent
&event
);
113 bool AcceptChanges();
117 wxDataViewMainWindow
*m_owner
;
119 wxString m_startValue
;
120 wxDataViewListModel
*m_model
;
124 bool m_aboutToFinish
;
126 DECLARE_EVENT_TABLE()
129 //-----------------------------------------------------------------------------
130 // wxDataViewMainWindow
131 //-----------------------------------------------------------------------------
133 WX_DEFINE_SORTED_ARRAY_SIZE_T( size_t, wxDataViewSelection
);
135 class wxDataViewMainWindow
: public wxWindow
138 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
140 const wxPoint
&pos
= wxDefaultPosition
,
141 const wxSize
&size
= wxDefaultSize
,
142 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
143 virtual ~wxDataViewMainWindow();
145 // notifications from wxDataViewListModel
148 bool RowInserted( size_t before
);
149 bool RowDeleted( size_t row
);
150 bool RowChanged( size_t row
);
151 bool ValueChanged( size_t col
, size_t row
);
152 bool RowsReordered( size_t *new_order
);
155 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
156 wxDataViewCtrl
*GetOwner() { return m_owner
; }
158 void OnPaint( wxPaintEvent
&event
);
159 void OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
);
160 void OnChar( wxKeyEvent
&event
);
161 void OnMouse( wxMouseEvent
&event
);
162 void OnSetFocus( wxFocusEvent
&event
);
163 void OnKillFocus( wxFocusEvent
&event
);
165 void UpdateDisplay();
166 void RecalculateDisplay();
167 void OnInternalIdle();
169 void OnRenameTimer();
170 void FinishEditing( wxTextCtrl
*text
);
172 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
174 bool HasCurrentRow() { return m_currentRow
!= (size_t)-1; }
175 void ChangeCurrentRow( size_t row
);
177 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); };
178 bool IsEmpty() { return GetRowCount() == 0; }
180 int GetCountPerPage();
181 int GetEndOfLastCol();
182 size_t GetFirstVisibleRow();
183 size_t GetLastVisibleRow();
184 size_t GetRowCount();
186 void SelectAllRows( bool on
);
187 void SelectRow( size_t row
, bool on
);
188 void SelectRows( size_t from
, size_t to
, bool on
);
189 void ReverseRowSelection( size_t row
);
190 bool IsRowSelected( size_t row
);
192 void RefreshRow( size_t row
);
193 void RefreshRows( size_t from
, size_t to
);
194 void RefreshRowsAfter( size_t firstRow
);
197 wxDataViewCtrl
*m_owner
;
201 wxDataViewColumn
*m_currentCol
;
203 wxDataViewSelection m_selection
;
205 wxDataViewRenameTimer
*m_renameTimer
;
206 wxDataViewTextCtrlWrapper
*m_textctrlWrapper
;
214 // for double click logic
215 size_t m_lineLastClicked
,
216 m_lineBeforeLastClicked
,
217 m_lineSelectSingleOnUp
;
220 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
221 DECLARE_EVENT_TABLE()
224 // ---------------------------------------------------------
225 // wxGenericDataViewListModelNotifier
226 // ---------------------------------------------------------
228 class wxGenericDataViewListModelNotifier
: public wxDataViewListModelNotifier
231 wxGenericDataViewListModelNotifier( wxDataViewMainWindow
*mainWindow
)
232 { m_mainWindow
= mainWindow
; }
234 virtual bool RowAppended()
235 { return m_mainWindow
->RowAppended(); }
236 virtual bool RowPrepended()
237 { return m_mainWindow
->RowPrepended(); }
238 virtual bool RowInserted( size_t before
)
239 { return m_mainWindow
->RowInserted( before
); }
240 virtual bool RowDeleted( size_t row
)
241 { return m_mainWindow
->RowDeleted( row
); }
242 virtual bool RowChanged( size_t row
)
243 { return m_mainWindow
->RowChanged( row
); }
244 virtual bool ValueChanged( size_t col
, size_t row
)
245 { return m_mainWindow
->ValueChanged( col
, row
); }
246 virtual bool RowsReordered( size_t *new_order
)
247 { return m_mainWindow
->RowsReordered( new_order
); }
248 virtual bool Cleared()
249 { return m_mainWindow
->Cleared(); }
251 wxDataViewMainWindow
*m_mainWindow
;
254 // ---------------------------------------------------------
256 // ---------------------------------------------------------
258 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
260 wxDataViewCell
::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
261 wxDataViewCellBase( varianttype
, mode
)
266 wxDataViewCell
::~wxDataViewCell()
272 wxDC
*wxDataViewCell
::GetDC()
276 if (GetOwner() == NULL
)
278 if (GetOwner()->GetOwner() == NULL
)
280 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
286 // ---------------------------------------------------------
287 // wxDataViewCustomCell
288 // ---------------------------------------------------------
290 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
292 wxDataViewCustomCell
::wxDataViewCustomCell( const wxString
&varianttype
,
293 wxDataViewCellMode mode
) :
294 wxDataViewCell( varianttype
, mode
)
298 // ---------------------------------------------------------
299 // wxDataViewTextCell
300 // ---------------------------------------------------------
302 IMPLEMENT_CLASS(wxDataViewTextCell
, wxDataViewCustomCell
)
304 wxDataViewTextCell
::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
305 wxDataViewCustomCell( varianttype
, mode
)
309 bool wxDataViewTextCell
::SetValue( const wxVariant
&value
)
311 m_text
= value
.GetString();
316 bool wxDataViewTextCell
::GetValue( wxVariant
& WXUNUSED(value
) )
321 bool wxDataViewTextCell
::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
323 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
328 wxSize wxDataViewTextCell
::GetSize()
330 return wxSize(80,20);
333 // ---------------------------------------------------------
334 // wxDataViewBitmapCell
335 // ---------------------------------------------------------
337 IMPLEMENT_CLASS(wxDataViewBitmapCell
, wxDataViewCustomCell
)
339 wxDataViewBitmapCell
::wxDataViewBitmapCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
340 wxDataViewCustomCell( varianttype
, mode
)
344 bool wxDataViewBitmapCell
::SetValue( const wxVariant
&value
)
346 if (value
.GetType() == wxT("wxBitmap"))
348 if (value
.GetType() == wxT("wxIcon"))
354 bool wxDataViewBitmapCell
::GetValue( wxVariant
& WXUNUSED(value
) )
359 bool wxDataViewBitmapCell
::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
362 dc
->DrawBitmap( m_bitmap
, cell
.x
, cell
.y
);
363 else if (m_icon
.Ok())
364 dc
->DrawIcon( m_icon
, cell
.x
, cell
.y
);
369 wxSize wxDataViewBitmapCell
::GetSize()
372 return wxSize( m_bitmap
.GetWidth(), m_bitmap
.GetHeight() );
373 else if (m_icon
.Ok())
374 return wxSize( m_icon
.GetWidth(), m_icon
.GetHeight() );
376 return wxSize(16,16);
379 // ---------------------------------------------------------
380 // wxDataViewToggleCell
381 // ---------------------------------------------------------
383 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCustomCell
)
385 wxDataViewToggleCell
::wxDataViewToggleCell( const wxString
&varianttype
,
386 wxDataViewCellMode mode
) :
387 wxDataViewCustomCell( varianttype
, mode
)
392 bool wxDataViewToggleCell
::SetValue( const wxVariant
&value
)
394 m_toggle
= value
.GetBool();
399 bool wxDataViewToggleCell
::GetValue( wxVariant
&WXUNUSED(value
) )
404 bool wxDataViewToggleCell
::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
406 // User wxRenderer here
409 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
411 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
416 flags
|= wxCONTROL_CHECKED
;
417 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
418 flags
|= wxCONTROL_DISABLED
;
420 wxRendererNative
::Get().DrawCheckBox(
421 GetOwner()->GetOwner(),
429 bool wxDataViewToggleCell
::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
431 bool value
= !m_toggle
;
432 wxVariant variant
= value
;
433 model
->SetValue( variant
, col
, row
);
434 model
->ValueChanged( col
, row
);
438 wxSize wxDataViewToggleCell
::GetSize()
440 return wxSize(20,20);
443 // ---------------------------------------------------------
444 // wxDataViewProgressCell
445 // ---------------------------------------------------------
447 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
449 wxDataViewProgressCell
::wxDataViewProgressCell( const wxString
&label
,
450 const wxString
&varianttype
, wxDataViewCellMode mode
) :
451 wxDataViewCustomCell( varianttype
, mode
)
457 wxDataViewProgressCell
::~wxDataViewProgressCell()
461 bool wxDataViewProgressCell
::SetValue( const wxVariant
&value
)
463 m_value
= (long) value
;
465 if (m_value
< 0) m_value
= 0;
466 if (m_value
> 100) m_value
= 100;
471 bool wxDataViewProgressCell
::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
473 double pct
= (double)m_value
/ 100.0;
475 bar
.width
= (int)(cell
.width
* pct
);
476 dc
->SetPen( *wxTRANSPARENT_PEN
);
477 dc
->SetBrush( *wxBLUE_BRUSH
);
478 dc
->DrawRectangle( bar
);
480 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
481 dc
->SetPen( *wxBLACK_PEN
);
482 dc
->DrawRectangle( cell
);
487 wxSize wxDataViewProgressCell
::GetSize()
489 return wxSize(40,12);
492 // ---------------------------------------------------------
493 // wxDataViewDateCell
494 // ---------------------------------------------------------
496 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
499 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
500 wxDataViewListModel
*model
, size_t col
, size_t row
) :
501 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
506 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
507 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
508 sizer
->Add( m_cal
, 1, wxGROW
);
513 void OnCalendar( wxCalendarEvent
&event
);
515 wxCalendarCtrl
*m_cal
;
516 wxDataViewListModel
*m_model
;
521 virtual void OnDismiss()
526 DECLARE_EVENT_TABLE()
529 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
530 EVT_CALENDAR( wxID_ANY
, wxDataViewDateCellPopupTransient
::OnCalendar
)
533 void wxDataViewDateCellPopupTransient
::OnCalendar( wxCalendarEvent
&event
)
535 wxDateTime date
= event
.GetDate();
536 wxVariant value
= date
;
537 m_model
->SetValue( value
, m_col
, m_row
);
538 m_model
->ValueChanged( m_col
, m_row
);
542 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
544 wxDataViewDateCell
::wxDataViewDateCell( const wxString
&varianttype
,
545 wxDataViewCellMode mode
) :
546 wxDataViewCustomCell( varianttype
, mode
)
550 bool wxDataViewDateCell
::SetValue( const wxVariant
&value
)
552 m_date
= value
.GetDateTime();
557 bool wxDataViewDateCell
::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
559 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
560 wxString tmp
= m_date
.FormatDate();
561 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
566 wxSize wxDataViewDateCell
::GetSize()
568 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
569 wxString tmp
= m_date
.FormatDate();
571 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
572 return wxSize(x
,y
+d
);
575 bool wxDataViewDateCell
::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
578 model
->GetValue( variant
, col
, row
);
579 wxDateTime value
= variant
.GetDateTime();
581 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
582 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
583 wxPoint pos
= wxGetMousePosition();
586 popup
->Popup( popup
->m_cal
);
591 // ---------------------------------------------------------
593 // ---------------------------------------------------------
595 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
597 wxDataViewColumn
::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
, size_t model_column
,
598 int fixed_width
, wxDataViewColumnSizing sizing
, int flags
) :
599 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
603 m_width
= fixed_width
;
604 m_fixedWidth
= fixed_width
;
607 wxDataViewColumn
::~wxDataViewColumn()
611 void wxDataViewColumn
::SetTitle( const wxString
&title
)
613 wxDataViewColumnBase
::SetTitle( title
);
617 int wxDataViewColumn
::GetWidth()
622 void wxDataViewColumn
::SetFixedWidth( int width
)
624 m_fixedWidth
= width
;
626 if (m_sizing
== wxDATAVIEW_COL_WIDTH_FIXED
)
633 int wxDataViewColumn
::GetFixedWidth()
638 //-----------------------------------------------------------------------------
639 // wxDataViewHeaderWindow
640 //-----------------------------------------------------------------------------
642 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow
, wxWindow
)
644 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
,wxWindow
)
645 EVT_PAINT (wxDataViewHeaderWindow
::OnPaint
)
646 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow
::OnMouse
)
647 EVT_SET_FOCUS (wxDataViewHeaderWindow
::OnSetFocus
)
650 wxDataViewHeaderWindow
::wxDataViewHeaderWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
651 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
652 wxWindow( parent
, id
, pos
, size
, 0, name
)
656 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
658 wxVisualAttributes attr
= wxPanel
::GetClassDefaultAttributes();
659 SetOwnForegroundColour( attr
.colFg
);
660 SetOwnBackgroundColour( attr
.colBg
);
662 SetOwnFont( attr
.font
);
665 wxDataViewHeaderWindow
::~wxDataViewHeaderWindow()
667 delete m_resizeCursor
;
670 void wxDataViewHeaderWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
673 GetClientSize( &w
, &h
);
675 wxPaintDC
dc( this );
678 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
681 m_owner
->GetViewStart( &x
, NULL
);
683 // account for the horz scrollbar offset
684 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
686 dc
.SetFont( GetFont() );
688 size_t cols
= GetOwner()->GetNumberOfColumns();
691 for (i
= 0; i
< cols
; i
++)
693 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
694 int width
= col
->GetWidth();
699 wxRendererNative
::Get().DrawHeaderButton
703 wxRect(xpos
, 0, cw
, ch
-1),
704 m_parent
->IsEnabled() ?
0
705 : (int)wxCONTROL_DISABLED
708 dc
.DrawText( col
->GetTitle(), xpos
+3, 3 );
714 void wxDataViewHeaderWindow
::OnMouse( wxMouseEvent
&WXUNUSED(event
) )
718 void wxDataViewHeaderWindow
::OnSetFocus( wxFocusEvent
&event
)
720 GetParent()->SetFocus();
724 //-----------------------------------------------------------------------------
725 // wxDataViewRenameTimer
726 //-----------------------------------------------------------------------------
728 wxDataViewRenameTimer
::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
733 void wxDataViewRenameTimer
::Notify()
735 m_owner
->OnRenameTimer();
738 //-----------------------------------------------------------------------------
739 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
740 //-----------------------------------------------------------------------------
742 BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper
, wxEvtHandler
)
743 EVT_CHAR (wxDataViewTextCtrlWrapper
::OnChar
)
744 EVT_KEY_UP (wxDataViewTextCtrlWrapper
::OnKeyUp
)
745 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper
::OnKillFocus
)
748 wxDataViewTextCtrlWrapper
::wxDataViewTextCtrlWrapper(
749 wxDataViewMainWindow
*owner
,
751 wxDataViewListModel
*model
,
752 size_t col
, size_t row
,
762 m_aboutToFinish
= false;
765 model
->GetValue( value
, col
, row
);
766 m_startValue
= value
.GetString();
768 m_owner
->GetOwner()->CalcScrolledPosition(
769 rectLabel
.x
, rectLabel
.y
, &rectLabel
.x
, &rectLabel
.y
);
771 m_text
->Create( owner
, wxID_ANY
, m_startValue
,
772 wxPoint(rectLabel
.x
-2,rectLabel
.y
-2),
773 wxSize(rectLabel
.width
+7,rectLabel
.height
+4) );
776 m_text
->PushEventHandler(this);
779 void wxDataViewTextCtrlWrapper
::AcceptChangesAndFinish()
781 m_aboutToFinish
= true;
783 // Notify the owner about the changes
786 // Even if vetoed, close the control (consistent with MSW)
790 void wxDataViewTextCtrlWrapper
::OnChar( wxKeyEvent
&event
)
792 switch ( event
.m_keyCode
)
795 AcceptChangesAndFinish();
799 // m_owner->OnRenameCancelled( m_itemEdited );
808 void wxDataViewTextCtrlWrapper
::OnKeyUp( wxKeyEvent
&event
)
816 // auto-grow the textctrl
817 wxSize parentSize
= m_owner
->GetSize();
818 wxPoint myPos
= m_text
->GetPosition();
819 wxSize mySize
= m_text
->GetSize();
821 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
822 if (myPos
.x
+ sx
> parentSize
.x
)
823 sx
= parentSize
.x
- myPos
.x
;
826 m_text
->SetSize(sx
, wxDefaultCoord
);
831 void wxDataViewTextCtrlWrapper
::OnKillFocus( wxFocusEvent
&event
)
833 if ( !m_finished
&& !m_aboutToFinish
)
836 //if ( !AcceptChanges() )
837 // m_owner->OnRenameCancelled( m_itemEdited );
842 // We must let the native text control handle focus
846 bool wxDataViewTextCtrlWrapper
::AcceptChanges()
848 const wxString value
= m_text
->GetValue();
850 if ( value
== m_startValue
)
851 // nothing changed, always accept
854 // if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
855 // vetoed by the user
858 // accepted, do rename the item
861 m_model
->SetValue( variant
, m_col
, m_row
);
862 m_model
->ValueChanged( m_col
, m_row
);
867 void wxDataViewTextCtrlWrapper
::Finish()
873 m_text
->RemoveEventHandler(this);
874 m_owner
->FinishEditing(m_text
);
877 wxPendingDelete
.Append( this );
881 //-----------------------------------------------------------------------------
882 // wxDataViewMainWindow
883 //-----------------------------------------------------------------------------
885 int LINKAGEMODE
wxDataViewSelectionCmp( size_t row1
, size_t row2
)
887 if (row1
> row2
) return 1;
888 if (row1
== row2
) return 0;
893 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
895 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
896 EVT_PAINT (wxDataViewMainWindow
::OnPaint
)
897 EVT_MOUSE_EVENTS (wxDataViewMainWindow
::OnMouse
)
898 EVT_SET_FOCUS (wxDataViewMainWindow
::OnSetFocus
)
899 EVT_KILL_FOCUS (wxDataViewMainWindow
::OnKillFocus
)
900 EVT_CHAR (wxDataViewMainWindow
::OnChar
)
903 wxDataViewMainWindow
::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
904 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
905 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
906 m_selection( wxDataViewSelectionCmp
)
911 m_lastOnSame
= false;
912 m_renameTimer
= new wxDataViewRenameTimer( this );
913 m_textctrlWrapper
= NULL
;
915 // TODO: user better initial values/nothing selected
919 // TODO: we need to calculate this smartly
923 m_dragStart
= wxPoint(0,0);
924 m_lineLastClicked
= (size_t) -1;
925 m_lineBeforeLastClicked
= (size_t) -1;
926 m_lineSelectSingleOnUp
= (size_t) -1;
930 SetBackgroundColour( *wxWHITE
);
935 wxDataViewMainWindow
::~wxDataViewMainWindow()
937 delete m_renameTimer
;
940 void wxDataViewMainWindow
::OnRenameTimer()
942 // We have to call this here because changes may just have
943 // been made and no screen update taken place.
949 size_t cols
= GetOwner()->GetNumberOfColumns();
951 for (i
= 0; i
< cols
; i
++)
953 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
954 if (c
== m_currentCol
)
956 xpos
+= c
->GetWidth();
958 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
, m_currentCol
->GetWidth(), m_lineHeight
);
960 wxClassInfo
*textControlClass
= CLASSINFO(wxTextCtrl
);
962 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
963 m_textctrlWrapper
= new wxDataViewTextCtrlWrapper(this, text
, GetOwner()->GetModel(),
964 m_currentCol
->GetModelColumn(), m_currentRow
, labelRect
);
967 void wxDataViewMainWindow
::FinishEditing( wxTextCtrl
*text
)
970 m_textctrlWrapper
= NULL
;
972 // SetFocusIgnoringChildren();
975 bool wxDataViewMainWindow
::RowAppended()
980 bool wxDataViewMainWindow
::RowPrepended()
985 bool wxDataViewMainWindow
::RowInserted( size_t WXUNUSED(before
) )
990 bool wxDataViewMainWindow
::RowDeleted( size_t WXUNUSED(row
) )
995 bool wxDataViewMainWindow
::RowChanged( size_t WXUNUSED(row
) )
1000 bool wxDataViewMainWindow
::ValueChanged( size_t WXUNUSED(col
), size_t row
)
1002 wxRect
rect( 0, row
*m_lineHeight
, 10000, m_lineHeight
);
1003 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1004 Refresh( true, &rect
);
1009 bool wxDataViewMainWindow
::RowsReordered( size_t *WXUNUSED(new_order
) )
1016 bool wxDataViewMainWindow
::Cleared()
1021 void wxDataViewMainWindow
::UpdateDisplay()
1026 void wxDataViewMainWindow
::OnInternalIdle()
1028 wxWindow
::OnInternalIdle();
1032 RecalculateDisplay();
1037 void wxDataViewMainWindow
::RecalculateDisplay()
1039 wxDataViewListModel
*model
= GetOwner()->GetModel();
1047 size_t cols
= GetOwner()->GetNumberOfColumns();
1049 for (i
= 0; i
< cols
; i
++)
1051 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1052 width
+= col
->GetWidth();
1055 int height
= model
->GetNumberOfRows() * m_lineHeight
;
1057 SetVirtualSize( width
, height
);
1058 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1063 void wxDataViewMainWindow
::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1065 wxWindow
::ScrollWindow( dx
, dy
, rect
);
1066 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1069 void wxDataViewMainWindow
::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1071 wxPaintDC
dc( this );
1073 GetOwner()->PrepareDC( dc
);
1075 dc
.SetFont( GetFont() );
1077 wxRect update
= GetUpdateRegion().GetBox();
1078 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1080 wxDataViewListModel
*model
= GetOwner()->GetModel();
1082 size_t item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1083 size_t item_count
= wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1084 (int)(model
->GetNumberOfRows()-item_start
) );
1089 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1091 if (m_selection
.Index( item
) != wxNOT_FOUND
)
1093 int flags
= wxCONTROL_SELECTED
;
1094 if (item
== m_currentRow
)
1095 flags
|= wxCONTROL_CURRENT
;
1097 flags
|= wxCONTROL_FOCUSED
;
1098 wxRect
rect( 0, item
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1099 wxRendererNative
::Get().DrawItemSelectionRect
1109 if (item
== m_currentRow
)
1111 int flags
= wxCONTROL_CURRENT
;
1113 flags
|= wxCONTROL_FOCUSED
; // should have no effect
1114 wxRect
rect( 0, item
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1115 wxRendererNative
::Get().DrawItemSelectionRect
1129 cell_rect
.height
= m_lineHeight
;
1130 size_t cols
= GetOwner()->GetNumberOfColumns();
1132 for (i
= 0; i
< cols
; i
++)
1134 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1135 wxDataViewCell
*cell
= col
->GetCell();
1136 cell_rect
.width
= col
->GetWidth();
1138 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1140 cell_rect
.y
= item
*m_lineHeight
;
1142 model
->GetValue( value
, col
->GetModelColumn(), item
);
1143 cell
->SetValue( value
);
1144 wxSize size
= cell
->GetSize();
1145 // cannot be bigger than allocated space
1146 size
.x
= wxMin( size
.x
, cell_rect
.width
);
1147 size
.y
= wxMin( size
.y
, cell_rect
.height
);
1148 // TODO: check for left/right/centre alignment here
1151 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1152 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1154 item_rect
.width
= size
.x
;
1155 item_rect
.height
= size
.y
;
1156 cell
->Render( item_rect
, &dc
, 0 );
1159 cell_rect
.x
+= cell_rect
.width
;
1163 int wxDataViewMainWindow
::GetCountPerPage()
1165 wxSize size
= GetClientSize();
1166 return size
.y
/ m_lineHeight
;
1169 int wxDataViewMainWindow
::GetEndOfLastCol()
1173 for (i
= 0; i
< GetOwner()->GetNumberOfColumns(); i
++)
1175 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1176 width
+= c
->GetWidth();
1181 size_t wxDataViewMainWindow
::GetFirstVisibleRow()
1185 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1187 return y
/ m_lineHeight
;
1190 size_t wxDataViewMainWindow
::GetLastVisibleRow()
1192 wxSize client_size
= GetClientSize();
1193 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
, &client_size
.x
, &client_size
.y
);
1195 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
1198 size_t wxDataViewMainWindow
::GetRowCount()
1200 return GetOwner()->GetModel()->GetNumberOfRows();
1203 void wxDataViewMainWindow
::ChangeCurrentRow( size_t row
)
1210 void wxDataViewMainWindow
::SelectAllRows( bool on
)
1217 m_selection
.Clear();
1218 for (size_t i
= 0; i
< GetRowCount(); i
++)
1219 m_selection
.Add( i
);
1224 size_t first_visible
= GetFirstVisibleRow();
1225 size_t last_visible
= GetLastVisibleRow();
1227 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1229 size_t row
= m_selection
[i
];
1230 if ((row
>= first_visible
) && (row
<= last_visible
))
1233 m_selection
.Clear();
1237 void wxDataViewMainWindow
::SelectRow( size_t row
, bool on
)
1239 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1243 m_selection
.Add( row
);
1251 m_selection
.Remove( row
);
1257 void wxDataViewMainWindow
::SelectRows( size_t from
, size_t to
, bool on
)
1267 for (i
= from
; i
<= to
; i
++)
1269 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1272 m_selection
.Add( i
);
1277 m_selection
.Remove( i
);
1280 RefreshRows( from
, to
);
1283 void wxDataViewMainWindow
::ReverseRowSelection( size_t row
)
1285 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1286 m_selection
.Add( row
);
1288 m_selection
.Remove( row
);
1292 bool wxDataViewMainWindow
::IsRowSelected( size_t row
)
1294 return (m_selection
.Index( row
) != wxNOT_FOUND
);
1297 void wxDataViewMainWindow
::RefreshRow( size_t row
)
1299 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
1300 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1302 wxSize client_size
= GetClientSize();
1303 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1304 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1305 if (intersect_rect
.width
> 0)
1306 Refresh( true, &intersect_rect
);
1309 void wxDataViewMainWindow
::RefreshRows( size_t from
, size_t to
)
1318 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
1319 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1321 wxSize client_size
= GetClientSize();
1322 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1323 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1324 if (intersect_rect
.width
> 0)
1325 Refresh( true, &intersect_rect
);
1328 void wxDataViewMainWindow
::RefreshRowsAfter( size_t firstRow
)
1330 size_t count
= GetRowCount();
1331 if (firstRow
> count
)
1334 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
1335 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1337 wxSize client_size
= GetClientSize();
1338 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1339 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1340 if (intersect_rect
.width
> 0)
1341 Refresh( true, &intersect_rect
);
1344 void wxDataViewMainWindow
::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
1346 wxCHECK_RET( newCurrent
< GetRowCount(),
1347 _T("invalid item index in OnArrowChar()") );
1349 // if there is no selection, we cannot move it anywhere
1350 if (!HasCurrentRow())
1353 size_t oldCurrent
= m_currentRow
;
1355 // in single selection we just ignore Shift as we can't select several
1357 if ( event
.ShiftDown() && !IsSingleSel() )
1359 RefreshRow( oldCurrent
);
1361 ChangeCurrentRow( newCurrent
);
1363 // select all the items between the old and the new one
1364 if ( oldCurrent
> newCurrent
)
1366 newCurrent
= oldCurrent
;
1367 oldCurrent
= m_currentRow
;
1370 SelectRows( oldCurrent
, newCurrent
, true );
1374 RefreshRow( oldCurrent
);
1376 // all previously selected items are unselected unless ctrl is held
1377 if ( !event
.ControlDown() )
1378 SelectAllRows(false);
1380 ChangeCurrentRow( newCurrent
);
1382 if ( !event
.ControlDown() )
1383 SelectRow( m_currentRow
, true );
1385 RefreshRow( m_currentRow
);
1391 void wxDataViewMainWindow
::OnChar( wxKeyEvent
&event
)
1393 if (event
.GetKeyCode() == WXK_TAB
)
1395 wxNavigationKeyEvent nevent
;
1396 nevent
.SetWindowChange( event
.ControlDown() );
1397 nevent
.SetDirection( !event
.ShiftDown() );
1398 nevent
.SetEventObject( GetParent()->GetParent() );
1399 nevent
.SetCurrentFocus( m_parent
);
1400 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
1404 // no item -> nothing to do
1405 if (!HasCurrentRow())
1411 // don't use m_linesPerPage directly as it might not be computed yet
1412 const int pageSize
= GetCountPerPage();
1413 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
1415 switch ( event
.GetKeyCode() )
1418 if ( m_currentRow
> 0 )
1419 OnArrowChar( m_currentRow
- 1, event
);
1423 if ( m_currentRow
< GetRowCount() - 1 )
1424 OnArrowChar( m_currentRow
+ 1, event
);
1429 OnArrowChar( GetRowCount() - 1, event
);
1434 OnArrowChar( 0, event
);
1439 int steps
= pageSize
- 1;
1440 int index
= m_currentRow
- steps
;
1444 OnArrowChar( index
, event
);
1450 int steps
= pageSize
- 1;
1451 size_t index
= m_currentRow
+ steps
;
1452 size_t count
= GetRowCount();
1453 if ( index
>= count
)
1456 OnArrowChar( index
, event
);
1465 void wxDataViewMainWindow
::OnMouse( wxMouseEvent
&event
)
1467 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
1469 // let the base handle mouse wheel events.
1474 int x
= event
.GetX();
1475 int y
= event
.GetY();
1476 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1478 wxDataViewColumn
*col
= NULL
;
1481 size_t cols
= GetOwner()->GetNumberOfColumns();
1483 for (i
= 0; i
< cols
; i
++)
1485 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1486 if (x
< xpos
+ c
->GetWidth())
1491 xpos
+= c
->GetWidth();
1495 wxDataViewCell
*cell
= col
->GetCell();
1497 size_t current
= y
/ m_lineHeight
;
1499 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
1501 // Unselect all if below the last row ?
1505 wxDataViewListModel
*model
= GetOwner()->GetModel();
1507 if (event
.Dragging())
1509 if (m_dragCount
== 0)
1511 // we have to report the raw, physical coords as we want to be
1512 // able to call HitTest(event.m_pointDrag) from the user code to
1513 // get the item being dragged
1514 m_dragStart
= event
.GetPosition();
1519 if (m_dragCount
!= 3)
1522 if (event
.LeftIsDown())
1524 // Notify cell about drag
1533 bool forceClick
= false;
1535 if (event
.ButtonDClick())
1537 m_renameTimer
->Stop();
1538 m_lastOnSame
= false;
1541 if (event
.LeftDClick())
1543 if ( current
== m_lineLastClicked
)
1545 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
1548 model
->GetValue( value
, col
->GetModelColumn(), current
);
1549 cell
->SetValue( value
);
1550 wxRect
cell_rect( xpos
, current
* m_lineHeight
, col
->GetWidth(), m_lineHeight
);
1551 cell
->Activate( cell_rect
, model
, col
->GetModelColumn(), current
);
1557 // The first click was on another item, so don't interpret this as
1558 // a double click, but as a simple click instead
1565 if (m_lineSelectSingleOnUp
!= (size_t)-1)
1567 // select single line
1568 SelectAllRows( false );
1569 SelectRow( m_lineSelectSingleOnUp
, true );
1574 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
1575 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
1577 m_renameTimer
->Start( 100, true );
1581 m_lastOnSame
= false;
1582 m_lineSelectSingleOnUp
= (size_t)-1;
1586 // This is necessary, because after a DnD operation in
1587 // from and to ourself, the up event is swallowed by the
1588 // DnD code. So on next non-up event (which means here and
1589 // now) m_lineSelectSingleOnUp should be reset.
1590 m_lineSelectSingleOnUp
= (size_t)-1;
1593 if (event
.RightDown())
1595 m_lineBeforeLastClicked
= m_lineLastClicked
;
1596 m_lineLastClicked
= current
;
1598 // If the item is already selected, do not update the selection.
1599 // Multi-selections should not be cleared if a selected item is clicked.
1600 if (!IsRowSelected(current
))
1602 SelectAllRows(false);
1603 ChangeCurrentRow(current
);
1604 SelectRow(m_currentRow
,true);
1607 // notify cell about right click
1610 // Allow generation of context menu event
1613 else if (event
.MiddleDown())
1615 // notify cell about middle click
1618 if (event
.LeftDown() || forceClick
)
1624 m_lineBeforeLastClicked
= m_lineLastClicked
;
1625 m_lineLastClicked
= current
;
1627 size_t oldCurrentRow
= m_currentRow
;
1628 bool oldWasSelected
= IsRowSelected(m_currentRow
);
1630 bool cmdModifierDown
= event
.CmdDown();
1631 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
1633 if ( IsSingleSel() || !IsRowSelected(current
) )
1635 SelectAllRows( false );
1637 ChangeCurrentRow(current
);
1639 SelectRow(m_currentRow
,true);
1641 else // multi sel & current is highlighted & no mod keys
1643 m_lineSelectSingleOnUp
= current
;
1644 ChangeCurrentRow(current
); // change focus
1647 else // multi sel & either ctrl or shift is down
1649 if (cmdModifierDown
)
1651 ChangeCurrentRow(current
);
1653 ReverseRowSelection(m_currentRow
);
1655 else if (event
.ShiftDown())
1657 ChangeCurrentRow(current
);
1659 size_t lineFrom
= oldCurrentRow
,
1662 if ( lineTo
< lineFrom
)
1665 lineFrom
= m_currentRow
;
1668 SelectRows(lineFrom
, lineTo
, true);
1670 else // !ctrl, !shift
1672 // test in the enclosing if should make it impossible
1673 wxFAIL_MSG( _T("how did we get here?") );
1677 if (m_currentRow
!= oldCurrentRow
)
1678 RefreshRow( oldCurrentRow
);
1680 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
1682 // Update selection here...
1685 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) && (current
== oldCurrentRow
)) && oldWasSelected
;
1689 void wxDataViewMainWindow
::OnSetFocus( wxFocusEvent
&event
)
1693 if (HasCurrentRow())
1699 void wxDataViewMainWindow
::OnKillFocus( wxFocusEvent
&event
)
1703 if (HasCurrentRow())
1709 //-----------------------------------------------------------------------------
1711 //-----------------------------------------------------------------------------
1713 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1715 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
1716 EVT_SIZE(wxDataViewCtrl
::OnSize
)
1719 wxDataViewCtrl
::~wxDataViewCtrl()
1722 GetModel()->RemoveNotifier( m_notifier
);
1725 void wxDataViewCtrl
::Init()
1730 bool wxDataViewCtrl
::Create(wxWindow
*parent
, wxWindowID id
,
1731 const wxPoint
& pos
, const wxSize
& size
,
1732 long style
, const wxValidator
& validator
)
1734 if (!wxControl
::Create( parent
, id
, pos
, size
, style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
1740 MacSetClipChildren( true ) ;
1743 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
1745 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,22) );
1747 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,25) );
1750 SetTargetWindow( m_clientArea
);
1752 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
1753 sizer
->Add( m_headerArea
, 0, wxGROW
);
1754 sizer
->Add( m_clientArea
, 1, wxGROW
);
1761 WXLRESULT wxDataViewCtrl
::MSWWindowProc(WXUINT nMsg
,
1765 WXLRESULT rc
= wxDataViewCtrlBase
::MSWWindowProc(nMsg
, wParam
, lParam
);
1768 // we need to process arrows ourselves for scrolling
1769 if ( nMsg
== WM_GETDLGCODE
)
1771 rc
|= DLGC_WANTARROWS
;
1779 void wxDataViewCtrl
::OnSize( wxSizeEvent
&WXUNUSED(event
) )
1781 // We need to override OnSize so that our scrolled
1782 // window a) does call Layout() to use sizers for
1783 // positioning the controls but b) does not query
1784 // the sizer for their size and use that for setting
1785 // the scrollable area as set that ourselves by
1786 // calling SetScrollbar() further down.
1793 bool wxDataViewCtrl
::AssociateModel( wxDataViewListModel
*model
)
1795 if (!wxDataViewCtrlBase
::AssociateModel( model
))
1798 m_notifier
= new wxGenericDataViewListModelNotifier( m_clientArea
);
1800 model
->AddNotifier( m_notifier
);
1802 m_clientArea
->UpdateDisplay();
1807 bool wxDataViewCtrl
::AppendColumn( wxDataViewColumn
*col
)
1809 if (!wxDataViewCtrlBase
::AppendColumn(col
))
1812 m_clientArea
->UpdateDisplay();
1818 // !wxUSE_GENERICDATAVIEWCTRL
1821 // wxUSE_DATAVIEWCTRL