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
)
304 // build initial index
306 for (i
= 1; i
< initial_size
+1; i
++)
307 m_hash
.Add( (void*) i
);
308 m_lastIndex
= initial_size
+ 1;
311 wxDataViewIndexListModel::~wxDataViewIndexListModel()
315 void wxDataViewIndexListModel::RowPrepended()
317 unsigned int id
= m_lastIndex
++;
318 m_hash
.Insert( (void*) id
, 0 );
319 wxDataViewItem
item( (void*) id
);
320 ItemAdded( wxDataViewItem(0), item
);
325 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
327 unsigned int id
= m_lastIndex
++;
328 m_hash
.Insert( (void*) id
, before
);
329 wxDataViewItem
item( (void*) id
);
330 ItemAdded( wxDataViewItem(0), item
);
335 void wxDataViewIndexListModel::RowAppended()
337 unsigned int id
= m_lastIndex
++;
338 m_hash
.Add( (void*) id
);
339 wxDataViewItem
item( (void*) id
);
340 ItemAdded( wxDataViewItem(0), item
);
343 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
345 wxDataViewItem
item( m_hash
[row
] );
346 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
347 m_hash
.RemoveAt( row
);
350 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
352 wxDataViewModel::ItemChanged( GetItem(row
) );
355 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
357 wxDataViewModel::ValueChanged( GetItem(row
), col
);
360 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
364 unsigned int pos
= (unsigned int) item
.GetID();
368 // assert for not found
369 return (unsigned int) m_hash
.Index( item
.GetID() );
372 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
374 wxASSERT( row
< m_hash
.GetCount() );
375 return wxDataViewItem( m_hash
[row
] );
378 bool wxDataViewIndexListModel::HasDefaultCompare() const
383 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
384 const wxDataViewItem
& item2
,
385 unsigned int WXUNUSED(column
),
390 unsigned int pos1
= (unsigned int) item1
.GetID();
391 unsigned int pos2
= (unsigned int) item2
.GetID();
400 return GetRow(item1
) - GetRow(item2
);
402 return GetRow(item2
) - GetRow(item1
);
405 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
406 const wxDataViewItem
&item
, unsigned int col
) const
408 GetValue( variant
, GetRow(item
), col
);
411 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
412 const wxDataViewItem
&item
, unsigned int col
)
414 return SetValue( variant
, GetRow(item
), col
);
417 bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem
&item
, unsigned int col
, wxDataViewItemAttr
&attr
)
419 return GetAttr( GetRow(item
), col
, attr
);
422 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
424 return wxDataViewItem(0);
427 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
429 // only the invisible root item has children
436 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
443 return m_hash
.GetCount();
446 //-----------------------------------------------------------------------------
447 // wxDataViewIconText
448 //-----------------------------------------------------------------------------
450 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
452 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
454 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
456 if (one
.GetText() != two
.GetText()) return false;
457 if (one
.IsSameAs(two
)) return false;
461 // ---------------------------------------------------------
462 // wxDataViewRendererBase
463 // ---------------------------------------------------------
465 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
467 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
468 wxDataViewCellMode
WXUNUSED(mode
),
469 int WXUNUSED(align
) )
471 m_variantType
= varianttype
;
475 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
477 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
480 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
482 m_item
= item
; // remember for later
484 unsigned int col
= GetOwner()->GetModelColumn();
486 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
488 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
490 wxDataViewEditorCtrlEvtHandler
*handler
=
491 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
493 m_editorCtrl
->PushEventHandler( handler
);
495 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
496 handler
->SetFocusOnIdle();
498 m_editorCtrl
->SetFocus();
501 // Now we should send Editing Started event
502 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
503 event
.SetDataViewColumn( GetOwner() );
504 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
505 event
.SetItem( item
);
506 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
511 void wxDataViewRendererBase::CancelEditing()
513 wxPendingDelete
.Append( m_editorCtrl
);
515 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
517 // m_editorCtrl->PopEventHandler( true );
520 bool wxDataViewRendererBase::FinishEditing()
523 GetValueFromEditorCtrl( m_editorCtrl
, value
);
525 wxPendingDelete
.Append( m_editorCtrl
);
527 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
529 if (!Validate(value
))
532 unsigned int col
= GetOwner()->GetModelColumn();
533 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
534 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
536 // m_editorCtrl->PopEventHandler( true );
538 // Now we should send Editing Done event
539 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
540 event
.SetDataViewColumn( GetOwner() );
541 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
542 event
.SetItem( m_item
);
543 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
548 //-----------------------------------------------------------------------------
549 // wxDataViewEditorCtrlEvtHandler
550 //-----------------------------------------------------------------------------
552 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
553 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
554 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
555 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
558 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
559 wxControl
*editorCtrl
,
560 wxDataViewRenderer
*owner
)
563 m_editorCtrl
= editorCtrl
;
568 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
572 m_focusOnIdle
= false;
573 if (wxWindow::FindFocus() != m_editorCtrl
)
574 m_editorCtrl
->SetFocus();
580 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
582 switch ( event
.m_keyCode
)
586 m_owner
->FinishEditing();
591 m_owner
->CancelEditing();
599 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
604 m_owner
->FinishEditing();
610 // ---------------------------------------------------------
611 // wxDataViewColumnBase
612 // ---------------------------------------------------------
614 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
616 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
617 wxDataViewRenderer
*renderer
,
618 unsigned int model_column
,
620 wxAlignment
WXUNUSED(align
),
623 m_renderer
= renderer
;
624 m_model_column
= model_column
;
626 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
628 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
629 // parameters inside the native control!
632 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
633 wxDataViewRenderer
*renderer
,
634 unsigned int model_column
,
636 wxAlignment
WXUNUSED(align
),
637 int WXUNUSED(flags
) )
639 m_renderer
= renderer
;
640 m_model_column
= model_column
;
643 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
646 wxDataViewColumnBase::~wxDataViewColumnBase()
652 int wxDataViewColumnBase::GetFlags() const
657 ret
|= wxDATAVIEW_COL_SORTABLE
;
659 ret
|= wxDATAVIEW_COL_RESIZABLE
;
661 ret
|= wxDATAVIEW_COL_HIDDEN
;
666 void wxDataViewColumnBase::SetFlags(int flags
)
668 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
669 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
670 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
673 // ---------------------------------------------------------
674 // wxDataViewCtrlBase
675 // ---------------------------------------------------------
677 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
679 wxDataViewCtrlBase::wxDataViewCtrlBase()
682 m_expander_column
= 0;
686 wxDataViewCtrlBase::~wxDataViewCtrlBase()
695 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
699 m_model
->DecRef(); // discard old model, if any
702 // add our own reference to the new model:
712 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
718 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
719 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
721 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
722 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
723 model_column
, width
, align
, flags
);
729 wxDataViewCtrlBase::AppendIconTextColumn( const wxString
&label
, unsigned int model_column
,
730 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
732 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
733 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
734 model_column
, width
, align
, flags
);
740 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
741 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
744 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
745 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
746 model_column
, width
, align
, flags
);
752 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
753 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
755 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
756 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
757 model_column
, width
, align
, flags
);
763 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
764 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
766 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
767 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
768 model_column
, width
, align
, flags
);
774 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
775 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
777 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
778 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
779 model_column
, width
, align
, flags
);
785 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
786 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
788 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
789 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
790 model_column
, width
, align
, flags
);
796 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
797 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
799 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
800 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
801 model_column
, width
, align
, flags
);
807 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
808 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
810 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
811 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
812 model_column
, width
, align
, flags
);
818 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
819 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
821 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
822 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
823 model_column
, width
, align
, flags
);
829 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
830 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
832 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
833 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
834 model_column
, width
, align
, flags
);
840 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
841 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
843 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
844 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
845 model_column
, width
, align
, flags
);
851 wxDataViewCtrlBase::PrependTextColumn( const wxString
&label
, unsigned int model_column
,
852 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
854 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
855 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
856 model_column
, width
, align
, flags
);
857 PrependColumn( ret
);
862 wxDataViewCtrlBase::PrependIconTextColumn( const wxString
&label
, unsigned int model_column
,
863 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
865 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
866 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
867 model_column
, width
, align
, flags
);
868 PrependColumn( ret
);
873 wxDataViewCtrlBase::PrependToggleColumn( const wxString
&label
, unsigned int model_column
,
874 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
877 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
878 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
879 model_column
, width
, align
, flags
);
880 PrependColumn( ret
);
885 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
886 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
888 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
889 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
890 model_column
, width
, align
, flags
);
891 PrependColumn( ret
);
896 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
897 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
899 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
900 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
901 model_column
, width
, align
, flags
);
902 PrependColumn( ret
);
907 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
908 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
910 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
911 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
912 model_column
, width
, align
, flags
);
913 PrependColumn( ret
);
918 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
919 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
921 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
922 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
923 model_column
, width
, align
, flags
);
924 PrependColumn( ret
);
929 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
930 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
932 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
933 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
934 model_column
, width
, align
, flags
);
935 PrependColumn( ret
);
940 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
941 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
943 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
944 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
945 model_column
, width
, align
, flags
);
946 PrependColumn( ret
);
951 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
952 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
954 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
955 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
956 model_column
, width
, align
, flags
);
957 PrependColumn( ret
);
962 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
963 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
965 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
966 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
967 model_column
, width
, align
, flags
);
968 PrependColumn( ret
);
973 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
974 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
976 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
977 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
978 model_column
, width
, align
, flags
);
979 PrependColumn( ret
);
984 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
986 col
->SetOwner( (wxDataViewCtrl
*) this );
991 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
993 col
->SetOwner( (wxDataViewCtrl
*) this );
997 // ---------------------------------------------------------
999 // ---------------------------------------------------------
1001 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
1003 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
)
1005 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
1006 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
1007 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
1008 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
1009 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
1010 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
1011 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
1012 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
)
1014 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
1015 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
1016 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
1019 // -------------------------------------
1020 // wxDataViewSpinRenderer
1021 // -------------------------------------
1023 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
1024 wxDataViewCustomRenderer( "long", mode
, alignment
)
1030 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1033 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
1034 labelRect
.GetTopLeft(), labelRect
.GetSize(), wxSP_ARROW_KEYS
, m_min
, m_max
, l
);
1037 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1039 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1040 long l
= sc
->GetValue();
1045 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1048 str
.Printf( "%d", (int) m_data
);
1049 RenderText( str
, 0, rect
, dc
, state
);
1053 wxSize
wxDataViewSpinRenderer::GetSize() const
1055 return wxSize(80,16);
1058 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1060 m_data
= value
.GetLong();
1064 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1070 //-----------------------------------------------------------------------------
1071 // wxDataViewTreeStore
1072 //-----------------------------------------------------------------------------
1074 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1075 wxDataViewTreeStoreNode
*parent
,
1076 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1084 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1090 #include "wx/listimpl.cpp"
1091 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
)
1093 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1094 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1095 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1096 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1098 m_iconExpanded
= expanded
;
1099 m_children
.DeleteContents(true);
1102 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1106 //-----------------------------------------------------------------------------
1108 wxDataViewTreeStore::wxDataViewTreeStore()
1110 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1113 wxDataViewTreeStore::~wxDataViewTreeStore()
1118 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1119 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1121 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1122 if (!parent_node
) return wxDataViewItem(0);
1124 wxDataViewTreeStoreNode
*node
=
1125 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1126 parent_node
->GetChildren().Append( node
);
1129 ItemAdded( parent
, node
->GetItem() );
1131 return node
->GetItem();
1134 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1135 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1137 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1138 if (!parent_node
) return wxDataViewItem(0);
1140 wxDataViewTreeStoreNode
*node
=
1141 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1142 parent_node
->GetChildren().Insert( node
);
1145 ItemAdded( parent
, node
->GetItem() );
1147 return node
->GetItem();
1151 wxDataViewTreeStore::InsertItem(const wxDataViewItem
& WXUNUSED(parent
),
1152 const wxDataViewItem
& WXUNUSED(previous
),
1153 const wxString
& WXUNUSED(text
),
1154 const wxIcon
& WXUNUSED(icon
),
1155 wxClientData
* WXUNUSED(data
))
1157 return wxDataViewItem(0);
1160 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1161 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1162 wxClientData
*data
)
1164 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1165 if (!parent_node
) return wxDataViewItem(0);
1167 wxDataViewTreeStoreContainerNode
*node
=
1168 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1169 parent_node
->GetChildren().Insert( node
);
1172 ItemAdded( parent
, node
->GetItem() );
1174 return node
->GetItem();
1178 wxDataViewTreeStore::AppendContainer(const wxDataViewItem
& parent
,
1179 const wxString
&text
,
1181 const wxIcon
& expanded
,
1182 wxClientData
* data
)
1184 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1185 if (!parent_node
) return wxDataViewItem(0);
1187 wxDataViewTreeStoreContainerNode
*node
=
1188 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1189 parent_node
->GetChildren().Append( node
);
1192 ItemAdded( parent
, node
->GetItem() );
1194 return node
->GetItem();
1198 wxDataViewTreeStore::InsertContainer(const wxDataViewItem
& WXUNUSED(parent
),
1199 const wxDataViewItem
& WXUNUSED(previous
),
1200 const wxString
& WXUNUSED(text
),
1201 const wxIcon
& WXUNUSED(icon
),
1202 const wxIcon
& WXUNUSED(expanded
),
1203 wxClientData
* WXUNUSED(data
))
1205 return wxDataViewItem(0);
1208 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1210 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1211 if (!parent_node
) return wxDataViewItem(0);
1213 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1215 return node
->GetData();
1217 return wxDataViewItem(0);
1220 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1222 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1223 if (!node
) return -1;
1225 if (!node
->IsContainer())
1228 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1229 return (int) container_node
->GetChildren().GetCount();
1232 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1234 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1237 node
->SetText( text
);
1240 ValueChanged( item
, 0 );
1243 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1245 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1246 if (!node
) return wxEmptyString
;
1248 return node
->GetText();
1251 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1253 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1256 node
->SetIcon( icon
);
1259 ValueChanged( item
, 0 );
1262 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1264 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1265 if (!node
) return wxNullIcon
;
1267 return node
->GetIcon();
1270 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1272 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1275 node
->SetExpandedIcon( icon
);
1278 ValueChanged( item
, 0 );
1281 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1283 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1284 if (!node
) return wxNullIcon
;
1286 return node
->GetExpandedIcon();
1289 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1291 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1294 node
->SetData( data
);
1296 // notify control? only sensible when sorting on client data
1297 // ValueChanged( item, 0 );
1300 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1302 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1303 if (!node
) return NULL
;
1305 return node
->GetData();
1308 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1310 if (!item
.IsOk()) return;
1312 wxDataViewItem parent_item
= GetParent( item
);
1314 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1315 if (!parent_node
) return;
1317 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1320 parent_node
->GetChildren().DeleteObject( node
);
1323 ItemDeleted( parent_item
, item
);
1326 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1328 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1331 wxDataViewItemArray array
;
1332 wxDataViewTreeStoreNodeList::iterator iter
;
1333 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1335 wxDataViewTreeStoreNode
* child
= *iter
;
1336 array
.Add( child
->GetItem() );
1339 node
->GetChildren().clear();
1342 ItemsDeleted( item
, array
);
1345 void wxDataViewTreeStore::DeleteAllItems()
1351 wxDataViewTreeStore::GetValue(wxVariant
&variant
,
1352 const wxDataViewItem
&item
,
1353 unsigned int WXUNUSED(col
)) const
1355 // if (col != 0) return;
1357 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1360 wxDataViewIconText
data( node
->GetText(), node
->GetIcon() );
1366 wxDataViewTreeStore::SetValue(const wxVariant
& variant
,
1367 const wxDataViewItem
& item
,
1368 unsigned int WXUNUSED(col
))
1370 // if (col != 0) return false;
1372 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1373 if (!node
) return false;
1375 wxDataViewIconText data
;
1379 node
->SetText( data
.GetText() );
1380 node
->SetIcon( data
.GetIcon() );
1385 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1387 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1388 if (!node
) return wxDataViewItem(0);
1390 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1391 if (!parent
) return wxDataViewItem(0);
1393 if (parent
== m_root
)
1394 return wxDataViewItem(0);
1396 return parent
->GetItem();
1399 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
&item
) const
1401 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1402 if (!node
) return false;
1404 return node
->IsContainer();
1407 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1409 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1410 if (!node
) return 0;
1412 wxDataViewTreeStoreNodeList::iterator iter
;
1413 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1415 wxDataViewTreeStoreNode
* child
= *iter
;
1416 children
.Add( child
->GetItem() );
1419 return node
->GetChildren().GetCount();
1422 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
1423 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) )
1425 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
1426 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
1428 if (!node1
|| !node2
)
1431 wxDataViewTreeStoreContainerNode
* parent1
=
1432 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
1433 wxDataViewTreeStoreContainerNode
* parent2
=
1434 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
1436 if (parent1
!= parent2
)
1438 wxLogError( wxT("Comparing items with different parent.") );
1442 if (node1
->IsContainer() && !!node2
->IsContainer())
1445 if (node2
->IsContainer() && !!node1
->IsContainer())
1448 return parent1
->GetChildren().IndexOf( node1
) - parent1
->GetChildren().IndexOf( node2
);
1451 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
1456 return (wxDataViewTreeStoreNode
*) item
.GetID();
1459 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
1462 return (wxDataViewTreeStoreContainerNode
*) m_root
;
1464 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
1466 if (!node
->IsContainer())
1469 return (wxDataViewTreeStoreContainerNode
*) node
;
1472 #endif // wxUSE_DATAVIEWCTRL