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
)
207 bool wxDataViewTextCell::GetValue( wxVariant
&value
)
212 bool wxDataViewTextCell::Render( wxRect cell
, wxDC
*dc
, int state
)
217 wxSize
wxDataViewTextCell::GetSize()
219 return wxSize(80,20);
222 // ---------------------------------------------------------
223 // wxDataViewToggleCell
224 // ---------------------------------------------------------
226 IMPLEMENT_ABSTRACT_CLASS(wxDataViewToggleCell
, wxDataViewCustomCell
)
228 wxDataViewToggleCell::wxDataViewToggleCell( const wxString
&varianttype
,
229 wxDataViewCellMode mode
) :
230 wxDataViewCustomCell( varianttype
, mode
)
234 bool wxDataViewToggleCell::SetValue( const wxVariant
&value
)
239 bool wxDataViewToggleCell::GetValue( wxVariant
&value
)
244 bool wxDataViewToggleCell::Render( wxRect cell
, wxDC
*dc
, int state
)
249 wxSize
wxDataViewToggleCell::GetSize()
251 return wxSize(20,20);
254 // ---------------------------------------------------------
255 // wxDataViewProgressCell
256 // ---------------------------------------------------------
258 IMPLEMENT_ABSTRACT_CLASS(wxDataViewProgressCell
, wxDataViewCustomCell
)
260 wxDataViewProgressCell::wxDataViewProgressCell( const wxString
&label
,
261 const wxString
&varianttype
, wxDataViewCellMode mode
) :
262 wxDataViewCustomCell( varianttype
, mode
)
268 wxDataViewProgressCell::~wxDataViewProgressCell()
272 bool wxDataViewProgressCell::SetValue( const wxVariant
&value
)
274 m_value
= (long) value
;
276 if (m_value
< 0) m_value
= 0;
277 if (m_value
> 100) m_value
= 100;
282 bool wxDataViewProgressCell::Render( wxRect cell
, wxDC
*dc
, int state
)
284 double pct
= (double)m_value
/ 100.0;
286 bar
.width
= (int)(cell
.width
* pct
);
287 dc
->SetPen( *wxTRANSPARENT_PEN
);
288 dc
->SetBrush( *wxBLUE_BRUSH
);
289 dc
->DrawRectangle( bar
);
291 dc
->SetBrush( *wxTRANSPARENT_BRUSH
);
292 dc
->SetPen( *wxBLACK_PEN
);
293 dc
->DrawRectangle( cell
);
298 wxSize
wxDataViewProgressCell::GetSize()
300 return wxSize(40,12);
303 // ---------------------------------------------------------
304 // wxDataViewDateCell
305 // ---------------------------------------------------------
307 class wxDataViewDateCellPopupTransient
: public wxPopupTransientWindow
310 wxDataViewDateCellPopupTransient( wxWindow
* parent
, wxDateTime
*value
,
311 wxDataViewListModel
*model
, size_t col
, size_t row
) :
312 wxPopupTransientWindow( parent
, wxBORDER_SIMPLE
)
317 m_cal
= new wxCalendarCtrl( this, -1, *value
);
318 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
319 sizer
->Add( m_cal
, 1, wxGROW
);
324 virtual void OnDismiss()
328 void OnCalendar( wxCalendarEvent
&event
);
330 wxCalendarCtrl
*m_cal
;
331 wxDataViewListModel
*m_model
;
336 DECLARE_EVENT_TABLE()
339 BEGIN_EVENT_TABLE(wxDataViewDateCellPopupTransient
,wxPopupTransientWindow
)
340 EVT_CALENDAR( -1, wxDataViewDateCellPopupTransient::OnCalendar
)
343 void wxDataViewDateCellPopupTransient::OnCalendar( wxCalendarEvent
&event
)
345 wxDateTime date
= event
.GetDate();
346 wxVariant value
= date
;
347 m_model
->SetValue( value
, m_col
, m_row
);
348 m_model
->ValueChanged( m_col
, m_row
);
352 IMPLEMENT_ABSTRACT_CLASS(wxDataViewDateCell
, wxDataViewCustomCell
)
354 wxDataViewDateCell::wxDataViewDateCell( const wxString
&varianttype
,
355 wxDataViewCellMode mode
) :
356 wxDataViewCustomCell( varianttype
, mode
)
360 bool wxDataViewDateCell::SetValue( const wxVariant
&value
)
362 m_date
= value
.GetDateTime();
367 bool wxDataViewDateCell::Render( wxRect cell
, wxDC
*dc
, int state
)
369 dc
->SetFont( GetOwner()->GetOwner()->GetFont() );
370 wxString tmp
= m_date
.FormatDate();
371 dc
->DrawText( tmp
, cell
.x
, cell
.y
);
376 wxSize
wxDataViewDateCell::GetSize()
378 wxDataViewCtrl
* view
= GetOwner()->GetOwner();
379 wxString tmp
= m_date
.FormatDate();
381 view
->GetTextExtent( tmp
, &x
, &y
, &d
);
382 return wxSize(x
,y
+d
);
385 bool wxDataViewDateCell::Activate( wxRect cell
, wxDataViewListModel
*model
, size_t col
, size_t row
)
388 model
->GetValue( variant
, col
, row
);
389 wxDateTime value
= variant
.GetDateTime();
391 wxDataViewDateCellPopupTransient
*popup
= new wxDataViewDateCellPopupTransient(
392 GetOwner()->GetOwner()->GetParent(), &value
, model
, col
, row
);
393 wxPoint pos
= wxGetMousePosition();
396 popup
->Popup( popup
->m_cal
);
401 // ---------------------------------------------------------
403 // ---------------------------------------------------------
405 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumn
, wxDataViewColumnBase
)
407 wxDataViewColumn::wxDataViewColumn( const wxString
&title
, wxDataViewCell
*cell
,
408 size_t model_column
, int flags
) :
409 wxDataViewColumnBase( title
, cell
, model_column
, flags
)
414 wxDataViewColumn::~wxDataViewColumn()
418 void wxDataViewColumn::SetTitle( const wxString
&title
)
420 wxDataViewColumnBase::SetTitle( title
);
424 //-----------------------------------------------------------------------------
425 // wxDataViewHeaderWindow
426 //-----------------------------------------------------------------------------
428 IMPLEMENT_ABSTRACT_CLASS(wxDataViewHeaderWindow
, wxWindow
)
430 BEGIN_EVENT_TABLE(wxDataViewHeaderWindow
,wxWindow
)
431 EVT_PAINT (wxDataViewHeaderWindow::OnPaint
)
432 EVT_MOUSE_EVENTS (wxDataViewHeaderWindow::OnMouse
)
433 EVT_SET_FOCUS (wxDataViewHeaderWindow::OnSetFocus
)
436 wxDataViewHeaderWindow::wxDataViewHeaderWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
437 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
438 wxWindow( parent
, id
, pos
, size
, 0, name
)
442 m_resizeCursor
= new wxCursor( wxCURSOR_SIZEWE
);
444 wxVisualAttributes attr
= wxPanel::GetClassDefaultAttributes();
445 SetOwnForegroundColour( attr
.colFg
);
446 SetOwnBackgroundColour( attr
.colBg
);
448 SetOwnFont( attr
.font
);
451 wxDataViewHeaderWindow::~wxDataViewHeaderWindow()
453 delete m_resizeCursor
;
456 void wxDataViewHeaderWindow::OnPaint( wxPaintEvent
&event
)
459 GetClientSize( &w
, &h
);
461 wxPaintDC
dc( this );
464 m_owner
->GetScrollPixelsPerUnit( &xpix
, NULL
);
467 m_owner
->GetViewStart( &x
, NULL
);
469 // account for the horz scrollbar offset
470 dc
.SetDeviceOrigin( -x
* xpix
, 0 );
472 dc
.SetFont( GetFont() );
474 size_t cols
= GetOwner()->GetNumberOfColumns();
477 for (i
= 0; i
< cols
; i
++)
479 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
480 int width
= col
->GetWidth();
482 // the width of the rect to draw: make it smaller to fit entirely
483 // inside the column rect
492 wxRendererNative::Get().DrawHeaderButton
496 wxRect(xpos
, 0, cw
, ch
),
497 m_parent
->IsEnabled() ? 0
498 : (int)wxCONTROL_DISABLED
501 dc
.DrawText( col
->GetTitle(), xpos
+3, 3 );
507 void wxDataViewHeaderWindow::OnMouse( wxMouseEvent
&event
)
511 void wxDataViewHeaderWindow::OnSetFocus( wxFocusEvent
&event
)
516 //-----------------------------------------------------------------------------
517 // wxDataViewMainWindow
518 //-----------------------------------------------------------------------------
520 IMPLEMENT_ABSTRACT_CLASS(wxDataViewMainWindow
, wxWindow
)
522 BEGIN_EVENT_TABLE(wxDataViewMainWindow
,wxWindow
)
523 EVT_PAINT (wxDataViewMainWindow::OnPaint
)
524 EVT_MOUSE_EVENTS (wxDataViewMainWindow::OnMouse
)
525 EVT_SET_FOCUS (wxDataViewMainWindow::OnSetFocus
)
528 wxDataViewMainWindow::wxDataViewMainWindow( wxDataViewCtrl
*parent
, wxWindowID id
,
529 const wxPoint
&pos
, const wxSize
&size
, const wxString
&name
) :
530 wxWindow( parent
, id
, pos
, size
, 0, name
)
534 // We need to calculate this smartly..
540 wxDataViewMainWindow::~wxDataViewMainWindow()
544 bool wxDataViewMainWindow::RowAppended()
549 bool wxDataViewMainWindow::RowPrepended()
554 bool wxDataViewMainWindow::RowInserted( size_t before
)
559 bool wxDataViewMainWindow::RowDeleted( size_t row
)
564 bool wxDataViewMainWindow::RowChanged( size_t row
)
569 bool wxDataViewMainWindow::ValueChanged( size_t col
, size_t row
)
574 bool wxDataViewMainWindow::RowsReordered( size_t *new_order
)
579 bool wxDataViewMainWindow::Cleared()
584 void wxDataViewMainWindow::UpdateDisplay()
589 void wxDataViewMainWindow::OnInternalIdle()
591 wxWindow::OnInternalIdle();
595 RecalculateDisplay();
600 void wxDataViewMainWindow::RecalculateDisplay()
602 wxDataViewListModel
*model
= GetOwner()->GetModel();
610 size_t cols
= GetOwner()->GetNumberOfColumns();
612 for (i
= 0; i
< cols
; i
++)
614 wxDataViewColumn
*col
= GetOwner()->GetColumn( i
);
615 width
+= col
->GetWidth();
618 int height
= model
->GetNumberOfRows() * m_lineHeight
;
620 SetVirtualSize( width
, height
);
621 GetOwner()->SetScrollRate( 10, m_lineHeight
);
626 void wxDataViewMainWindow::ScrollWindow( int dx
, int dy
, const wxRect
*rect
)
628 wxWindow::ScrollWindow( dx
, dy
, rect
);
629 GetOwner()->m_headerArea
->ScrollWindow( dx
, 0 );
632 void wxDataViewMainWindow::OnPaint( wxPaintEvent
&event
)
634 wxPaintDC
dc( this );
636 GetOwner()->PrepareDC( dc
);
638 dc
.SetFont( GetFont() );
640 dc
.DrawText( wxT("main window"), 5, 5 );
643 void wxDataViewMainWindow::OnMouse( wxMouseEvent
&event
)
648 void wxDataViewMainWindow::OnSetFocus( wxFocusEvent
&event
)
653 //-----------------------------------------------------------------------------
655 //-----------------------------------------------------------------------------
657 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
, wxDataViewCtrlBase
)
659 BEGIN_EVENT_TABLE(wxDataViewCtrl
, wxDataViewCtrlBase
)
660 EVT_SIZE(wxDataViewCtrl::OnSize
)
663 wxDataViewCtrl::~wxDataViewCtrl()
666 GetModel()->RemoveNotifier( m_notifier
);
669 void wxDataViewCtrl::Init()
674 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
675 const wxPoint
& pos
, const wxSize
& size
,
676 long style
, const wxValidator
& validator
)
678 if (!wxControl::Create( parent
, id
, pos
, size
, style
| wxScrolledWindowStyle
|wxSUNKEN_BORDER
, validator
))
684 MacSetClipChildren( true ) ;
687 m_clientArea
= new wxDataViewMainWindow( this, -1 );
688 m_headerArea
= new wxDataViewHeaderWindow( this, -1, wxDefaultPosition
, wxSize(-1,25) );
690 SetTargetWindow( m_clientArea
);
692 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
693 sizer
->Add( m_headerArea
, 0, wxGROW
);
694 sizer
->Add( m_clientArea
, 1, wxGROW
);
701 WXLRESULT
wxDataViewCtrl::MSWWindowProc(WXUINT nMsg
,
705 WXLRESULT rc
= wxPanel::MSWWindowProc(nMsg
, wParam
, lParam
);
708 // we need to process arrows ourselves for scrolling
709 if ( nMsg
== WM_GETDLGCODE
)
711 rc
|= DLGC_WANTARROWS
;
719 void wxDataViewCtrl::OnSize( wxSizeEvent
&event
)
721 // We need to override OnSize so that our scrolled
722 // window a) does call Layout() to use sizers for
723 // positioning the controls but b) does not query
724 // the sizer for their size and use that for setting
725 // the scrollable area as set that ourselves by
726 // calling SetScrollbar() further down.
733 bool wxDataViewCtrl::AssociateModel( wxDataViewListModel
*model
)
735 if (!wxDataViewCtrlBase::AssociateModel( model
))
738 m_notifier
= new wxGenericDataViewListModelNotifier( m_clientArea
);
740 model
->AddNotifier( m_notifier
);
742 m_clientArea
->UpdateDisplay();
747 bool wxDataViewCtrl::AppendColumn( wxDataViewColumn
*col
)
749 if (!wxDataViewCtrlBase::AppendColumn(col
))
752 m_clientArea
->UpdateDisplay();
758 // !wxUSE_GENERICDATAVIEWCTRL
761 // wxUSE_DATAVIEWCTRL