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
26 #include "wx/dcclient.h"
28 #include "wx/settings.h"
31 #include "wx/stockitem.h"
32 #include "wx/calctrl.h"
33 #include "wx/popupwin.h"
34 #include "wx/renderer.h"
37 #include "wx/msw/wrapwin.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 ~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 ~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_ABSTRACT_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 // wxDataViewToggleCell
335 // ---------------------------------------------------------
337 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCustomCell
)
339 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
340 wxDataViewCellMode mode
) :
341 wxDataViewCustomCell( varianttype
, mode
)
346 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
348 m_toggle
= value
.GetBool();
353 bool wxDataViewToggleCell::GetValue( wxVariant
&WXUNUSED(value
) )
358 bool wxDataViewToggleCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
360 // User wxRenderer here
363 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
365 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
370 flags
|= wxCONTROL_CHECKED
;
371 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
372 flags
|= wxCONTROL_DISABLED
;
374 wxRendererNative::Get().DrawCheckBox(
375 GetOwner()->GetOwner(),
383 bool wxDataViewToggleCell::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
385 bool value
= !m_toggle
;
386 wxVariant variant
= value
;
387 model
->SetValue( variant
, col
, row
);
388 model
->ValueChanged( col
, row
);
392 wxSize
wxDataViewToggleCell::GetSize()
394 return wxSize(20,20);
397 // ---------------------------------------------------------
398 // wxDataViewProgressCell
399 // ---------------------------------------------------------
401 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
403 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
404 const wxString
&varianttype
, wxDataViewCellMode mode
) :
405 wxDataViewCustomCell( varianttype
, mode
)
411 wxDataViewProgressCell::~wxDataViewProgressCell()
415 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
417 m_value
= (long) value
;
419 if (m_value
< 0) m_value
= 0;
420 if (m_value
> 100) m_value
= 100;
425 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
427 double pct
= (double)m_value
/ 100.0;
429 bar
.width
= (int)(cell
.width
* pct
);
430 dc
->SetPen( *wxTRANSPARENT_PEN
);
431 dc
->SetBrush( *wxBLUE_BRUSH
);
432 dc
->DrawRectangle( bar
);
434 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
435 dc
->SetPen( *wxBLACK_PEN
);
436 dc
->DrawRectangle( cell
);
441 wxSize
wxDataViewProgressCell::GetSize()
443 return wxSize(40,12);
446 // ---------------------------------------------------------
447 // wxDataViewDateCell
448 // ---------------------------------------------------------
450 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
453 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
454 wxDataViewListModel
*model
, size_t col
, size_t row
) :
455 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
460 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
461 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
462 sizer
->Add( m_cal
, 1, wxGROW
);
467 void OnCalendar( wxCalendarEvent
&event
);
469 wxCalendarCtrl
*m_cal
;
470 wxDataViewListModel
*m_model
;
475 virtual void OnDismiss()
480 DECLARE_EVENT_TABLE()
483 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
484 EVT_CALENDAR( wxID_ANY
, wxDataViewDateCellPopupTransient::OnCalendar
)
487 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
489 wxDateTime date
= event
.GetDate();
490 wxVariant value
= date
;
491 m_model
->SetValue( value
, m_col
, m_row
);
492 m_model
->ValueChanged( m_col
, m_row
);
496 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
498 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
499 wxDataViewCellMode mode
) :
500 wxDataViewCustomCell( varianttype
, mode
)
504 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
506 m_date
= value
.GetDateTime();
511 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
513 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
514 wxString tmp
= m_date
.FormatDate();
515 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
520 wxSize
wxDataViewDateCell::GetSize()
522 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
523 wxString tmp
= m_date
.FormatDate();
525 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
526 return wxSize(x
,y
+d
);
529 bool wxDataViewDateCell::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
532 model
->GetValue( variant
, col
, row
);
533 wxDateTime value
= variant
.GetDateTime();
535 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
536 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
537 wxPoint pos
= wxGetMousePosition();
540 popup
->Popup( popup
->m_cal
);
545 // ---------------------------------------------------------
547 // ---------------------------------------------------------
549 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
551 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
, size_t model_column
,
552 int fixed_width
, wxDataViewColumnSizing sizing
, int flags
) :
553 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
557 m_width
= fixed_width
;
558 m_fixedWidth
= fixed_width
;
561 wxDataViewColumn::~wxDataViewColumn()
565 void wxDataViewColumn::SetTitle( const wxString
&title
)
567 wxDataViewColumnBase::SetTitle( title
);
571 int wxDataViewColumn::GetWidth()
576 void wxDataViewColumn::SetFixedWidth( int width
)
578 m_fixedWidth
= width
;
580 if (m_sizing
== wxDATAVIEW_COL_WIDTH_FIXED
)
587 int wxDataViewColumn::GetFixedWidth()
592 //-----------------------------------------------------------------------------
593 // wxDataViewHeaderWindow
594 //-----------------------------------------------------------------------------
596 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow
, wxWindow
)
598 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
,wxWindow
)
599 EVT_PAINT (wxDataViewHeaderWindow::OnPaint
)
600 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse
)
601 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus
)
604 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
605 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
606 wxWindow( parent
, id
, pos
, size
, 0, name
)
610 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
612 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
613 SetOwnForegroundColour( attr
.colFg
);
614 SetOwnBackgroundColour( attr
.colBg
);
616 SetOwnFont( attr
.font
);
619 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
621 delete m_resizeCursor
;
624 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
627 GetClientSize( &w
, &h
);
629 wxPaintDC
dc( this );
632 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
635 m_owner
->GetViewStart( &x
, NULL
);
637 // account for the horz scrollbar offset
638 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
640 dc
.SetFont( GetFont() );
642 size_t cols
= GetOwner()->GetNumberOfColumns();
645 for (i
= 0; i
< cols
; i
++)
647 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
648 int width
= col
->GetWidth();
653 wxRendererNative::Get().DrawHeaderButton
657 wxRect(xpos
, 0, cw
, ch
-1),
658 m_parent
->IsEnabled() ? 0
659 : (int)wxCONTROL_DISABLED
662 dc
.DrawText( col
->GetTitle(), xpos
+3, 3 );
668 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent
&WXUNUSED(event
) )
672 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
674 GetParent()->SetFocus();
678 //-----------------------------------------------------------------------------
679 // wxDataViewRenameTimer
680 //-----------------------------------------------------------------------------
682 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
687 void wxDataViewRenameTimer::Notify()
689 m_owner
->OnRenameTimer();
692 //-----------------------------------------------------------------------------
693 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
694 //-----------------------------------------------------------------------------
696 BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper
, wxEvtHandler
)
697 EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar
)
698 EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp
)
699 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus
)
702 wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper(
703 wxDataViewMainWindow
*owner
,
705 wxDataViewListModel
*model
,
706 size_t col
, size_t row
,
716 m_aboutToFinish
= false;
719 model
->GetValue( value
, col
, row
);
720 m_startValue
= value
.GetString();
722 m_owner
->GetOwner()->CalcScrolledPosition(
723 rectLabel
.x
, rectLabel
.y
, &rectLabel
.x
, &rectLabel
.y
);
725 m_text
->Create( owner
, wxID_ANY
, m_startValue
,
726 wxPoint(rectLabel
.x
-2,rectLabel
.y
-2),
727 wxSize(rectLabel
.width
+7,rectLabel
.height
+4) );
730 m_text
->PushEventHandler(this);
733 void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish()
735 m_aboutToFinish
= true;
737 // Notify the owner about the changes
740 // Even if vetoed, close the control (consistent with MSW)
744 void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
746 switch ( event
.m_keyCode
)
749 AcceptChangesAndFinish();
753 // m_owner->OnRenameCancelled( m_itemEdited );
762 void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
770 // auto-grow the textctrl
771 wxSize parentSize
= m_owner
->GetSize();
772 wxPoint myPos
= m_text
->GetPosition();
773 wxSize mySize
= m_text
->GetSize();
775 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
776 if (myPos
.x
+ sx
> parentSize
.x
)
777 sx
= parentSize
.x
- myPos
.x
;
780 m_text
->SetSize(sx
, wxDefaultCoord
);
785 void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
787 if ( !m_finished
&& !m_aboutToFinish
)
790 //if ( !AcceptChanges() )
791 // m_owner->OnRenameCancelled( m_itemEdited );
796 // We must let the native text control handle focus
800 bool wxDataViewTextCtrlWrapper::AcceptChanges()
802 const wxString value
= m_text
->GetValue();
804 if ( value
== m_startValue
)
805 // nothing changed, always accept
808 // if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
809 // vetoed by the user
812 // accepted, do rename the item
815 m_model
->SetValue( variant
, m_col
, m_row
);
816 m_model
->ValueChanged( m_col
, m_row
);
821 void wxDataViewTextCtrlWrapper::Finish()
827 m_text
->RemoveEventHandler(this);
828 m_owner
->FinishEditing(m_text
);
831 wxPendingDelete
.Append( this );
835 //-----------------------------------------------------------------------------
836 // wxDataViewMainWindow
837 //-----------------------------------------------------------------------------
839 int LINKAGEMODE
wxDataViewSelectionCmp( size_t row1
, size_t row2
)
841 if (row1
> row2
) return 1;
842 if (row1
== row2
) return 0;
847 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
849 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
850 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
851 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
852 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
853 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
854 EVT_CHAR (wxDataViewMainWindow::OnChar
)
857 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
858 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
859 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
860 m_selection( wxDataViewSelectionCmp
)
865 m_lastOnSame
= false;
866 m_renameTimer
= new wxDataViewRenameTimer( this );
867 m_textctrlWrapper
= NULL
;
869 // TODO: user better initial values/nothing selected
873 // TODO: we need to calculate this smartly
877 m_dragStart
= wxPoint(0,0);
878 m_lineLastClicked
= (size_t) -1;
879 m_lineBeforeLastClicked
= (size_t) -1;
880 m_lineSelectSingleOnUp
= (size_t) -1;
884 SetBackgroundColour( *wxWHITE
);
889 wxDataViewMainWindow::~wxDataViewMainWindow()
891 delete m_renameTimer
;
894 void wxDataViewMainWindow::OnRenameTimer()
896 // We have to call this here because changes may just have
897 // been made and no screen update taken place.
903 size_t cols
= GetOwner()->GetNumberOfColumns();
905 for (i
= 0; i
< cols
; i
++)
907 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
908 if (c
== m_currentCol
)
910 xpos
+= c
->GetWidth();
912 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
, m_currentCol
->GetWidth(), m_lineHeight
);
914 wxClassInfo
*textControlClass
= CLASSINFO(wxTextCtrl
);
916 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
917 m_textctrlWrapper
= new wxDataViewTextCtrlWrapper(this, text
, GetOwner()->GetModel(),
918 m_currentCol
->GetModelColumn(), m_currentRow
, labelRect
);
921 void wxDataViewMainWindow::FinishEditing( wxTextCtrl
*text
)
924 m_textctrlWrapper
= NULL
;
926 // SetFocusIgnoringChildren();
929 bool wxDataViewMainWindow::RowAppended()
934 bool wxDataViewMainWindow::RowPrepended()
939 bool wxDataViewMainWindow::RowInserted( size_t WXUNUSED(before
) )
944 bool wxDataViewMainWindow::RowDeleted( size_t WXUNUSED(row
) )
949 bool wxDataViewMainWindow::RowChanged( size_t WXUNUSED(row
) )
954 bool wxDataViewMainWindow::ValueChanged( size_t WXUNUSED(col
), size_t row
)
956 wxRect
rect( 0, row
*m_lineHeight
, 10000, m_lineHeight
);
957 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
958 Refresh( true, &rect
);
963 bool wxDataViewMainWindow::RowsReordered( size_t *WXUNUSED(new_order
) )
970 bool wxDataViewMainWindow::Cleared()
975 void wxDataViewMainWindow::UpdateDisplay()
980 void wxDataViewMainWindow::OnInternalIdle()
982 wxWindow::OnInternalIdle();
986 RecalculateDisplay();
991 void wxDataViewMainWindow::RecalculateDisplay()
993 wxDataViewListModel
*model
= GetOwner()->GetModel();
1001 size_t cols
= GetOwner()->GetNumberOfColumns();
1003 for (i
= 0; i
< cols
; i
++)
1005 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1006 width
+= col
->GetWidth();
1009 int height
= model
->GetNumberOfRows() * m_lineHeight
;
1011 SetVirtualSize( width
, height
);
1012 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1017 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1019 wxWindow::ScrollWindow( dx
, dy
, rect
);
1020 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1023 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1025 wxPaintDC
dc( this );
1027 GetOwner()->PrepareDC( dc
);
1029 dc
.SetFont( GetFont() );
1031 wxRect update
= GetUpdateRegion().GetBox();
1032 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1034 wxDataViewListModel
*model
= GetOwner()->GetModel();
1036 size_t item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1037 size_t item_count
= wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1038 (int)(model
->GetNumberOfRows()-item_start
) );
1043 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1045 if (m_selection
.Index( item
) != wxNOT_FOUND
)
1047 int flags
= wxCONTROL_SELECTED
;
1048 if (item
== m_currentRow
)
1049 flags
|= wxCONTROL_CURRENT
;
1051 flags
|= wxCONTROL_FOCUSED
;
1052 wxRect
rect( 0, item
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1053 wxRendererNative::Get().DrawItemSelectionRect
1063 if (item
== m_currentRow
)
1065 int flags
= wxCONTROL_CURRENT
;
1067 flags
|= wxCONTROL_FOCUSED
; // should have no effect
1068 wxRect
rect( 0, item
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1069 wxRendererNative::Get().DrawItemSelectionRect
1083 cell_rect
.height
= m_lineHeight
;
1084 size_t cols
= GetOwner()->GetNumberOfColumns();
1086 for (i
= 0; i
< cols
; i
++)
1088 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1089 wxDataViewCell
*cell
= col
->GetCell();
1090 cell_rect
.width
= col
->GetWidth();
1092 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1094 cell_rect
.y
= item
*m_lineHeight
;
1096 model
->GetValue( value
, col
->GetModelColumn(), item
);
1097 cell
->SetValue( value
);
1098 wxSize size
= cell
->GetSize();
1099 // cannot be bigger than allocated space
1100 size
.x
= wxMin( size
.x
, cell_rect
.width
);
1101 size
.y
= wxMin( size
.y
, cell_rect
.height
);
1102 // TODO: check for left/right/centre alignment here
1105 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1106 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1108 item_rect
.width
= size
.x
;
1109 item_rect
.height
= size
.y
;
1110 cell
->Render( item_rect
, &dc
, 0 );
1113 cell_rect
.x
+= cell_rect
.width
;
1117 int wxDataViewMainWindow::GetCountPerPage()
1119 wxSize size
= GetClientSize();
1120 return size
.y
/ m_lineHeight
;
1123 int wxDataViewMainWindow::GetEndOfLastCol()
1127 for (i
= 0; i
< GetOwner()->GetNumberOfColumns(); i
++)
1129 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1130 width
+= c
->GetWidth();
1135 size_t wxDataViewMainWindow::GetFirstVisibleRow()
1139 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1141 return y
/ m_lineHeight
;
1144 size_t wxDataViewMainWindow::GetLastVisibleRow()
1146 wxSize client_size
= GetClientSize();
1147 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
, &client_size
.x
, &client_size
.y
);
1149 return wxMin( GetRowCount()-1, ((unsigned)client_size
.y
/m_lineHeight
)+1 );
1152 size_t wxDataViewMainWindow::GetRowCount()
1154 return GetOwner()->GetModel()->GetNumberOfRows();
1157 void wxDataViewMainWindow::ChangeCurrentRow( size_t row
)
1164 void wxDataViewMainWindow::SelectAllRows( bool on
)
1171 m_selection
.Clear();
1172 for (size_t i
= 0; i
< GetRowCount(); i
++)
1173 m_selection
.Add( i
);
1178 size_t first_visible
= GetFirstVisibleRow();
1179 size_t last_visible
= GetLastVisibleRow();
1181 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1183 size_t row
= m_selection
[i
];
1184 if ((row
>= first_visible
) && (row
<= last_visible
))
1187 m_selection
.Clear();
1191 void wxDataViewMainWindow::SelectRow( size_t row
, bool on
)
1193 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1197 m_selection
.Add( row
);
1205 m_selection
.Remove( row
);
1211 void wxDataViewMainWindow::SelectRows( size_t from
, size_t to
, bool on
)
1221 for (i
= from
; i
<= to
; i
++)
1223 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1226 m_selection
.Add( i
);
1231 m_selection
.Remove( i
);
1234 RefreshRows( from
, to
);
1237 void wxDataViewMainWindow::ReverseRowSelection( size_t row
)
1239 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1240 m_selection
.Add( row
);
1242 m_selection
.Remove( row
);
1246 bool wxDataViewMainWindow::IsRowSelected( size_t row
)
1248 return (m_selection
.Index( row
) != wxNOT_FOUND
);
1251 void wxDataViewMainWindow::RefreshRow( size_t row
)
1253 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
1254 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1256 wxSize client_size
= GetClientSize();
1257 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1258 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1259 if (intersect_rect
.width
> 0)
1260 Refresh( true, &intersect_rect
);
1263 void wxDataViewMainWindow::RefreshRows( size_t from
, size_t to
)
1272 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
1273 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1275 wxSize client_size
= GetClientSize();
1276 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1277 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1278 if (intersect_rect
.width
> 0)
1279 Refresh( true, &intersect_rect
);
1282 void wxDataViewMainWindow::RefreshRowsAfter( size_t firstRow
)
1284 size_t count
= GetRowCount();
1285 if (firstRow
> count
)
1288 wxRect
rect( 0, firstRow
*m_lineHeight
, GetEndOfLastCol(), count
* m_lineHeight
);
1289 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1291 wxSize client_size
= GetClientSize();
1292 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1293 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1294 if (intersect_rect
.width
> 0)
1295 Refresh( true, &intersect_rect
);
1298 void wxDataViewMainWindow::OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
)
1300 wxCHECK_RET( newCurrent
< GetRowCount(),
1301 _T("invalid item index in OnArrowChar()") );
1303 // if there is no selection, we cannot move it anywhere
1304 if (!HasCurrentRow())
1307 size_t oldCurrent
= m_currentRow
;
1309 // in single selection we just ignore Shift as we can't select several
1311 if ( event
.ShiftDown() && !IsSingleSel() )
1313 RefreshRow( oldCurrent
);
1315 ChangeCurrentRow( newCurrent
);
1317 // select all the items between the old and the new one
1318 if ( oldCurrent
> newCurrent
)
1320 newCurrent
= oldCurrent
;
1321 oldCurrent
= m_currentRow
;
1324 SelectRows( oldCurrent
, newCurrent
, true );
1328 RefreshRow( oldCurrent
);
1330 // all previously selected items are unselected unless ctrl is held
1331 if ( !event
.ControlDown() )
1332 SelectAllRows(false);
1334 ChangeCurrentRow( newCurrent
);
1336 if ( !event
.ControlDown() )
1337 SelectRow( m_currentRow
, true );
1339 RefreshRow( m_currentRow
);
1345 void wxDataViewMainWindow::OnChar( wxKeyEvent
&event
)
1347 if (event
.GetKeyCode() == WXK_TAB
)
1349 wxNavigationKeyEvent nevent
;
1350 nevent
.SetWindowChange( event
.ControlDown() );
1351 nevent
.SetDirection( !event
.ShiftDown() );
1352 nevent
.SetEventObject( GetParent()->GetParent() );
1353 nevent
.SetCurrentFocus( m_parent
);
1354 if (GetParent()->GetParent()->GetEventHandler()->ProcessEvent( nevent
))
1358 // no item -> nothing to do
1359 if (!HasCurrentRow())
1365 // don't use m_linesPerPage directly as it might not be computed yet
1366 const int pageSize
= GetCountPerPage();
1367 wxCHECK_RET( pageSize
, _T("should have non zero page size") );
1369 switch ( event
.GetKeyCode() )
1372 if ( m_currentRow
> 0 )
1373 OnArrowChar( m_currentRow
- 1, event
);
1377 if ( m_currentRow
< GetRowCount() - 1 )
1378 OnArrowChar( m_currentRow
+ 1, event
);
1383 OnArrowChar( GetRowCount() - 1, event
);
1388 OnArrowChar( 0, event
);
1393 int steps
= pageSize
- 1;
1394 int index
= m_currentRow
- steps
;
1398 OnArrowChar( index
, event
);
1404 int steps
= pageSize
- 1;
1405 size_t index
= m_currentRow
+ steps
;
1406 size_t count
= GetRowCount();
1407 if ( index
>= count
)
1410 OnArrowChar( index
, event
);
1419 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
1421 if (event
.GetEventType() == wxEVT_MOUSEWHEEL
)
1423 // let the base handle mouse wheel events.
1428 int x
= event
.GetX();
1429 int y
= event
.GetY();
1430 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1432 wxDataViewColumn
*col
= NULL
;
1435 size_t cols
= GetOwner()->GetNumberOfColumns();
1437 for (i
= 0; i
< cols
; i
++)
1439 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1440 if (x
< xpos
+ c
->GetWidth())
1445 xpos
+= c
->GetWidth();
1449 wxDataViewCell
*cell
= col
->GetCell();
1451 size_t current
= y
/ m_lineHeight
;
1453 if ((current
> GetRowCount()) || (x
> GetEndOfLastCol()))
1455 // Unselect all if below the last row ?
1459 wxDataViewListModel
*model
= GetOwner()->GetModel();
1461 if (event
.Dragging())
1463 if (m_dragCount
== 0)
1465 // we have to report the raw, physical coords as we want to be
1466 // able to call HitTest(event.m_pointDrag) from the user code to
1467 // get the item being dragged
1468 m_dragStart
= event
.GetPosition();
1473 if (m_dragCount
!= 3)
1476 if (event
.LeftIsDown())
1478 // Notify cell about drag
1487 bool forceClick
= false;
1489 if (event
.ButtonDClick())
1491 m_renameTimer
->Stop();
1492 m_lastOnSame
= false;
1495 if (event
.LeftDClick())
1497 if ( current
== m_lineLastClicked
)
1499 if (cell
->GetMode() == wxDATAVIEW_CELL_ACTIVATABLE
)
1502 model
->GetValue( value
, col
->GetModelColumn(), current
);
1503 cell
->SetValue( value
);
1504 wxRect
cell_rect( xpos
, current
* m_lineHeight
, col
->GetWidth(), m_lineHeight
);
1505 cell
->Activate( cell_rect
, model
, col
->GetModelColumn(), current
);
1511 // The first click was on another item, so don't interpret this as
1512 // a double click, but as a simple click instead
1519 if (m_lineSelectSingleOnUp
!= (size_t)-1)
1521 // select single line
1522 SelectAllRows( false );
1523 SelectRow( m_lineSelectSingleOnUp
, true );
1528 if ((col
== m_currentCol
) && (current
== m_currentRow
) &&
1529 (cell
->GetMode() == wxDATAVIEW_CELL_EDITABLE
) )
1531 m_renameTimer
->Start( 100, true );
1535 m_lastOnSame
= false;
1536 m_lineSelectSingleOnUp
= (size_t)-1;
1540 // This is necessary, because after a DnD operation in
1541 // from and to ourself, the up event is swallowed by the
1542 // DnD code. So on next non-up event (which means here and
1543 // now) m_lineSelectSingleOnUp should be reset.
1544 m_lineSelectSingleOnUp
= (size_t)-1;
1547 if (event
.RightDown())
1549 m_lineBeforeLastClicked
= m_lineLastClicked
;
1550 m_lineLastClicked
= current
;
1552 // If the item is already selected, do not update the selection.
1553 // Multi-selections should not be cleared if a selected item is clicked.
1554 if (!IsRowSelected(current
))
1556 SelectAllRows(false);
1557 ChangeCurrentRow(current
);
1558 SelectRow(m_currentRow
,true);
1561 // notify cell about right click
1564 // Allow generation of context menu event
1567 else if (event
.MiddleDown())
1569 // notify cell about middle click
1572 if (event
.LeftDown() || forceClick
)
1578 m_lineBeforeLastClicked
= m_lineLastClicked
;
1579 m_lineLastClicked
= current
;
1581 size_t oldCurrentRow
= m_currentRow
;
1582 bool oldWasSelected
= IsRowSelected(m_currentRow
);
1584 bool cmdModifierDown
= event
.CmdDown();
1585 if ( IsSingleSel() || !(cmdModifierDown
|| event
.ShiftDown()) )
1587 if ( IsSingleSel() || !IsRowSelected(current
) )
1589 SelectAllRows( false );
1591 ChangeCurrentRow(current
);
1593 SelectRow(m_currentRow
,true);
1595 else // multi sel & current is highlighted & no mod keys
1597 m_lineSelectSingleOnUp
= current
;
1598 ChangeCurrentRow(current
); // change focus
1601 else // multi sel & either ctrl or shift is down
1603 if (cmdModifierDown
)
1605 ChangeCurrentRow(current
);
1607 ReverseRowSelection(m_currentRow
);
1609 else if (event
.ShiftDown())
1611 ChangeCurrentRow(current
);
1613 size_t lineFrom
= oldCurrentRow
,
1616 if ( lineTo
< lineFrom
)
1619 lineFrom
= m_currentRow
;
1622 SelectRows(lineFrom
, lineTo
, true);
1624 else // !ctrl, !shift
1626 // test in the enclosing if should make it impossible
1627 wxFAIL_MSG( _T("how did we get here?") );
1631 if (m_currentRow
!= oldCurrentRow
)
1632 RefreshRow( oldCurrentRow
);
1634 wxDataViewColumn
*oldCurrentCol
= m_currentCol
;
1636 // Update selection here...
1639 m_lastOnSame
= !forceClick
&& ((col
== oldCurrentCol
) && (current
== oldCurrentRow
)) && oldWasSelected
;
1643 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
1647 if (HasCurrentRow())
1653 void wxDataViewMainWindow::OnKillFocus( wxFocusEvent
&event
)
1657 if (HasCurrentRow())
1663 //-----------------------------------------------------------------------------
1665 //-----------------------------------------------------------------------------
1667 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
1669 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
1670 EVT_SIZE(wxDataViewCtrl::OnSize
)
1673 wxDataViewCtrl::~wxDataViewCtrl()
1676 GetModel()->RemoveNotifier( m_notifier
);
1679 void wxDataViewCtrl::Init()
1684 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
1685 const wxPoint
& pos
, const wxSize
& size
,
1686 long style
, const wxValidator
& validator
)
1688 if (!wxControl::Create( parent
, id
, pos
, size
, style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
1694 MacSetClipChildren( true ) ;
1697 m_clientArea
= new wxDataViewMainWindow( this, wxID_ANY
);
1699 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,22) );
1701 m_headerArea
= new wxDataViewHeaderWindow( this, wxID_ANY
, wxDefaultPosition
, wxSize(wxDefaultCoord
,25) );
1704 SetTargetWindow( m_clientArea
);
1706 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
1707 sizer
->Add( m_headerArea
, 0, wxGROW
);
1708 sizer
->Add( m_clientArea
, 1, wxGROW
);
1715 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
1719 WXLRESULT rc
= wxDataViewCtrlBase::MSWWindowProc(nMsg
, wParam
, lParam
);
1722 // we need to process arrows ourselves for scrolling
1723 if ( nMsg
== WM_GETDLGCODE
)
1725 rc
|= DLGC_WANTARROWS
;
1733 void wxDataViewCtrl::OnSize( wxSizeEvent
&WXUNUSED(event
) )
1735 // We need to override OnSize so that our scrolled
1736 // window a) does call Layout() to use sizers for
1737 // positioning the controls but b) does not query
1738 // the sizer for their size and use that for setting
1739 // the scrollable area as set that ourselves by
1740 // calling SetScrollbar() further down.
1747 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
1749 if (!wxDataViewCtrlBase::AssociateModel( model
))
1752 m_notifier
= new wxGenericDataViewListModelNotifier( m_clientArea
);
1754 model
->AddNotifier( m_notifier
);
1756 m_clientArea
->UpdateDisplay();
1761 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
1763 if (!wxDataViewCtrlBase::AppendColumn(col
))
1766 m_clientArea
->UpdateDisplay();
1772 // !wxUSE_GENERICDATAVIEWCTRL
1775 // wxUSE_DATAVIEWCTRL