1 /////////////////////////////////////////////////////////////////////////////
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"
15 #if wxUSE_DATAVIEWCTRL
17 #include "wx/dataview.h"
19 #ifdef wxUSE_GENERICDATAVIEWCTRL
21 #include "wx/stockitem.h"
22 #include "wx/dcclient.h"
23 #include "wx/calctrl.h"
24 #include "wx/popupwin.h"
27 #include "wx/renderer.h"
30 #include <windows.h> // for DLGC_WANTARROWS
31 #include "wx/msw/winundef.h"
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
41 // wxDataViewHeaderWindow
42 //-----------------------------------------------------------------------------
44 class wxDataViewHeaderWindow
: public wxWindow
47 wxDataViewHeaderWindow( wxDataViewCtrl
*parent
,
49 const wxPoint
&pos
= wxDefaultPosition
,
50 const wxSize
&size
= wxDefaultSize
,
51 const wxString
&name
= wxT("wxdataviewctrlheaderwindow") );
52 ~wxDataViewHeaderWindow();
54 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
55 wxDataViewCtrl
*GetOwner() { return m_owner
; }
57 void OnPaint( wxPaintEvent
&event
);
58 void OnMouse( wxMouseEvent
&event
);
59 void OnSetFocus( wxFocusEvent
&event
);
62 wxDataViewCtrl
*m_owner
;
63 wxCursor
*m_resizeCursor
;
66 DECLARE_DYNAMIC_CLASS(wxDataViewHeaderWindow
)
70 //-----------------------------------------------------------------------------
71 // wxDataViewMainWindow
72 //-----------------------------------------------------------------------------
74 class wxDataViewMainWindow
: public wxWindow
77 wxDataViewMainWindow( wxDataViewCtrl
*parent
,
79 const wxPoint
&pos
= wxDefaultPosition
,
80 const wxSize
&size
= wxDefaultSize
,
81 const wxString
&name
= wxT("wxdataviewctrlmainwindow") );
82 ~wxDataViewMainWindow();
84 // notifications from wxDataViewListModel
87 bool RowInserted( size_t before
);
88 bool RowDeleted( size_t row
);
89 bool RowChanged( size_t row
);
90 bool ValueChanged( size_t col
, size_t row
);
91 bool RowsReordered( size_t *new_order
);
94 void SetOwner( wxDataViewCtrl
* owner
) { m_owner
= owner
; }
95 wxDataViewCtrl
*GetOwner() { return m_owner
; }
97 void OnPaint( wxPaintEvent
&event
);
98 void OnMouse( wxMouseEvent
&event
);
99 void OnSetFocus( wxFocusEvent
&event
);
101 void UpdateDisplay();
102 void RecalculateDisplay();
103 void OnInternalIdle();
105 void ScrollWindow( int dx
, int dy
, const wxRect
*rect
);
107 wxDataViewCtrl
*m_owner
;
112 DECLARE_DYNAMIC_CLASS(wxDataViewMainWindow
)
113 DECLARE_EVENT_TABLE()
116 // ---------------------------------------------------------
117 // wxGenericDataViewListModelNotifier
118 // ---------------------------------------------------------
120 class wxGenericDataViewListModelNotifier
: public wxDataViewListModelNotifier
123 wxGenericDataViewListModelNotifier( wxDataViewMainWindow
*mainWindow
)
124 { m_mainWindow
= mainWindow
; }
126 virtual bool RowAppended()
127 { return m_mainWindow
->RowAppended(); }
128 virtual bool RowPrepended()
129 { return m_mainWindow
->RowPrepended(); }
130 virtual bool RowInserted( size_t before
)
131 { return m_mainWindow
->RowInserted( before
); }
132 virtual bool RowDeleted( size_t row
)
133 { return m_mainWindow
->RowDeleted( row
); }
134 virtual bool RowChanged( size_t row
)
135 { return m_mainWindow
->RowChanged( row
); }
136 virtual bool ValueChanged( size_t col
, size_t row
)
137 { return m_mainWindow
->ValueChanged( col
, row
); }
138 virtual bool RowsReordered( size_t *new_order
)
139 { return m_mainWindow
->RowsReordered( new_order
); }
140 virtual bool Cleared()
141 { return m_mainWindow
->Cleared(); }
143 wxDataViewMainWindow
*m_mainWindow
;
146 // ---------------------------------------------------------
148 // ---------------------------------------------------------
150 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCell
, wxDataViewCellBase
)
152 wxDataViewCell::wxDataViewCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
153 wxDataViewCellBase( varianttype
, mode
)
158 wxDataViewCell::~wxDataViewCell()
164 wxDC
*wxDataViewCell::GetDC()
168 if (GetOwner() == NULL
)
170 if (GetOwner()->GetOwner() == NULL
)
172 m_dc
= new wxClientDC( GetOwner()->GetOwner() );
178 // ---------------------------------------------------------
179 // wxDataViewCustomCell
180 // ---------------------------------------------------------
182 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCustomCell
, wxDataViewCell
)
184 wxDataViewCustomCell::wxDataViewCustomCell( const wxString
&varianttype
,
185 wxDataViewCellMode mode
) :
186 wxDataViewCell( varianttype
, mode
)
191 // ---------------------------------------------------------
192 // wxDataViewTextCell
193 // ---------------------------------------------------------
195 IMPLEMENT_ABSTRACT_CLASS(wxDataViewTextCell
, wxDataViewCustomCell
)
197 wxDataViewTextCell::wxDataViewTextCell( const wxString
&varianttype
, wxDataViewCellMode mode
) :
198 wxDataViewCustomCell( varianttype
, mode
)
202 bool wxDataViewTextCell::SetValue( const wxVariant
&value
)
204 m_text
= value
.GetString();
209 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
214 bool wxDataViewTextCell::Render( wxRect cell
, wxDC
*dc
, int state
)
216 dc
->DrawText( m_text
, cell
.x
, cell
.y
);
221 wxSize
wxDataViewTextCell::GetSize()
223 return wxSize(80,20);
226 // ---------------------------------------------------------
227 // wxDataViewToggleCell
228 // ---------------------------------------------------------
230 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCustomCell
)
232 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
233 wxDataViewCellMode mode
) :
234 wxDataViewCustomCell( varianttype
, mode
)
239 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
241 m_toggle
= value
.GetBool();
246 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
251 bool wxDataViewToggleCell::Render( wxRect cell
, wxDC
*dc
, int state
)
253 // User wxRenderer here
255 dc
->SetPen( *wxBLACK_PEN
);
256 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
258 rect
.x
= cell
.x
+ cell
.width
/2 - 10;
260 rect
.y
= cell
.y
+ cell
.height
/2 - 10;
262 dc
->DrawRectangle( rect
);
269 dc
->DrawLine( rect
.x
, rect
.y
, rect
.x
+rect
.width
, rect
.y
+rect
.height
);
270 dc
->DrawLine( rect
.x
+rect
.width
, rect
.y
, rect
.x
, rect
.y
+rect
.height
);
276 wxSize
wxDataViewToggleCell::GetSize()
278 return wxSize(20,20);
281 // ---------------------------------------------------------
282 // wxDataViewProgressCell
283 // ---------------------------------------------------------
285 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
287 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
288 const wxString
&varianttype
, wxDataViewCellMode mode
) :
289 wxDataViewCustomCell( varianttype
, mode
)
295 wxDataViewProgressCell::~wxDataViewProgressCell()
299 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
301 m_value
= (long) value
;
303 if (m_value
< 0) m_value
= 0;
304 if (m_value
> 100) m_value
= 100;
309 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
311 double pct
= (double)m_value
/ 100.0;
313 bar
.width
= (int)(cell
.width
* pct
);
314 dc
->SetPen( *wxTRANSPARENT_PEN
);
315 dc
->SetBrush( *wxBLUE_BRUSH
);
316 dc
->DrawRectangle( bar
);
318 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
319 dc
->SetPen( *wxBLACK_PEN
);
320 dc
->DrawRectangle( cell
);
325 wxSize
wxDataViewProgressCell::GetSize()
327 return wxSize(40,12);
330 // ---------------------------------------------------------
331 // wxDataViewDateCell
332 // ---------------------------------------------------------
334 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
337 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
338 wxDataViewListModel
*model
, size_t col
, size_t row
) :
339 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
344 m_cal
= new wxCalendarCtrl( this, -1, *value
);
345 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
346 sizer
->Add( m_cal
, 1, wxGROW
);
351 virtual void OnDismiss()
355 void OnCalendar( wxCalendarEvent
&event
);
357 wxCalendarCtrl
*m_cal
;
358 wxDataViewListModel
*m_model
;
363 DECLARE_EVENT_TABLE()
366 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
367 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
370 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
372 wxDateTime date
= event
.GetDate();
373 wxVariant value
= date
;
374 m_model
->SetValue( value
, m_col
, m_row
);
375 m_model
->ValueChanged( m_col
, m_row
);
379 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
381 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
382 wxDataViewCellMode mode
) :
383 wxDataViewCustomCell( varianttype
, mode
)
387 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
389 m_date
= value
.GetDateTime();
394 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
396 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
397 wxString tmp
= m_date
.FormatDate();
398 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
403 wxSize
wxDataViewDateCell::GetSize()
405 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
406 wxString tmp
= m_date
.FormatDate();
408 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
409 return wxSize(x
,y
+d
);
412 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
415 model
->GetValue( variant
, col
, row
);
416 wxDateTime value
= variant
.GetDateTime();
418 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
419 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
420 wxPoint pos
= wxGetMousePosition();
423 popup
->Popup( popup
->m_cal
);
428 // ---------------------------------------------------------
430 // ---------------------------------------------------------
432 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
434 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
435 size_t model_column
, int flags
) :
436 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
441 wxDataViewColumn::~wxDataViewColumn()
445 void wxDataViewColumn::SetTitle( const wxString
&title
)
447 wxDataViewColumnBase::SetTitle( title
);
451 //-----------------------------------------------------------------------------
452 // wxDataViewHeaderWindow
453 //-----------------------------------------------------------------------------
455 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow
, wxWindow
)
457 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
,wxWindow
)
458 EVT_PAINT (wxDataViewHeaderWindow::OnPaint
)
459 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse
)
460 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus
)
463 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
464 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
465 wxWindow( parent
, id
, pos
, size
, 0, name
)
469 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
471 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
472 SetOwnForegroundColour( attr
.colFg
);
473 SetOwnBackgroundColour( attr
.colBg
);
475 SetOwnFont( attr
.font
);
478 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
480 delete m_resizeCursor
;
483 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent
&event
)
486 GetClientSize( &w
, &h
);
488 wxPaintDC
dc( this );
491 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
494 m_owner
->GetViewStart( &x
, NULL
);
496 // account for the horz scrollbar offset
497 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
499 dc
.SetFont( GetFont() );
501 size_t cols
= GetOwner()->GetNumberOfColumns();
504 for (i
= 0; i
< cols
; i
++)
506 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
507 int width
= col
->GetWidth();
509 // the width of the rect to draw: make it smaller to fit entirely
510 // inside the column rect
519 wxRendererNative::Get().DrawHeaderButton
523 wxRect(xpos
, 0, cw
, ch
),
524 m_parent
->IsEnabled() ? 0
525 : (int)wxCONTROL_DISABLED
528 dc
.DrawText( col
->GetTitle(), xpos
+3, 3 );
534 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
538 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
543 //-----------------------------------------------------------------------------
544 // wxDataViewMainWindow
545 //-----------------------------------------------------------------------------
547 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
549 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
550 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
551 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
552 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
555 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
556 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
557 wxWindow( parent
, id
, pos
, size
, 0, name
)
561 // We need to calculate this smartly..
567 wxDataViewMainWindow::~wxDataViewMainWindow()
571 bool wxDataViewMainWindow::RowAppended()
576 bool wxDataViewMainWindow::RowPrepended()
581 bool wxDataViewMainWindow::RowInserted( size_t before
)
586 bool wxDataViewMainWindow::RowDeleted( size_t row
)
591 bool wxDataViewMainWindow::RowChanged( size_t row
)
596 bool wxDataViewMainWindow::ValueChanged( size_t col
, size_t row
)
601 bool wxDataViewMainWindow::RowsReordered( size_t *new_order
)
606 bool wxDataViewMainWindow::Cleared()
611 void wxDataViewMainWindow::UpdateDisplay()
616 void wxDataViewMainWindow::OnInternalIdle()
618 wxWindow::OnInternalIdle();
622 RecalculateDisplay();
627 void wxDataViewMainWindow::RecalculateDisplay()
629 wxDataViewListModel
*model
= GetOwner()->GetModel();
637 size_t cols
= GetOwner()->GetNumberOfColumns();
639 for (i
= 0; i
< cols
; i
++)
641 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
642 width
+= col
->GetWidth();
645 int height
= model
->GetNumberOfRows() * m_lineHeight
;
647 SetVirtualSize( width
, height
);
648 GetOwner()->SetScrollRate( 10, m_lineHeight
);
653 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
655 wxWindow::ScrollWindow( dx
, dy
, rect
);
656 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
659 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&event
)
661 wxPaintDC
dc( this );
663 GetOwner()->PrepareDC( dc
);
665 dc
.SetFont( GetFont() );
667 wxRect update
= GetUpdateRegion().GetBox();
668 m_owner
->CalcUnscrolledPosition( update
.x
, update
.y
, &update
.x
, &update
.y
);
670 wxDataViewListModel
*model
= GetOwner()->GetModel();
672 size_t item_start
= update
.y
/ m_lineHeight
;
673 size_t item_count
= (update
.height
/ m_lineHeight
) + 1;
677 cell_rect
.height
= m_lineHeight
;
678 size_t cols
= GetOwner()->GetNumberOfColumns();
680 for (i
= 0; i
< cols
; i
++)
682 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
683 wxDataViewCell
*cell
= col
->GetCell();
684 cell_rect
.width
= col
->GetWidth();
687 for (item
= item_start
; item
<= item_start
+item_count
; item
++)
689 cell_rect
.y
= item
*m_lineHeight
;
691 model
->GetValue( value
, col
->GetModelColumn(), item
);
692 cell
->SetValue( value
);
693 cell
->Render( cell_rect
, &dc
, 0 );
696 cell_rect
.x
+= cell_rect
.width
;
700 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
705 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
710 //-----------------------------------------------------------------------------
712 //-----------------------------------------------------------------------------
714 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
716 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
717 EVT_SIZE(wxDataViewCtrl::OnSize
)
720 wxDataViewCtrl::~wxDataViewCtrl()
723 GetModel()->RemoveNotifier( m_notifier
);
726 void wxDataViewCtrl::Init()
731 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
732 const wxPoint
& pos
, const wxSize
& size
,
733 long style
, const wxValidator
& validator
)
735 if (!wxControl::Create( parent
, id
, pos
, size
, style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
741 MacSetClipChildren( true ) ;
744 m_clientArea
= new wxDataViewMainWindow( this, -1 );
745 m_headerArea
= new wxDataViewHeaderWindow( this, -1, wxDefaultPosition
, wxSize(-1,25) );
747 SetTargetWindow( m_clientArea
);
749 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
750 sizer
->Add( m_headerArea
, 0, wxGROW
);
751 sizer
->Add( m_clientArea
, 1, wxGROW
);
758 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
762 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
765 // we need to process arrows ourselves for scrolling
766 if ( nMsg
== WM_GETDLGCODE
)
768 rc
|= DLGC_WANTARROWS
;
776 void wxDataViewCtrl::OnSize( wxSizeEvent
&event
)
778 // We need to override OnSize so that our scrolled
779 // window a) does call Layout() to use sizers for
780 // positioning the controls but b) does not query
781 // the sizer for their size and use that for setting
782 // the scrollable area as set that ourselves by
783 // calling SetScrollbar() further down.
790 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
792 if (!wxDataViewCtrlBase::AssociateModel( model
))
795 m_notifier
= new wxGenericDataViewListModelNotifier( m_clientArea
);
797 model
->AddNotifier( m_notifier
);
799 m_clientArea
->UpdateDisplay();
804 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
806 if (!wxDataViewCtrlBase::AppendColumn(col
))
809 m_clientArea
->UpdateDisplay();
815 // !wxUSE_GENERICDATAVIEWCTRL
818 // wxUSE_DATAVIEWCTRL