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"
13 #if wxUSE_DATAVIEWCTRL
15 #include "wx/dataview.h"
17 #ifdef wxUSE_GENERICDATAVIEWCTRL
24 #include "wx/stockitem.h"
25 #include "wx/dcclient.h"
26 #include "wx/calctrl.h"
27 #include "wx/popupwin.h"
28 #include "wx/renderer.h"
30 #include "wx/settings.h"
33 #include "wx/msw/wrapwin.h"
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 // wxDataViewHeaderWindow
44 //-----------------------------------------------------------------------------
46 class wxDataViewHeaderWindow
: public wxWindow
49 wxDataViewHeaderWindow( wxDataViewCtrl
*parent
,
51 const wxPoint
&pos
= wxDefaultPosition
,
52 const wxSize
&size
= wxDefaultSize
,
53 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") );
54 ~wxDataViewHeaderWindow();
56 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
57 wxDataViewCtrl
*GetOwner() { return m_owner
; }
59 void OnPaint( wxPaintEvent
&event
);
60 void OnMouse( wxMouseEvent
&event
);
61 void OnSetFocus( wxFocusEvent
&event
);
64 wxDataViewCtrl
*m_owner
;
65 wxCursor
*m_resizeCursor
;
68 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow
)
72 //-----------------------------------------------------------------------------
73 // wxDataViewRenameTimer
74 //-----------------------------------------------------------------------------
76 class wxDataViewRenameTimer
: public wxTimer
79 wxDataViewMainWindow
*m_owner
;
82 wxDataViewRenameTimer( wxDataViewMainWindow
*owner
);
86 //-----------------------------------------------------------------------------
87 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
88 //-----------------------------------------------------------------------------
90 class wxDataViewTextCtrlWrapper
: public wxEvtHandler
93 // NB: text must be a valid object but not Create()d yet
94 wxDataViewTextCtrlWrapper( wxDataViewMainWindow
*owner
,
96 wxDataViewListModel
*model
,
97 size_t col
, size_t row
,
100 wxTextCtrl
*GetText() const { return m_text
; }
102 void AcceptChangesAndFinish();
105 void OnChar( wxKeyEvent
&event
);
106 void OnKeyUp( wxKeyEvent
&event
);
107 void OnKillFocus( wxFocusEvent
&event
);
109 bool AcceptChanges();
113 wxDataViewMainWindow
*m_owner
;
115 wxString m_startValue
;
116 wxDataViewListModel
*m_model
;
120 bool m_aboutToFinish
;
122 DECLARE_EVENT_TABLE()
125 //-----------------------------------------------------------------------------
126 // wxDataViewMainWindow
127 //-----------------------------------------------------------------------------
129 WX_DEFINE_SORTED_ARRAY_SIZE_T( size_t, wxDataViewSelection
);
131 class wxDataViewMainWindow
: public wxWindow
134 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
136 const wxPoint
&pos
= wxDefaultPosition
,
137 const wxSize
&size
= wxDefaultSize
,
138 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
139 ~wxDataViewMainWindow();
141 // notifications from wxDataViewListModel
144 bool RowInserted( size_t before
);
145 bool RowDeleted( size_t row
);
146 bool RowChanged( size_t row
);
147 bool ValueChanged( size_t col
, size_t row
);
148 bool RowsReordered( size_t *new_order
);
151 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
152 wxDataViewCtrl
*GetOwner() { return m_owner
; }
154 void OnPaint( wxPaintEvent
&event
);
155 void OnArrowChar(size_t newCurrent
, const wxKeyEvent
& event
);
156 void OnChar( wxKeyEvent
&event
);
157 void OnMouse( wxMouseEvent
&event
);
158 void OnSetFocus( wxFocusEvent
&event
);
159 void OnKillFocus( wxFocusEvent
&event
);
161 void UpdateDisplay();
162 void RecalculateDisplay();
163 void OnInternalIdle();
165 void OnRenameTimer();
166 void FinishEditing( wxTextCtrl
*text
);
168 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
170 bool HasCurrentRow() { return m_currentRow
!= (size_t)-1; }
171 void ChangeCurrentRow( size_t row
);
173 bool IsSingleSel() const { return !GetParent()->HasFlag(wxDV_MULTIPLE
); };
174 bool IsEmpty() { return GetRowCount() == 0; }
176 int GetCountPerPage();
177 int GetEndOfLastCol();
178 size_t GetFirstVisibleRow();
179 size_t GetLastVisibleRow();
182 void SelectAllRows( bool on
);
183 void SelectRow( size_t row
, bool on
);
184 void SelectRows( size_t from
, size_t to
, bool on
);
185 void ReverseRowSelection( size_t row
);
186 bool IsRowSelected( size_t row
);
188 void RefreshRow( size_t row
);
189 void RefreshRows( size_t from
, size_t to
);
190 void RefreshRowsAfter( size_t firstRow
);
193 wxDataViewCtrl
*m_owner
;
197 wxDataViewColumn
*m_currentCol
;
199 wxDataViewSelection m_selection
;
201 wxDataViewRenameTimer
*m_renameTimer
;
202 wxDataViewTextCtrlWrapper
*m_textctrlWrapper
;
205 wxBrush
*m_highlightBrush
,
206 *m_highlightUnfocusedBrush
;
212 // for double click logic
213 size_t m_lineLastClicked
,
214 m_lineBeforeLastClicked
,
215 m_lineSelectSingleOnUp
;
218 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
219 DECLARE_EVENT_TABLE()
222 // ---------------------------------------------------------
223 // wxGenericDataViewListModelNotifier
224 // ---------------------------------------------------------
226 class wxGenericDataViewListModelNotifier
: public wxDataViewListModelNotifier
229 wxGenericDataViewListModelNotifier( wxDataViewMainWindow
*mainWindow
)
230 { m_mainWindow
= mainWindow
; }
232 virtual bool RowAppended()
233 { return m_mainWindow
->RowAppended(); }
234 virtual bool RowPrepended()
235 { return m_mainWindow
->RowPrepended(); }
236 virtual bool RowInserted( size_t before
)
237 { return m_mainWindow
->RowInserted( before
); }
238 virtual bool RowDeleted( size_t row
)
239 { return m_mainWindow
->RowDeleted( row
); }
240 virtual bool RowChanged( size_t row
)
241 { return m_mainWindow
->RowChanged( row
); }
242 virtual bool ValueChanged( size_t col
, size_t row
)
243 { return m_mainWindow
->ValueChanged( col
, row
); }
244 virtual bool RowsReordered( size_t *new_order
)
245 { return m_mainWindow
->RowsReordered( new_order
); }
246 virtual bool Cleared()
247 { return m_mainWindow
->Cleared(); }
249 wxDataViewMainWindow
*m_mainWindow
;
252 // ---------------------------------------------------------
254 // ---------------------------------------------------------
256 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
258 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
259 wxDataViewCellBase( varianttype
, mode
)
264 wxDataViewCell::~wxDataViewCell()
270 wxDC
*wxDataViewCell::GetDC()
274 if (GetOwner() == NULL
)
276 if (GetOwner()->GetOwner() == NULL
)
278 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
284 // ---------------------------------------------------------
285 // wxDataViewCustomCell
286 // ---------------------------------------------------------
288 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
290 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
291 wxDataViewCellMode mode
) :
292 wxDataViewCell( varianttype
, mode
)
296 // ---------------------------------------------------------
297 // wxDataViewTextCell
298 // ---------------------------------------------------------
300 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCustomCell
)
302 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
303 wxDataViewCustomCell( varianttype
, mode
)
307 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
309 m_text
= value
.GetString();
314 bool wxDataViewTextCell::GetValue( wxVariant
& WXUNUSED(value
) )
319 bool wxDataViewTextCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
321 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
326 wxSize
wxDataViewTextCell::GetSize()
328 return wxSize(80,20);
331 // ---------------------------------------------------------
332 // wxDataViewToggleCell
333 // ---------------------------------------------------------
335 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCustomCell
)
337 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
338 wxDataViewCellMode mode
) :
339 wxDataViewCustomCell( varianttype
, mode
)
344 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
346 m_toggle
= value
.GetBool();
351 bool wxDataViewToggleCell::GetValue( wxVariant
&WXUNUSED(value
) )
356 bool wxDataViewToggleCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
358 // User wxRenderer here
361 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
363 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
368 flags
|= wxCONTROL_CHECKED
;
369 if (GetMode() != wxDATAVIEW_CELL_ACTIVATABLE
)
370 flags
|= wxCONTROL_DISABLED
;
372 wxRendererNative::Get().DrawCheckButton(
373 GetOwner()->GetOwner(),
381 bool wxDataViewToggleCell::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
383 bool value
= !m_toggle
;
384 wxVariant variant
= value
;
385 model
->SetValue( variant
, col
, row
);
386 model
->ValueChanged( col
, row
);
390 wxSize
wxDataViewToggleCell::GetSize()
392 return wxSize(20,20);
395 // ---------------------------------------------------------
396 // wxDataViewProgressCell
397 // ---------------------------------------------------------
399 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
401 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
402 const wxString
&varianttype
, wxDataViewCellMode mode
) :
403 wxDataViewCustomCell( varianttype
, mode
)
409 wxDataViewProgressCell::~wxDataViewProgressCell()
413 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
415 m_value
= (long) value
;
417 if (m_value
< 0) m_value
= 0;
418 if (m_value
> 100) m_value
= 100;
423 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
425 double pct
= (double)m_value
/ 100.0;
427 bar
.width
= (int)(cell
.width
* pct
);
428 dc
->SetPen( *wxTRANSPARENT_PEN
);
429 dc
->SetBrush( *wxBLUE_BRUSH
);
430 dc
->DrawRectangle( bar
);
432 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
433 dc
->SetPen( *wxBLACK_PEN
);
434 dc
->DrawRectangle( cell
);
439 wxSize
wxDataViewProgressCell::GetSize()
441 return wxSize(40,12);
444 // ---------------------------------------------------------
445 // wxDataViewDateCell
446 // ---------------------------------------------------------
448 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
451 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
452 wxDataViewListModel
*model
, size_t col
, size_t row
) :
453 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
458 m_cal
= new wxCalendarCtrl( this, wxID_ANY
, *value
);
459 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
460 sizer
->Add( m_cal
, 1, wxGROW
);
465 virtual void OnDismiss()
469 void OnCalendar( wxCalendarEvent
&event
);
471 wxCalendarCtrl
*m_cal
;
472 wxDataViewListModel
*m_model
;
477 DECLARE_EVENT_TABLE()
480 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
481 EVT_CALENDAR( wxID_ANY
, wxDataViewDateCellPopupTransient::OnCalendar
)
484 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
486 wxDateTime date
= event
.GetDate();
487 wxVariant value
= date
;
488 m_model
->SetValue( value
, m_col
, m_row
);
489 m_model
->ValueChanged( m_col
, m_row
);
493 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
495 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
496 wxDataViewCellMode mode
) :
497 wxDataViewCustomCell( varianttype
, mode
)
501 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
503 m_date
= value
.GetDateTime();
508 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int WXUNUSED(state
) )
510 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
511 wxString tmp
= m_date
.FormatDate();
512 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
517 wxSize
wxDataViewDateCell::GetSize()
519 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
520 wxString tmp
= m_date
.FormatDate();
522 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
523 return wxSize(x
,y
+d
);
526 bool wxDataViewDateCell::Activate( wxRect
WXUNUSED(cell
), wxDataViewListModel
*model
, size_t col
, size_t row
)
529 model
->GetValue( variant
, col
, row
);
530 wxDateTime value
= variant
.GetDateTime();
532 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
533 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
534 wxPoint pos
= wxGetMousePosition();
537 popup
->Popup( popup
->m_cal
);
542 // ---------------------------------------------------------
544 // ---------------------------------------------------------
546 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
548 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
, size_t model_column
,
549 int fixed_width
, wxDataViewColumnSizing sizing
, int flags
) :
550 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
554 m_width
= fixed_width
;
555 m_fixedWidth
= fixed_width
;
558 wxDataViewColumn::~wxDataViewColumn()
562 void wxDataViewColumn::SetTitle( const wxString
&title
)
564 wxDataViewColumnBase::SetTitle( title
);
568 int wxDataViewColumn::GetWidth()
573 void wxDataViewColumn::SetFixedWidth( int width
)
575 m_fixedWidth
= width
;
577 if (m_sizing
== wxDATAVIEW_COL_WIDTH_FIXED
)
584 int wxDataViewColumn::GetFixedWidth()
589 //-----------------------------------------------------------------------------
590 // wxDataViewHeaderWindow
591 //-----------------------------------------------------------------------------
593 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow
, wxWindow
)
595 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
,wxWindow
)
596 EVT_PAINT (wxDataViewHeaderWindow::OnPaint
)
597 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse
)
598 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus
)
601 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
602 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
603 wxWindow( parent
, id
, pos
, size
, 0, name
)
607 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
609 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
610 SetOwnForegroundColour( attr
.colFg
);
611 SetOwnBackgroundColour( attr
.colBg
);
613 SetOwnFont( attr
.font
);
616 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
618 delete m_resizeCursor
;
621 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
624 GetClientSize( &w
, &h
);
626 wxPaintDC
dc( this );
629 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
632 m_owner
->GetViewStart( &x
, NULL
);
634 // account for the horz scrollbar offset
635 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
637 dc
.SetFont( GetFont() );
639 size_t cols
= GetOwner()->GetNumberOfColumns();
642 for (i
= 0; i
< cols
; i
++)
644 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
645 int width
= col
->GetWidth();
650 wxRendererNative::Get().DrawHeaderButton
654 wxRect(xpos
, 0, cw
, ch
-1),
655 m_parent
->IsEnabled() ? 0
656 : (int)wxCONTROL_DISABLED
659 dc
.DrawText( col
->GetTitle(), xpos
+3, 3 );
665 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent
&WXUNUSED(event
) )
669 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
671 GetParent()->SetFocus();
675 //-----------------------------------------------------------------------------
676 // wxDataViewRenameTimer
677 //-----------------------------------------------------------------------------
679 wxDataViewRenameTimer::wxDataViewRenameTimer( wxDataViewMainWindow
*owner
)
684 void wxDataViewRenameTimer::Notify()
686 m_owner
->OnRenameTimer();
689 //-----------------------------------------------------------------------------
690 // wxDataViewTextCtrlWrapper: wraps a wxTextCtrl for inline editing
691 //-----------------------------------------------------------------------------
693 BEGIN_EVENT_TABLE(wxDataViewTextCtrlWrapper
, wxEvtHandler
)
694 EVT_CHAR (wxDataViewTextCtrlWrapper::OnChar
)
695 EVT_KEY_UP (wxDataViewTextCtrlWrapper::OnKeyUp
)
696 EVT_KILL_FOCUS (wxDataViewTextCtrlWrapper::OnKillFocus
)
699 wxDataViewTextCtrlWrapper::wxDataViewTextCtrlWrapper(
700 wxDataViewMainWindow
*owner
,
702 wxDataViewListModel
*model
,
703 size_t col
, size_t row
,
713 m_aboutToFinish
= false;
716 model
->GetValue( value
, col
, row
);
717 m_startValue
= value
.GetString();
719 m_owner
->GetOwner()->CalcScrolledPosition(
720 rectLabel
.x
, rectLabel
.y
, &rectLabel
.x
, &rectLabel
.y
);
722 m_text
->Create( owner
, wxID_ANY
, m_startValue
,
723 wxPoint(rectLabel
.x
-2,rectLabel
.y
-2),
724 wxSize(rectLabel
.width
+7,rectLabel
.height
+4) );
727 m_text
->PushEventHandler(this);
730 void wxDataViewTextCtrlWrapper::AcceptChangesAndFinish()
732 m_aboutToFinish
= true;
734 // Notify the owner about the changes
737 // Even if vetoed, close the control (consistent with MSW)
741 void wxDataViewTextCtrlWrapper::OnChar( wxKeyEvent
&event
)
743 switch ( event
.m_keyCode
)
746 AcceptChangesAndFinish();
750 // m_owner->OnRenameCancelled( m_itemEdited );
759 void wxDataViewTextCtrlWrapper::OnKeyUp( wxKeyEvent
&event
)
767 // auto-grow the textctrl
768 wxSize parentSize
= m_owner
->GetSize();
769 wxPoint myPos
= m_text
->GetPosition();
770 wxSize mySize
= m_text
->GetSize();
772 m_text
->GetTextExtent(m_text
->GetValue() + _T("MM"), &sx
, &sy
);
773 if (myPos
.x
+ sx
> parentSize
.x
)
774 sx
= parentSize
.x
- myPos
.x
;
777 m_text
->SetSize(sx
, wxDefaultCoord
);
782 void wxDataViewTextCtrlWrapper::OnKillFocus( wxFocusEvent
&event
)
784 if ( !m_finished
&& !m_aboutToFinish
)
787 //if ( !AcceptChanges() )
788 // m_owner->OnRenameCancelled( m_itemEdited );
793 // We must let the native text control handle focus
797 bool wxDataViewTextCtrlWrapper::AcceptChanges()
799 const wxString value
= m_text
->GetValue();
801 if ( value
== m_startValue
)
802 // nothing changed, always accept
805 // if ( !m_owner->OnRenameAccept(m_itemEdited, value) )
806 // vetoed by the user
809 // accepted, do rename the item
812 m_model
->SetValue( variant
, m_col
, m_row
);
813 m_model
->ValueChanged( m_col
, m_row
);
818 void wxDataViewTextCtrlWrapper::Finish()
824 m_text
->RemoveEventHandler(this);
825 m_owner
->FinishEditing(m_text
);
828 wxPendingDelete
.Append( this );
832 //-----------------------------------------------------------------------------
833 // wxDataViewMainWindow
834 //-----------------------------------------------------------------------------
836 int LINKAGEMODE
wxDataViewSelectionCmp( size_t row1
, size_t row2
)
838 if (row1
> row2
) return 1;
839 if (row1
== row2
) return 0;
844 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
846 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
847 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
848 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
849 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
850 EVT_KILL_FOCUS (wxDataViewMainWindow::OnKillFocus
)
851 EVT_CHAR (wxDataViewMainWindow::OnChar
)
854 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
855 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
856 wxWindow( parent
, id
, pos
, size
, wxWANTS_CHARS
, name
),
857 m_selection( wxDataViewSelectionCmp
)
862 m_lastOnSame
= false;
863 m_renameTimer
= new wxDataViewRenameTimer( this );
864 m_textctrlWrapper
= NULL
;
866 // TODO: user better initial values/nothing selected
870 // TODO: we need to calculate this smartly
874 m_dragStart
= wxPoint(0,0);
875 m_lineLastClicked
= (size_t) -1;
876 m_lineBeforeLastClicked
= (size_t) -1;
877 m_lineSelectSingleOnUp
= (size_t) -1;
879 m_highlightBrush
= new wxBrush
881 wxSystemSettings::GetColour
883 wxSYS_COLOUR_HIGHLIGHT
888 m_highlightUnfocusedBrush
= new wxBrush
890 wxSystemSettings::GetColour
892 wxSYS_COLOUR_BTNSHADOW
899 SetBackgroundColour( *wxWHITE
);
904 wxDataViewMainWindow::~wxDataViewMainWindow()
906 delete m_renameTimer
;
907 delete m_highlightBrush
;
908 delete m_highlightUnfocusedBrush
;
911 void wxDataViewMainWindow::OnRenameTimer()
913 // We have to call this here because changes may just have
914 // been made and no screen update taken place.
920 size_t cols
= GetOwner()->GetNumberOfColumns();
922 for (i
= 0; i
< cols
; i
++)
924 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
925 if (c
== m_currentCol
)
927 xpos
+= c
->GetWidth();
929 wxRect
labelRect( xpos
, m_currentRow
* m_lineHeight
, m_currentCol
->GetWidth(), m_lineHeight
);
931 wxClassInfo
*textControlClass
= CLASSINFO(wxTextCtrl
);
933 wxTextCtrl
* const text
= (wxTextCtrl
*)textControlClass
->CreateObject();
934 m_textctrlWrapper
= new wxDataViewTextCtrlWrapper(this, text
, GetOwner()->GetModel(),
935 m_currentCol
->GetModelColumn(), m_currentRow
, labelRect
);
938 void wxDataViewMainWindow::FinishEditing( wxTextCtrl
*text
)
941 m_textctrlWrapper
= NULL
;
943 // SetFocusIgnoringChildren();
946 bool wxDataViewMainWindow::RowAppended()
951 bool wxDataViewMainWindow::RowPrepended()
956 bool wxDataViewMainWindow::RowInserted( size_t WXUNUSED(before
) )
961 bool wxDataViewMainWindow::RowDeleted( size_t WXUNUSED(row
) )
966 bool wxDataViewMainWindow::RowChanged( size_t WXUNUSED(row
) )
971 bool wxDataViewMainWindow::ValueChanged( size_t WXUNUSED(col
), size_t row
)
973 wxRect
rect( 0, row
*m_lineHeight
, 10000, m_lineHeight
);
974 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
975 Refresh( true, &rect
);
980 bool wxDataViewMainWindow::RowsReordered( size_t *WXUNUSED(new_order
) )
987 bool wxDataViewMainWindow::Cleared()
992 void wxDataViewMainWindow::UpdateDisplay()
997 void wxDataViewMainWindow::OnInternalIdle()
999 wxWindow::OnInternalIdle();
1003 RecalculateDisplay();
1008 void wxDataViewMainWindow::RecalculateDisplay()
1010 wxDataViewListModel
*model
= GetOwner()->GetModel();
1018 size_t cols
= GetOwner()->GetNumberOfColumns();
1020 for (i
= 0; i
< cols
; i
++)
1022 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1023 width
+= col
->GetWidth();
1026 int height
= model
->GetNumberOfRows() * m_lineHeight
;
1028 SetVirtualSize( width
, height
);
1029 GetOwner()->SetScrollRate( 10, m_lineHeight
);
1034 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
1036 wxWindow::ScrollWindow( dx
, dy
, rect
);
1037 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
1040 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
1042 wxPaintDC
dc( this );
1044 GetOwner()->PrepareDC( dc
);
1046 dc
.SetFont( GetFont() );
1048 wxRect update
= GetUpdateRegion().GetBox();
1049 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
1051 wxDataViewListModel
*model
= GetOwner()->GetModel();
1053 size_t item_start
= wxMax( 0, (update
.y
/ m_lineHeight
) );
1054 size_t item_count
= wxMin( (int)(((update
.y
+ update
.height
) / m_lineHeight
) - item_start
+ 1),
1055 (int)(model
->GetNumberOfRows()-item_start
) );
1059 dc
.SetBrush( *m_highlightBrush
);
1061 dc
.SetBrush( *m_highlightUnfocusedBrush
);
1062 dc
.SetPen( *wxTRANSPARENT_PEN
);
1065 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1067 if (m_selection
.Index( item
) != wxNOT_FOUND
)
1069 wxRect
rect( 0, item
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1070 dc
.DrawRectangle( rect
);
1074 dc
.SetBrush( *wxTRANSPARENT_BRUSH
);
1075 dc
.SetPen( *wxBLACK_PEN
);
1076 if (HasCurrentRow())
1078 wxRect
rect( 0, m_currentRow
*m_lineHeight
+1, GetEndOfLastCol(), m_lineHeight
-2 );
1079 dc
.DrawRectangle( rect
);
1084 cell_rect
.height
= m_lineHeight
;
1085 size_t cols
= GetOwner()->GetNumberOfColumns();
1087 for (i
= 0; i
< cols
; i
++)
1089 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
1090 wxDataViewCell
*cell
= col
->GetCell();
1091 cell_rect
.width
= col
->GetWidth();
1093 for (item
= item_start
; item
< item_start
+item_count
; item
++)
1095 cell_rect
.y
= item
*m_lineHeight
;
1097 model
->GetValue( value
, col
->GetModelColumn(), item
);
1098 cell
->SetValue( value
);
1099 wxSize size
= cell
->GetSize();
1100 // cannot be bigger than allocated space
1101 size
.x
= wxMin( size
.x
, cell_rect
.width
);
1102 size
.y
= wxMin( size
.y
, cell_rect
.height
);
1103 // TODO: check for left/right/centre alignment here
1106 item_rect
.x
= cell_rect
.x
+ (cell_rect
.width
/ 2) - (size
.x
/ 2);
1107 item_rect
.y
= cell_rect
.y
+ (cell_rect
.height
/ 2) - (size
.y
/ 2);
1109 item_rect
.width
= size
.x
;
1110 item_rect
.height
= size
.y
;
1111 cell
->Render( item_rect
, &dc
, 0 );
1114 cell_rect
.x
+= cell_rect
.width
;
1118 int wxDataViewMainWindow::GetCountPerPage()
1120 wxSize size
= GetClientSize();
1121 return size
.y
/ m_lineHeight
;
1124 int wxDataViewMainWindow::GetEndOfLastCol()
1128 for (i
= 0; i
< GetOwner()->GetNumberOfColumns(); i
++)
1130 wxDataViewColumn
*c
= GetOwner()->GetColumn( i
);
1131 width
+= c
->GetWidth();
1136 size_t wxDataViewMainWindow::GetFirstVisibleRow()
1140 m_owner
->CalcUnscrolledPosition( x
, y
, &x
, &y
);
1142 return y
/ m_lineHeight
;
1145 size_t wxDataViewMainWindow::GetLastVisibleRow()
1147 wxSize client_size
= GetClientSize();
1148 m_owner
->CalcUnscrolledPosition( client_size
.x
, client_size
.y
, &client_size
.x
, &client_size
.y
);
1150 return wxMin( GetRowCount()-1, (client_size
.y
/m_lineHeight
)+1 );
1153 int wxDataViewMainWindow::GetRowCount()
1155 return GetOwner()->GetModel()->GetNumberOfRows();
1158 void wxDataViewMainWindow::ChangeCurrentRow( size_t row
)
1165 void wxDataViewMainWindow::SelectAllRows( bool on
)
1167 if (GetRowCount() == 0) return;
1171 m_selection
.Clear();
1173 for (i
= 0; i
< GetRowCount(); i
++)
1174 m_selection
.Add( i
);
1179 size_t first_visible
= GetFirstVisibleRow();
1180 size_t last_visible
= GetLastVisibleRow();
1182 for (i
= 0; i
< m_selection
.GetCount(); i
++)
1184 size_t row
= m_selection
[i
];
1185 if ((row
>= first_visible
) && (row
<= last_visible
))
1188 m_selection
.Clear();
1192 void wxDataViewMainWindow::SelectRow( size_t row
, bool on
)
1194 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1198 m_selection
.Add( row
);
1206 m_selection
.Remove( row
);
1212 void wxDataViewMainWindow::SelectRows( size_t from
, size_t to
, bool on
)
1222 for (i
= from
; i
<= to
; i
++)
1224 if (m_selection
.Index( i
) == wxNOT_FOUND
)
1227 m_selection
.Add( i
);
1232 m_selection
.Remove( i
);
1235 RefreshRows( from
, to
);
1238 void wxDataViewMainWindow::ReverseRowSelection( size_t row
)
1240 if (m_selection
.Index( row
) == wxNOT_FOUND
)
1241 m_selection
.Add( row
);
1243 m_selection
.Remove( row
);
1247 bool wxDataViewMainWindow::IsRowSelected( size_t row
)
1249 return (m_selection
.Index( row
) != wxNOT_FOUND
);
1252 void wxDataViewMainWindow::RefreshRow( size_t row
)
1254 wxRect
rect( 0, row
*m_lineHeight
, GetEndOfLastCol(), m_lineHeight
);
1255 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1257 wxSize client_size
= GetClientSize();
1258 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1259 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1260 if (intersect_rect
.width
> 0)
1261 Refresh( true, &intersect_rect
);
1264 void wxDataViewMainWindow::RefreshRows( size_t from
, size_t to
)
1273 wxRect
rect( 0, from
*m_lineHeight
, GetEndOfLastCol(), (to
-from
+1) * m_lineHeight
);
1274 m_owner
->CalcScrolledPosition( rect
.x
, rect
.y
, &rect
.x
, &rect
.y
);
1276 wxSize client_size
= GetClientSize();
1277 wxRect
client_rect( 0, 0, client_size
.x
, client_size
.y
);
1278 wxRect intersect_rect
= client_rect
.Intersect( rect
);
1279 if (intersect_rect
.width
> 0)
1280 Refresh( true, &intersect_rect
);
1283 void wxDataViewMainWindow::RefreshRowsAfter( size_t firstRow
)
1285 size_t count
= GetRowCount();
1286 if (firstRow
> count
) return;
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
< (size_t)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
< (size_t)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