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"
22 #include "wx/spinctrl.h"
26 #include "wx/settings.h"
32 const wxChar wxDataViewCtrlNameStr
[] = wxT("dataviewCtrl");
35 bool operator == (const wxDataViewItem
&left
, const wxDataViewItem
&right
)
37 return (left
.GetID() == right
.GetID() );
41 void wxDataViewItem::Print(const wxString
& text
) const
43 wxPrintf("item %s: %l\n", text
, (long)m_id
);
47 // ---------------------------------------------------------
48 // wxDataViewModelNotifier
49 // ---------------------------------------------------------
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxDataViewModelNotifiers
)
54 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
56 size_t count
= items
.GetCount();
58 for (i
= 0; i
< count
; i
++)
59 if (!ItemAdded( parent
, items
[i
] )) return false;
64 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
66 size_t count
= items
.GetCount();
68 for (i
= 0; i
< count
; i
++)
69 if (!ItemDeleted( parent
, items
[i
] )) return false;
74 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray
&items
)
76 size_t count
= items
.GetCount();
78 for (i
= 0; i
< count
; i
++)
79 if (!ItemChanged( items
[i
] )) return false;
84 // ---------------------------------------------------------
86 // ---------------------------------------------------------
88 wxDataViewModel::wxDataViewModel()
90 m_notifiers
.DeleteContents( true );
93 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
97 wxDataViewModelNotifiers::iterator iter
;
98 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
100 wxDataViewModelNotifier
* notifier
= *iter
;
101 if (!notifier
->ItemAdded( parent
, item
))
108 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
112 wxDataViewModelNotifiers::iterator iter
;
113 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
115 wxDataViewModelNotifier
* notifier
= *iter
;
116 if (!notifier
->ItemDeleted( parent
, item
))
123 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
127 wxDataViewModelNotifiers::iterator iter
;
128 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
130 wxDataViewModelNotifier
* notifier
= *iter
;
131 if (!notifier
->ItemChanged( item
))
138 bool wxDataViewModel::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
142 wxDataViewModelNotifiers::iterator iter
;
143 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
145 wxDataViewModelNotifier
* notifier
= *iter
;
146 if (!notifier
->ItemsAdded( parent
, items
))
153 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
157 wxDataViewModelNotifiers::iterator iter
;
158 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
160 wxDataViewModelNotifier
* notifier
= *iter
;
161 if (!notifier
->ItemsDeleted( parent
, items
))
168 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray
&items
)
172 wxDataViewModelNotifiers::iterator iter
;
173 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
175 wxDataViewModelNotifier
* notifier
= *iter
;
176 if (!notifier
->ItemsChanged( items
))
183 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
187 wxDataViewModelNotifiers::iterator iter
;
188 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
190 wxDataViewModelNotifier
* notifier
= *iter
;
191 if (!notifier
->ValueChanged( item
, col
))
198 bool wxDataViewModel::Cleared()
202 wxDataViewModelNotifiers::iterator iter
;
203 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
205 wxDataViewModelNotifier
* notifier
= *iter
;
206 if (!notifier
->Cleared())
213 void wxDataViewModel::Resort()
215 wxDataViewModelNotifiers::iterator iter
;
216 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
218 wxDataViewModelNotifier
* notifier
= *iter
;
223 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
225 m_notifiers
.push_back( notifier
);
226 notifier
->SetOwner( this );
229 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
231 m_notifiers
.DeleteObject( notifier
);
234 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
235 unsigned int column
, bool ascending
)
237 // sort branches before leaves
238 bool item1_is_container
= IsContainer(item1
);
239 bool item2_is_container
= IsContainer(item2
);
241 if (item1_is_container
&& !item2_is_container
)
243 if (item2_is_container
&& !item1_is_container
)
246 wxVariant value1
,value2
;
247 GetValue( value1
, item1
, column
);
248 GetValue( value2
, item2
, column
);
252 wxVariant temp
= value1
;
257 if (value1
.GetType() == wxT("string"))
259 wxString str1
= value1
.GetString();
260 wxString str2
= value2
.GetString();
261 int res
= str1
.Cmp( str2
);
264 if (value1
.GetType() == wxT("long"))
266 long l1
= value1
.GetLong();
267 long l2
= value2
.GetLong();
271 if (value1
.GetType() == wxT("double"))
273 double d1
= value1
.GetDouble();
274 double d2
= value2
.GetDouble();
275 if (d1
< d2
) return 1;
276 if (d1
> d2
) return -1;
278 if (value1
.GetType() == wxT("datetime"))
280 wxDateTime dt1
= value1
.GetDateTime();
281 wxDateTime dt2
= value2
.GetDateTime();
282 if (dt1
.IsEarlierThan(dt2
)) return 1;
283 if (dt2
.IsEarlierThan(dt1
)) return -11;
286 // items must be different
287 unsigned long litem1
= (unsigned long) item1
.GetID();
288 unsigned long litem2
= (unsigned long) item2
.GetID();
291 return litem2
-litem2
;
293 return litem1
-litem2
;
296 // ---------------------------------------------------------
297 // wxDataViewIndexListModel
298 // ---------------------------------------------------------
300 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
302 // build initial index
304 for (i
= 1; i
< initial_size
+1; i
++)
305 m_hash
.Add( (void*) i
);
306 m_lastIndex
= initial_size
+ 1;
309 wxDataViewIndexListModel::~wxDataViewIndexListModel()
313 void wxDataViewIndexListModel::RowPrepended()
315 unsigned int id
= m_lastIndex
++;
316 m_hash
.Insert( (void*) id
, 0 );
317 wxDataViewItem
item( (void*) id
);
318 ItemAdded( wxDataViewItem(0), item
);
321 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
323 unsigned int id
= m_lastIndex
++;
324 m_hash
.Insert( (void*) id
, before
);
325 wxDataViewItem
item( (void*) id
);
326 ItemAdded( wxDataViewItem(0), item
);
329 void wxDataViewIndexListModel::RowAppended()
331 unsigned int id
= m_lastIndex
++;
332 m_hash
.Add( (void*) id
);
333 wxDataViewItem
item( (void*) id
);
334 ItemAdded( wxDataViewItem(0), item
);
337 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
339 wxDataViewItem
item( m_hash
[row
] );
340 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
341 m_hash
.RemoveAt( row
);
344 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
346 wxDataViewModel::ItemChanged( GetItem(row
) );
349 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
351 wxDataViewModel::ValueChanged( GetItem(row
), col
);
354 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
356 // assert for not found
357 return (unsigned int) m_hash
.Index( item
.GetID() );
360 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
362 wxASSERT( row
< m_hash
.GetCount() );
363 return wxDataViewItem( m_hash
[row
] );
366 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
367 const wxDataViewItem
& item2
,
368 unsigned int WXUNUSED(column
),
372 return GetRow(item1
) - GetRow(item2
);
374 return GetRow(item2
) - GetRow(item1
);
377 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
378 const wxDataViewItem
&item
, unsigned int col
) const
380 GetValue( variant
, GetRow(item
), col
);
383 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
384 const wxDataViewItem
&item
, unsigned int col
)
386 return SetValue( variant
, GetRow(item
), col
);
389 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
391 return wxDataViewItem(0);
394 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
396 // only the invisible root item has children
403 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
410 return m_hash
.GetCount();
413 //-----------------------------------------------------------------------------
414 // wxDataViewIconText
415 //-----------------------------------------------------------------------------
417 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
419 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
421 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
423 if (one
.GetText() != two
.GetText()) return false;
424 if (one
.IsSameAs(two
)) return false;
428 // ---------------------------------------------------------
429 // wxDataViewRendererBase
430 // ---------------------------------------------------------
432 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
434 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
435 wxDataViewCellMode
WXUNUSED(mode
),
436 int WXUNUSED(align
) )
438 m_variantType
= varianttype
;
442 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
444 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
447 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
449 m_item
= item
; // remember for later
451 unsigned int col
= GetOwner()->GetModelColumn();
453 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
455 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
457 wxDataViewEditorCtrlEvtHandler
*handler
=
458 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
460 m_editorCtrl
->PushEventHandler( handler
);
462 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
463 handler
->SetFocusOnIdle();
465 m_editorCtrl
->SetFocus();
468 // Now we should send Editing Started event
469 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
470 event
.SetDataViewColumn( GetOwner() );
471 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
472 event
.SetItem( item
);
473 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
478 void wxDataViewRendererBase::CancelEditing()
480 wxPendingDelete
.Append( m_editorCtrl
);
482 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
484 // m_editorCtrl->PopEventHandler( true );
487 bool wxDataViewRendererBase::FinishEditing()
490 GetValueFromEditorCtrl( m_editorCtrl
, value
);
492 wxPendingDelete
.Append( m_editorCtrl
);
494 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
496 if (!Validate(value
))
499 unsigned int col
= GetOwner()->GetModelColumn();
500 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
501 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
503 // m_editorCtrl->PopEventHandler( true );
505 // Now we should send Editing Done event
506 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
507 event
.SetDataViewColumn( GetOwner() );
508 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
509 event
.SetItem( m_item
);
510 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
515 //-----------------------------------------------------------------------------
516 // wxDataViewEditorCtrlEvtHandler
517 //-----------------------------------------------------------------------------
519 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
520 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
521 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
522 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
525 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
526 wxControl
*editorCtrl
,
527 wxDataViewRenderer
*owner
)
530 m_editorCtrl
= editorCtrl
;
535 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
539 m_focusOnIdle
= false;
540 if (wxWindow::FindFocus() != m_editorCtrl
)
541 m_editorCtrl
->SetFocus();
547 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
549 switch ( event
.m_keyCode
)
553 m_owner
->FinishEditing();
558 m_owner
->CancelEditing();
566 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
571 m_owner
->FinishEditing();
577 // ---------------------------------------------------------
578 // wxDataViewColumnBase
579 // ---------------------------------------------------------
581 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
583 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
584 wxDataViewRenderer
*renderer
,
585 unsigned int model_column
,
587 wxAlignment
WXUNUSED(align
),
590 m_renderer
= renderer
;
591 m_model_column
= model_column
;
593 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
595 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
596 // parameters inside the native control!
599 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
600 wxDataViewRenderer
*renderer
,
601 unsigned int model_column
,
603 wxAlignment
WXUNUSED(align
),
604 int WXUNUSED(flags
) )
606 m_renderer
= renderer
;
607 m_model_column
= model_column
;
610 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
613 wxDataViewColumnBase::~wxDataViewColumnBase()
619 int wxDataViewColumnBase::GetFlags() const
624 ret
|= wxDATAVIEW_COL_SORTABLE
;
626 ret
|= wxDATAVIEW_COL_RESIZABLE
;
628 ret
|= wxDATAVIEW_COL_HIDDEN
;
633 void wxDataViewColumnBase::SetFlags(int flags
)
635 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
636 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
637 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
640 // ---------------------------------------------------------
641 // wxDataViewCtrlBase
642 // ---------------------------------------------------------
644 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
646 wxDataViewCtrlBase::wxDataViewCtrlBase()
649 m_expander_column
= 0;
653 wxDataViewCtrlBase::~wxDataViewCtrlBase()
662 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
666 m_model
->DecRef(); // discard old model, if any
669 // add our own reference to the new model:
679 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
685 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
686 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
688 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
689 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
690 model_column
, width
, align
, flags
);
696 wxDataViewCtrlBase::AppendIconTextColumn( const wxString
&label
, unsigned int model_column
,
697 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
699 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
700 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
701 model_column
, width
, align
, flags
);
707 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
708 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
711 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
712 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
713 model_column
, width
, align
, flags
);
719 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
720 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
722 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
723 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
724 model_column
, width
, align
, flags
);
730 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
731 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
733 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
734 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
735 model_column
, width
, align
, flags
);
741 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
742 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
744 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
745 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
746 model_column
, width
, align
, flags
);
752 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
753 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
755 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
756 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
757 model_column
, width
, align
, flags
);
763 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
764 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
766 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
767 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
768 model_column
, width
, align
, flags
);
774 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
775 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
777 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
778 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
779 model_column
, width
, align
, flags
);
785 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
786 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
788 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
789 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
790 model_column
, width
, align
, flags
);
796 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
797 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
799 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
800 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
801 model_column
, width
, align
, flags
);
807 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
808 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
810 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
811 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
812 model_column
, width
, align
, flags
);
818 wxDataViewCtrlBase::PrependTextColumn( const wxString
&label
, unsigned int model_column
,
819 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
821 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
822 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
823 model_column
, width
, align
, flags
);
824 PrependColumn( ret
);
829 wxDataViewCtrlBase::PrependIconTextColumn( const wxString
&label
, unsigned int model_column
,
830 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
832 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
833 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
834 model_column
, width
, align
, flags
);
835 PrependColumn( ret
);
840 wxDataViewCtrlBase::PrependToggleColumn( const wxString
&label
, unsigned int model_column
,
841 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
844 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
845 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
846 model_column
, width
, align
, flags
);
847 PrependColumn( ret
);
852 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
853 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
855 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
856 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
857 model_column
, width
, align
, flags
);
858 PrependColumn( ret
);
863 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
864 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
866 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
867 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
868 model_column
, width
, align
, flags
);
869 PrependColumn( ret
);
874 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
875 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
877 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
878 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
879 model_column
, width
, align
, flags
);
880 PrependColumn( ret
);
885 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
886 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
888 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
889 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
890 model_column
, width
, align
, flags
);
891 PrependColumn( ret
);
896 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
897 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
899 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
900 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
901 model_column
, width
, align
, flags
);
902 PrependColumn( ret
);
907 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
908 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
910 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
911 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
912 model_column
, width
, align
, flags
);
913 PrependColumn( ret
);
918 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
919 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
921 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
922 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
923 model_column
, width
, align
, flags
);
924 PrependColumn( ret
);
929 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
930 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
932 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
933 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
934 model_column
, width
, align
, flags
);
935 PrependColumn( ret
);
940 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
941 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
943 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
944 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
945 model_column
, width
, align
, flags
);
946 PrependColumn( ret
);
951 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
953 col
->SetOwner( (wxDataViewCtrl
*) this );
958 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
960 col
->SetOwner( (wxDataViewCtrl
*) this );
964 // ---------------------------------------------------------
966 // ---------------------------------------------------------
968 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
970 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
)
972 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
973 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
974 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
975 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
976 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
977 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
978 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
979 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
)
981 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
982 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
983 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
986 // -------------------------------------
987 // wxDataViewSpinRenderer
988 // -------------------------------------
990 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
991 wxDataViewCustomRenderer( "long", mode
, alignment
)
997 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1000 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
1001 labelRect
.GetTopLeft(), labelRect
.GetSize(), wxSP_ARROW_KEYS
, m_min
, m_max
, l
);
1004 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1006 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1007 long l
= sc
->GetValue();
1012 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1015 str
.Printf( "%d", (int) m_data
);
1016 RenderText( str
, 0, rect
, dc
, state
);
1020 wxSize
wxDataViewSpinRenderer::GetSize() const
1022 return wxSize(80,16);
1025 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1027 m_data
= value
.GetLong();
1031 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1037 //-----------------------------------------------------------------------------
1038 // wxDataViewTreeStore
1039 //-----------------------------------------------------------------------------
1041 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1042 wxDataViewTreeStoreNode
*parent
,
1043 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1051 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1057 #include "wx/listimpl.cpp"
1058 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
)
1060 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1061 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1062 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1063 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1065 m_iconExpanded
= expanded
;
1066 m_children
.DeleteContents(true);
1069 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1073 //-----------------------------------------------------------------------------
1075 wxDataViewTreeStore::wxDataViewTreeStore()
1077 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1080 wxDataViewTreeStore::~wxDataViewTreeStore()
1085 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1086 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1088 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1089 if (!parent_node
) return wxDataViewItem(0);
1091 wxDataViewTreeStoreNode
*node
=
1092 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1093 parent_node
->GetChildren().Append( node
);
1096 ItemAdded( parent
, node
->GetItem() );
1098 return node
->GetItem();
1101 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1102 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1104 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1105 if (!parent_node
) return wxDataViewItem(0);
1107 wxDataViewTreeStoreNode
*node
=
1108 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1109 parent_node
->GetChildren().Insert( node
);
1112 ItemAdded( parent
, node
->GetItem() );
1114 return node
->GetItem();
1118 wxDataViewTreeStore::InsertItem(const wxDataViewItem
& WXUNUSED(parent
),
1119 const wxDataViewItem
& WXUNUSED(previous
),
1120 const wxString
& WXUNUSED(text
),
1121 const wxIcon
& WXUNUSED(icon
),
1122 wxClientData
* WXUNUSED(data
))
1124 return wxDataViewItem(0);
1127 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1128 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1129 wxClientData
*data
)
1131 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1132 if (!parent_node
) return wxDataViewItem(0);
1134 wxDataViewTreeStoreContainerNode
*node
=
1135 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1136 parent_node
->GetChildren().Insert( node
);
1139 ItemAdded( parent
, node
->GetItem() );
1141 return node
->GetItem();
1145 wxDataViewTreeStore::AppendContainer(const wxDataViewItem
& parent
,
1146 const wxString
&text
,
1148 const wxIcon
& expanded
,
1149 wxClientData
* data
)
1151 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1152 if (!parent_node
) return wxDataViewItem(0);
1154 wxDataViewTreeStoreContainerNode
*node
=
1155 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1156 parent_node
->GetChildren().Append( node
);
1159 ItemAdded( parent
, node
->GetItem() );
1161 return node
->GetItem();
1165 wxDataViewTreeStore::InsertContainer(const wxDataViewItem
& WXUNUSED(parent
),
1166 const wxDataViewItem
& WXUNUSED(previous
),
1167 const wxString
& WXUNUSED(text
),
1168 const wxIcon
& WXUNUSED(icon
),
1169 const wxIcon
& WXUNUSED(expanded
),
1170 wxClientData
* WXUNUSED(data
))
1172 return wxDataViewItem(0);
1175 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1177 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1178 if (!parent_node
) return wxDataViewItem(0);
1180 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1182 return node
->GetData();
1184 return wxDataViewItem(0);
1187 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1189 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1190 if (!node
) return -1;
1192 if (!node
->IsContainer())
1195 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1196 return (int) container_node
->GetChildren().GetCount();
1199 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1201 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1204 node
->SetText( text
);
1207 ValueChanged( item
, 0 );
1210 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1212 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1213 if (!node
) return wxEmptyString
;
1215 return node
->GetText();
1218 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1220 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1223 node
->SetIcon( icon
);
1226 ValueChanged( item
, 0 );
1229 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1231 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1232 if (!node
) return wxNullIcon
;
1234 return node
->GetIcon();
1237 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1239 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1242 node
->SetExpandedIcon( icon
);
1245 ValueChanged( item
, 0 );
1248 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1250 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1251 if (!node
) return wxNullIcon
;
1253 return node
->GetExpandedIcon();
1256 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1258 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1261 node
->SetData( data
);
1263 // notify control? only sensible when sorting on client data
1264 // ValueChanged( item, 0 );
1267 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1269 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1270 if (!node
) return NULL
;
1272 return node
->GetData();
1275 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1277 if (!item
.IsOk()) return;
1279 wxDataViewItem parent_item
= GetParent( item
);
1281 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1282 if (!parent_node
) return;
1284 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1287 parent_node
->GetChildren().DeleteObject( node
);
1290 ItemDeleted( parent_item
, item
);
1293 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1295 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1298 wxDataViewItemArray array
;
1299 wxDataViewTreeStoreNodeList::iterator iter
;
1300 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1302 wxDataViewTreeStoreNode
* child
= *iter
;
1303 array
.Add( child
->GetItem() );
1306 node
->GetChildren().clear();
1309 ItemsDeleted( item
, array
);
1312 void wxDataViewTreeStore::DeleteAllItems()
1318 wxDataViewTreeStore::GetValue(wxVariant
&variant
,
1319 const wxDataViewItem
&item
,
1320 unsigned int WXUNUSED(col
)) const
1322 // if (col != 0) return;
1324 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1327 wxDataViewIconText
data( node
->GetText(), node
->GetIcon() );
1333 wxDataViewTreeStore::SetValue(const wxVariant
& variant
,
1334 const wxDataViewItem
& item
,
1335 unsigned int WXUNUSED(col
))
1337 // if (col != 0) return false;
1339 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1340 if (!node
) return false;
1342 wxDataViewIconText data
;
1346 node
->SetText( data
.GetText() );
1347 node
->SetIcon( data
.GetIcon() );
1352 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1354 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1355 if (!node
) return wxDataViewItem(0);
1357 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1358 if (!parent
) return wxDataViewItem(0);
1360 if (parent
== m_root
)
1361 return wxDataViewItem(0);
1363 return parent
->GetItem();
1366 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
&item
) const
1368 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1369 if (!node
) return false;
1371 return node
->IsContainer();
1374 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1376 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1377 if (!node
) return 0;
1379 wxDataViewTreeStoreNodeList::iterator iter
;
1380 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1382 wxDataViewTreeStoreNode
* child
= *iter
;
1383 children
.Add( child
->GetItem() );
1386 return node
->GetChildren().GetCount();
1389 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
1390 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) )
1392 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
1393 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
1395 if (!node1
|| !node2
)
1398 wxDataViewTreeStoreContainerNode
* parent1
=
1399 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
1400 wxDataViewTreeStoreContainerNode
* parent2
=
1401 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
1403 if (parent1
!= parent2
)
1405 wxLogError( wxT("Comparing items with different parent.") );
1409 if (node1
->IsContainer() && !!node2
->IsContainer())
1412 if (node2
->IsContainer() && !!node1
->IsContainer())
1415 return parent1
->GetChildren().IndexOf( node1
) - parent1
->GetChildren().IndexOf( node2
);
1418 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
1423 return (wxDataViewTreeStoreNode
*) item
.GetID();
1426 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
1429 return (wxDataViewTreeStoreContainerNode
*) m_root
;
1431 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
1433 if (!node
->IsContainer())
1436 return (wxDataViewTreeStoreContainerNode
*) node
;
1439 #endif // wxUSE_DATAVIEWCTRL