1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/datavcmn.cpp
3 // Purpose: wxDataViewCtrl base classes and common parts
4 // Author: Robert Roebling
7 // Copyright: (c) 2006, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
26 const wxChar wxDataViewCtrlNameStr
[] = wxT("dataviewCtrl");
29 bool operator == (const wxDataViewItem
&left
, const wxDataViewItem
&right
)
31 return (left
.GetID() == right
.GetID() );
35 // ---------------------------------------------------------
37 // ---------------------------------------------------------
39 #include <wx/listimpl.cpp>
40 WX_DEFINE_LIST(wxDataViewModelNotifiers
);
42 wxDataViewModel::wxDataViewModel()
44 m_notifiers
.DeleteContents( true );
47 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
51 wxDataViewModelNotifiers::iterator iter
;
52 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
54 wxDataViewModelNotifier
* notifier
= *iter
;
55 if (!notifier
->ItemAdded( parent
, item
))
62 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
66 wxDataViewModelNotifiers::iterator iter
;
67 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
69 wxDataViewModelNotifier
* notifier
= *iter
;
70 if (!notifier
->ItemDeleted( parent
, item
))
77 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
81 wxDataViewModelNotifiers::iterator iter
;
82 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
84 wxDataViewModelNotifier
* notifier
= *iter
;
85 if (!notifier
->ItemChanged( item
))
92 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
96 wxDataViewModelNotifiers::iterator iter
;
97 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
99 wxDataViewModelNotifier
* notifier
= *iter
;
100 if (!notifier
->ValueChanged( item
, col
))
107 bool wxDataViewModel::Cleared()
111 wxDataViewModelNotifiers::iterator iter
;
112 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
114 wxDataViewModelNotifier
* notifier
= *iter
;
115 if (!notifier
->Cleared())
122 void wxDataViewModel::Resort()
124 wxDataViewModelNotifiers::iterator iter
;
125 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
127 wxDataViewModelNotifier
* notifier
= *iter
;
132 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
134 m_notifiers
.push_back( notifier
);
135 notifier
->SetOwner( this );
138 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
140 m_notifiers
.DeleteObject( notifier
);
143 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
144 unsigned int column
, bool ascending
)
146 // sort branches before leaves
147 bool item1_is_container
= IsContainer(item1
);
148 bool item2_is_container
= IsContainer(item2
);
150 if (item1_is_container
&& !item2_is_container
)
152 if (item2_is_container
&& !item1_is_container
)
155 wxVariant value1
,value2
;
156 GetValue( value1
, item1
, column
);
157 GetValue( value2
, item2
, column
);
161 wxVariant temp
= value1
;
166 if (value1
.GetType() == wxT("string"))
168 wxString str1
= value1
.GetString();
169 wxString str2
= value2
.GetString();
170 int res
= str1
.Cmp( str2
);
173 if (value1
.GetType() == wxT("long"))
175 long l1
= value1
.GetLong();
176 long l2
= value2
.GetLong();
180 if (value1
.GetType() == wxT("double"))
182 double d1
= value1
.GetDouble();
183 double d2
= value2
.GetDouble();
184 if (d1
< d2
) return 1;
185 if (d1
> d2
) return -1;
187 if (value1
.GetType() == wxT("datetime"))
189 wxDateTime dt1
= value1
.GetDateTime();
190 wxDateTime dt2
= value2
.GetDateTime();
191 if (dt1
.IsEarlierThan(dt2
)) return 1;
192 if (dt2
.IsEarlierThan(dt1
)) return -11;
195 // items must be different
196 unsigned long litem1
= (unsigned long) item1
.GetID();
197 unsigned long litem2
= (unsigned long) item2
.GetID();
200 return litem2
-litem2
;
202 return litem1
-litem2
;
205 // ---------------------------------------------------------
206 // wxDataViewIndexListModel
207 // ---------------------------------------------------------
209 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
211 // build initial index
213 for (i
= 1; i
< initial_size
+1; i
++)
214 m_hash
.Add( (void*) i
);
215 m_lastIndex
= initial_size
+ 1;
218 wxDataViewIndexListModel::~wxDataViewIndexListModel()
222 void wxDataViewIndexListModel::RowPrepended()
224 unsigned int id
= m_lastIndex
++;
225 m_hash
.Insert( (void*) id
, 0 );
226 wxDataViewItem
item( (void*) id
);
227 ItemAdded( wxDataViewItem(0), item
);
230 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
232 unsigned int id
= m_lastIndex
++;
233 m_hash
.Insert( (void*) id
, before
);
234 wxDataViewItem
item( (void*) id
);
235 ItemAdded( wxDataViewItem(0), item
);
238 void wxDataViewIndexListModel::RowAppended()
240 unsigned int id
= m_lastIndex
++;
241 m_hash
.Add( (void*) id
);
242 wxDataViewItem
item( (void*) id
);
243 ItemAdded( wxDataViewItem(0), item
);
246 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
248 wxDataViewItem
item( m_hash
[row
] );
249 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
250 m_hash
.RemoveAt( row
);
253 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
255 wxDataViewModel::ItemChanged( GetItem(row
) );
258 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
260 wxDataViewModel::ValueChanged( GetItem(row
), col
);
263 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
265 // assert for not found
266 return (unsigned int) m_hash
.Index( item
.GetID() );
269 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
271 wxASSERT( row
< m_hash
.GetCount() );
272 return wxDataViewItem( m_hash
[row
] );
275 int wxDataViewIndexListModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
276 unsigned int column
, bool ascending
)
279 return GetRow(item1
) - GetRow(item2
);
281 return GetRow(item2
) - GetRow(item1
);
284 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
285 const wxDataViewItem
&item
, unsigned int col
) const
287 GetValue( variant
, GetRow(item
), col
);
290 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
291 const wxDataViewItem
&item
, unsigned int col
)
293 return SetValue( variant
, GetRow(item
), col
);
296 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
298 return wxDataViewItem(0);
301 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
303 // only the invisible root item has children
310 wxDataViewItem
wxDataViewIndexListModel::GetFirstChild( const wxDataViewItem
&parent
) const
314 if (m_hash
.GetCount() == 0)
315 return wxDataViewItem(0);
317 return wxDataViewItem( m_hash
[0]);
320 return wxDataViewItem(0);
323 wxDataViewItem
wxDataViewIndexListModel::GetNextSibling( const wxDataViewItem
&item
) const
326 return wxDataViewItem(0);
328 int pos
= m_hash
.Index( item
.GetID() );
329 if ((pos
== wxNOT_FOUND
) || (pos
== (int) (m_hash
.GetCount()-1)))
330 return wxDataViewItem(0);
332 return wxDataViewItem( m_hash
[pos
+1] );
335 //-----------------------------------------------------------------------------
336 // wxDataViewIconText
337 //-----------------------------------------------------------------------------
339 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
341 IMPLEMENT_VARIANT_OBJECT(wxDataViewIconText
)
343 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
345 if (one
.GetText() != two
.GetText()) return false;
346 if (one
.IsSameAs(two
)) return false;
350 // ---------------------------------------------------------
351 // wxDataViewRendererBase
352 // ---------------------------------------------------------
354 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
356 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
357 wxDataViewCellMode
WXUNUSED(mode
),
358 int WXUNUSED(align
) )
360 m_variantType
= varianttype
;
364 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
366 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
369 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
371 m_item
= item
; // remember for later
373 unsigned int col
= GetOwner()->GetModelColumn();
375 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
377 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
379 wxDataViewEditorCtrlEvtHandler
*handler
=
380 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
382 m_editorCtrl
->PushEventHandler( handler
);
384 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
385 handler
->SetFocusOnIdle();
387 m_editorCtrl
->SetFocus();
390 // Now we should send Editing Started event
391 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
392 event
.SetDataViewColumn( GetOwner() );
393 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
394 event
.SetItem( item
);
395 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
400 void wxDataViewRendererBase::CancelEditing()
402 wxPendingDelete
.Append( m_editorCtrl
);
404 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
406 // m_editorCtrl->PopEventHandler( true );
409 bool wxDataViewRendererBase::FinishEditing()
412 GetValueFromEditorCtrl( m_editorCtrl
, value
);
414 wxPendingDelete
.Append( m_editorCtrl
);
416 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
418 if (!Validate(value
))
421 unsigned int col
= GetOwner()->GetModelColumn();
422 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
423 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
425 // m_editorCtrl->PopEventHandler( true );
427 // Now we should send Editing Done event
428 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
429 event
.SetDataViewColumn( GetOwner() );
430 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
431 event
.SetItem( m_item
);
432 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
437 //-----------------------------------------------------------------------------
438 // wxDataViewEditorCtrlEvtHandler
439 //-----------------------------------------------------------------------------
441 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
442 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
443 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
444 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
447 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
448 wxControl
*editorCtrl
,
449 wxDataViewRenderer
*owner
)
452 m_editorCtrl
= editorCtrl
;
457 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
461 m_focusOnIdle
= false;
462 if (wxWindow::FindFocus() != m_editorCtrl
)
463 m_editorCtrl
->SetFocus();
469 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
471 switch ( event
.m_keyCode
)
475 m_owner
->FinishEditing();
480 m_owner
->CancelEditing();
488 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
493 m_owner
->FinishEditing();
499 // ---------------------------------------------------------
500 // wxDataViewColumnBase
501 // ---------------------------------------------------------
503 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
505 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
506 wxDataViewRenderer
*renderer
,
507 unsigned int model_column
,
509 wxAlignment
WXUNUSED(align
),
512 m_renderer
= renderer
;
513 m_model_column
= model_column
;
515 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
517 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
518 // parameters inside the native control!
521 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
522 wxDataViewRenderer
*renderer
,
523 unsigned int model_column
,
525 wxAlignment
WXUNUSED(align
),
526 int WXUNUSED(flags
) )
528 m_renderer
= renderer
;
529 m_model_column
= model_column
;
532 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
535 wxDataViewColumnBase::~wxDataViewColumnBase()
541 int wxDataViewColumnBase::GetFlags() const
546 ret
|= wxDATAVIEW_COL_SORTABLE
;
548 ret
|= wxDATAVIEW_COL_RESIZABLE
;
550 ret
|= wxDATAVIEW_COL_HIDDEN
;
555 void wxDataViewColumnBase::SetFlags(int flags
)
557 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
558 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
559 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
562 // ---------------------------------------------------------
563 // wxDataViewCtrlBase
564 // ---------------------------------------------------------
566 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
568 wxDataViewCtrlBase::wxDataViewCtrlBase()
571 m_expander_column
= 0;
575 wxDataViewCtrlBase::~wxDataViewCtrlBase()
584 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
588 m_model
->DecRef(); // discard old model, if any
591 // add our own reference to the new model:
601 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
607 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
608 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
610 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
611 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
612 model_column
, width
, align
, flags
);
618 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
619 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
622 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
623 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
624 model_column
, width
, align
, flags
);
630 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
631 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
633 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
634 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
635 model_column
, width
, align
, flags
);
641 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
642 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
644 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
645 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
646 model_column
, width
, align
, flags
);
652 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
653 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
655 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
656 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
657 model_column
, width
, align
, flags
);
663 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
664 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
666 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
667 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
668 model_column
, width
, align
, flags
);
674 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
675 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
677 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
678 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
679 model_column
, width
, align
, flags
);
685 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
686 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
688 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
689 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
690 model_column
, width
, align
, flags
);
696 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
697 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
699 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
700 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
701 model_column
, width
, align
, flags
);
707 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
708 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
710 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
711 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
712 model_column
, width
, align
, flags
);
718 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
720 col
->SetOwner( (wxDataViewCtrl
*) this );
724 // ---------------------------------------------------------
726 // ---------------------------------------------------------
728 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
730 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
)
731 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_DESELECTED
)
732 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
733 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
734 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
735 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
736 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
737 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
738 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
740 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
741 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
742 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
744 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
)
745 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
)
746 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
)
747 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
)
748 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
)