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
)
310 // IDs are ordered until an item gets deleted or inserted
313 // build initial index
315 for (i
= 1; i
< initial_size
+1; i
++)
316 m_hash
.Add( (void*) i
);
317 m_lastIndex
= initial_size
+ 1;
321 m_lastIndex
= initial_size
-1;
325 wxDataViewIndexListModel::~wxDataViewIndexListModel()
329 void wxDataViewIndexListModel::RowPrepended()
335 unsigned int id
= m_lastIndex
++;
336 m_hash
.Insert( (void*) id
, 0 );
337 wxDataViewItem
item( (void*) id
);
338 ItemAdded( wxDataViewItem(0), item
);
343 wxDataViewItem
item( (void*) 0 );
344 ItemAdded( wxDataViewItem(0), item
);
348 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
354 unsigned int id
= m_lastIndex
++;
355 m_hash
.Insert( (void*) id
, before
);
356 wxDataViewItem
item( (void*) id
);
357 ItemAdded( wxDataViewItem(0), item
);
362 wxDataViewItem
item( (void*) before
);
363 ItemAdded( wxDataViewItem(0), item
);
367 void wxDataViewIndexListModel::RowAppended()
371 unsigned int id
= m_lastIndex
++;
372 m_hash
.Add( (void*) id
);
373 wxDataViewItem
item( (void*) id
);
374 ItemAdded( wxDataViewItem(0), item
);
379 wxDataViewItem
item( (void*) m_lastIndex
);
380 ItemAdded( wxDataViewItem(0), item
);
384 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
388 wxDataViewItem
item( m_hash
[row
] );
389 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
390 m_hash
.RemoveAt( row
);
394 wxDataViewItem
item( (void*) row
);
395 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
400 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
402 wxDataViewModel::ItemChanged( GetItem(row
) );
405 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
407 wxDataViewModel::ValueChanged( GetItem(row
), col
);
410 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
416 unsigned int pos
= wxPtrToUInt( item
.GetID() );
420 // assert for not found
421 return (unsigned int) m_hash
.Index( item
.GetID() );
425 return wxPtrToUInt( item
.GetID() );
429 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
433 wxASSERT( row
< m_hash
.GetCount() );
434 return wxDataViewItem( m_hash
[row
] );
438 return wxDataViewItem( (void*) row
);
442 bool wxDataViewIndexListModel::HasDefaultCompare() const
447 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
448 const wxDataViewItem
& item2
,
449 unsigned int WXUNUSED(column
),
452 if (m_ordered
|| !m_useHash
)
454 unsigned int pos1
= wxPtrToUInt(item1
.GetID());
455 unsigned int pos2
= wxPtrToUInt(item2
.GetID());
464 return GetRow(item1
) - GetRow(item2
);
466 return GetRow(item2
) - GetRow(item1
);
469 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
470 const wxDataViewItem
&item
, unsigned int col
) const
472 GetValue( variant
, GetRow(item
), col
);
475 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
476 const wxDataViewItem
&item
, unsigned int col
)
478 return SetValue( variant
, GetRow(item
), col
);
481 bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem
&item
, unsigned int col
, wxDataViewItemAttr
&attr
)
483 return GetAttr( GetRow(item
), col
, attr
);
486 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
488 return wxDataViewItem(0);
491 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
493 // only the invisible root item has children
500 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
510 return m_hash
.GetCount();
513 //-----------------------------------------------------------------------------
514 // wxDataViewIconText
515 //-----------------------------------------------------------------------------
517 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
519 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
521 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
523 if (one
.GetText() != two
.GetText()) return false;
524 if (one
.IsSameAs(two
)) return false;
528 // ---------------------------------------------------------
529 // wxDataViewRendererBase
530 // ---------------------------------------------------------
532 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
534 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
535 wxDataViewCellMode
WXUNUSED(mode
),
536 int WXUNUSED(align
) )
538 m_variantType
= varianttype
;
542 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
544 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
547 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
549 m_item
= item
; // remember for later
551 unsigned int col
= GetOwner()->GetModelColumn();
553 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
555 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
557 wxDataViewEditorCtrlEvtHandler
*handler
=
558 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
560 m_editorCtrl
->PushEventHandler( handler
);
562 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
563 handler
->SetFocusOnIdle();
565 m_editorCtrl
->SetFocus();
568 // Now we should send Editing Started event
569 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
570 event
.SetDataViewColumn( GetOwner() );
571 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
572 event
.SetItem( item
);
573 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
578 void wxDataViewRendererBase::CancelEditing()
580 wxPendingDelete
.Append( m_editorCtrl
);
582 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
584 // m_editorCtrl->PopEventHandler( true );
587 bool wxDataViewRendererBase::FinishEditing()
590 GetValueFromEditorCtrl( m_editorCtrl
, value
);
592 wxPendingDelete
.Append( m_editorCtrl
);
594 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
596 if (!Validate(value
))
599 unsigned int col
= GetOwner()->GetModelColumn();
600 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
601 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
603 // m_editorCtrl->PopEventHandler( true );
605 // Now we should send Editing Done event
606 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
607 event
.SetDataViewColumn( GetOwner() );
608 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
609 event
.SetItem( m_item
);
610 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
615 //-----------------------------------------------------------------------------
616 // wxDataViewEditorCtrlEvtHandler
617 //-----------------------------------------------------------------------------
619 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
620 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
621 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
622 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
625 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
626 wxControl
*editorCtrl
,
627 wxDataViewRenderer
*owner
)
630 m_editorCtrl
= editorCtrl
;
635 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
639 m_focusOnIdle
= false;
640 if (wxWindow::FindFocus() != m_editorCtrl
)
641 m_editorCtrl
->SetFocus();
647 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
649 switch ( event
.m_keyCode
)
653 m_owner
->FinishEditing();
658 m_owner
->CancelEditing();
666 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
671 m_owner
->FinishEditing();
677 // ---------------------------------------------------------
678 // wxDataViewColumnBase
679 // ---------------------------------------------------------
681 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
683 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
684 wxDataViewRenderer
*renderer
,
685 unsigned int model_column
,
687 wxAlignment
WXUNUSED(align
),
690 m_renderer
= renderer
;
691 m_model_column
= model_column
;
693 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
695 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
696 // parameters inside the native control!
699 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
700 wxDataViewRenderer
*renderer
,
701 unsigned int model_column
,
703 wxAlignment
WXUNUSED(align
),
704 int WXUNUSED(flags
) )
706 m_renderer
= renderer
;
707 m_model_column
= model_column
;
710 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
713 wxDataViewColumnBase::~wxDataViewColumnBase()
719 int wxDataViewColumnBase::GetFlags() const
724 ret
|= wxDATAVIEW_COL_SORTABLE
;
726 ret
|= wxDATAVIEW_COL_RESIZABLE
;
728 ret
|= wxDATAVIEW_COL_HIDDEN
;
733 void wxDataViewColumnBase::SetFlags(int flags
)
735 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
736 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
737 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
740 // ---------------------------------------------------------
741 // wxDataViewCtrlBase
742 // ---------------------------------------------------------
744 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
746 wxDataViewCtrlBase::wxDataViewCtrlBase()
749 m_expander_column
= 0;
753 wxDataViewCtrlBase::~wxDataViewCtrlBase()
762 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
766 m_model
->DecRef(); // discard old model, if any
769 // add our own reference to the new model:
779 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
785 wxDataViewCtrlBase::AppendTextColumn( const wxString
&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 wxString
&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 wxString
&label
, unsigned int model_column
,
808 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
811 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
812 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
813 model_column
, width
, align
, flags
);
819 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
820 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
822 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
823 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
824 model_column
, width
, align
, flags
);
830 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
831 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
833 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
834 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
835 model_column
, width
, align
, flags
);
841 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
842 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
844 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
845 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
846 model_column
, width
, align
, flags
);
852 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
853 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
855 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
856 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
857 model_column
, width
, align
, flags
);
863 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
864 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
866 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
867 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
868 model_column
, width
, align
, flags
);
874 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
875 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
);
885 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&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
);
896 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&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
);
907 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&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
);
918 wxDataViewCtrlBase::PrependTextColumn( const wxString
&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 wxString
&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 wxString
&label
, unsigned int model_column
,
941 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
944 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
945 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
946 model_column
, width
, align
, flags
);
947 PrependColumn( ret
);
952 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
953 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
955 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
956 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
957 model_column
, width
, align
, flags
);
958 PrependColumn( ret
);
963 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
964 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
966 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
967 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
968 model_column
, width
, align
, flags
);
969 PrependColumn( ret
);
974 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
975 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
977 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
978 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
979 model_column
, width
, align
, flags
);
980 PrependColumn( ret
);
985 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
986 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
988 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
989 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
990 model_column
, width
, align
, flags
);
991 PrependColumn( ret
);
996 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
997 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
999 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1000 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
1001 model_column
, width
, align
, flags
);
1002 PrependColumn( ret
);
1007 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
1008 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1010 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1011 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
1012 model_column
, width
, align
, flags
);
1013 PrependColumn( ret
);
1018 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
1019 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1021 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1022 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
1023 model_column
, width
, align
, flags
);
1024 PrependColumn( ret
);
1029 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
1030 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1032 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1033 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
1034 model_column
, width
, align
, flags
);
1035 PrependColumn( ret
);
1040 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
1041 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1043 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1044 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
1045 model_column
, width
, align
, flags
);
1046 PrependColumn( ret
);
1051 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
1053 col
->SetOwner( (wxDataViewCtrl
*) this );
1058 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
1060 col
->SetOwner( (wxDataViewCtrl
*) this );
1064 // ---------------------------------------------------------
1066 // ---------------------------------------------------------
1068 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
1070 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
)
1072 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
1073 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
1074 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
1075 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
1076 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
1077 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
1078 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
1079 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
)
1081 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
1082 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
1083 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
1086 // -------------------------------------
1087 // wxDataViewSpinRenderer
1088 // -------------------------------------
1090 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
1091 wxDataViewCustomRenderer( "long", mode
, alignment
)
1097 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1100 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
1101 labelRect
.GetTopLeft(), labelRect
.GetSize(), wxSP_ARROW_KEYS
, m_min
, m_max
, l
);
1104 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1106 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1107 long l
= sc
->GetValue();
1112 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1115 str
.Printf( "%d", (int) m_data
);
1116 RenderText( str
, 0, rect
, dc
, state
);
1120 wxSize
wxDataViewSpinRenderer::GetSize() const
1122 return wxSize(80,16);
1125 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1127 m_data
= value
.GetLong();
1131 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1137 //-----------------------------------------------------------------------------
1138 // wxDataViewTreeStore
1139 //-----------------------------------------------------------------------------
1141 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1142 wxDataViewTreeStoreNode
*parent
,
1143 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1151 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1157 #include "wx/listimpl.cpp"
1158 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
)
1160 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1161 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1162 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1163 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1165 m_iconExpanded
= expanded
;
1166 m_children
.DeleteContents(true);
1169 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1173 //-----------------------------------------------------------------------------
1175 wxDataViewTreeStore::wxDataViewTreeStore()
1177 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1180 wxDataViewTreeStore::~wxDataViewTreeStore()
1185 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1186 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1188 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1189 if (!parent_node
) return wxDataViewItem(0);
1191 wxDataViewTreeStoreNode
*node
=
1192 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1193 parent_node
->GetChildren().Append( node
);
1196 ItemAdded( parent
, node
->GetItem() );
1198 return node
->GetItem();
1201 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1202 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1204 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1205 if (!parent_node
) return wxDataViewItem(0);
1207 wxDataViewTreeStoreNode
*node
=
1208 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1209 parent_node
->GetChildren().Insert( node
);
1212 ItemAdded( parent
, node
->GetItem() );
1214 return node
->GetItem();
1218 wxDataViewTreeStore::InsertItem(const wxDataViewItem
& WXUNUSED(parent
),
1219 const wxDataViewItem
& WXUNUSED(previous
),
1220 const wxString
& WXUNUSED(text
),
1221 const wxIcon
& WXUNUSED(icon
),
1222 wxClientData
* WXUNUSED(data
))
1224 return wxDataViewItem(0);
1227 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1228 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1229 wxClientData
*data
)
1231 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1232 if (!parent_node
) return wxDataViewItem(0);
1234 wxDataViewTreeStoreContainerNode
*node
=
1235 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1236 parent_node
->GetChildren().Insert( node
);
1239 ItemAdded( parent
, node
->GetItem() );
1241 return node
->GetItem();
1245 wxDataViewTreeStore::AppendContainer(const wxDataViewItem
& parent
,
1246 const wxString
&text
,
1248 const wxIcon
& expanded
,
1249 wxClientData
* data
)
1251 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1252 if (!parent_node
) return wxDataViewItem(0);
1254 wxDataViewTreeStoreContainerNode
*node
=
1255 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1256 parent_node
->GetChildren().Append( node
);
1259 ItemAdded( parent
, node
->GetItem() );
1261 return node
->GetItem();
1265 wxDataViewTreeStore::InsertContainer(const wxDataViewItem
& WXUNUSED(parent
),
1266 const wxDataViewItem
& WXUNUSED(previous
),
1267 const wxString
& WXUNUSED(text
),
1268 const wxIcon
& WXUNUSED(icon
),
1269 const wxIcon
& WXUNUSED(expanded
),
1270 wxClientData
* WXUNUSED(data
))
1272 return wxDataViewItem(0);
1275 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1277 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1278 if (!parent_node
) return wxDataViewItem(0);
1280 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1282 return node
->GetData();
1284 return wxDataViewItem(0);
1287 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1289 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1290 if (!node
) return -1;
1292 if (!node
->IsContainer())
1295 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1296 return (int) container_node
->GetChildren().GetCount();
1299 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1301 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1304 node
->SetText( text
);
1307 ValueChanged( item
, 0 );
1310 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1312 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1313 if (!node
) return wxEmptyString
;
1315 return node
->GetText();
1318 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1320 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1323 node
->SetIcon( icon
);
1326 ValueChanged( item
, 0 );
1329 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1331 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1332 if (!node
) return wxNullIcon
;
1334 return node
->GetIcon();
1337 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1339 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1342 node
->SetExpandedIcon( icon
);
1345 ValueChanged( item
, 0 );
1348 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1350 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1351 if (!node
) return wxNullIcon
;
1353 return node
->GetExpandedIcon();
1356 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1358 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1361 node
->SetData( data
);
1363 // notify control? only sensible when sorting on client data
1364 // ValueChanged( item, 0 );
1367 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1369 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1370 if (!node
) return NULL
;
1372 return node
->GetData();
1375 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1377 if (!item
.IsOk()) return;
1379 wxDataViewItem parent_item
= GetParent( item
);
1381 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1382 if (!parent_node
) return;
1384 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1387 parent_node
->GetChildren().DeleteObject( node
);
1390 ItemDeleted( parent_item
, item
);
1393 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1395 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1398 wxDataViewItemArray array
;
1399 wxDataViewTreeStoreNodeList::iterator iter
;
1400 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1402 wxDataViewTreeStoreNode
* child
= *iter
;
1403 array
.Add( child
->GetItem() );
1406 node
->GetChildren().clear();
1409 ItemsDeleted( item
, array
);
1412 void wxDataViewTreeStore::DeleteAllItems()
1418 wxDataViewTreeStore::GetValue(wxVariant
&variant
,
1419 const wxDataViewItem
&item
,
1420 unsigned int WXUNUSED(col
)) const
1422 // if (col != 0) return;
1424 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1427 wxDataViewIconText
data( node
->GetText(), node
->GetIcon() );
1433 wxDataViewTreeStore::SetValue(const wxVariant
& variant
,
1434 const wxDataViewItem
& item
,
1435 unsigned int WXUNUSED(col
))
1437 // if (col != 0) return false;
1439 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1440 if (!node
) return false;
1442 wxDataViewIconText data
;
1446 node
->SetText( data
.GetText() );
1447 node
->SetIcon( data
.GetIcon() );
1452 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1454 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1455 if (!node
) return wxDataViewItem(0);
1457 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1458 if (!parent
) return wxDataViewItem(0);
1460 if (parent
== m_root
)
1461 return wxDataViewItem(0);
1463 return parent
->GetItem();
1466 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
&item
) const
1468 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1469 if (!node
) return false;
1471 return node
->IsContainer();
1474 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1476 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1477 if (!node
) return 0;
1479 wxDataViewTreeStoreNodeList::iterator iter
;
1480 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1482 wxDataViewTreeStoreNode
* child
= *iter
;
1483 children
.Add( child
->GetItem() );
1486 return node
->GetChildren().GetCount();
1489 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
1490 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) )
1492 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
1493 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
1495 if (!node1
|| !node2
)
1498 wxDataViewTreeStoreContainerNode
* parent1
=
1499 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
1500 wxDataViewTreeStoreContainerNode
* parent2
=
1501 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
1503 if (parent1
!= parent2
)
1505 wxLogError( wxT("Comparing items with different parent.") );
1509 if (node1
->IsContainer() && !!node2
->IsContainer())
1512 if (node2
->IsContainer() && !!node1
->IsContainer())
1515 return parent1
->GetChildren().IndexOf( node1
) - parent1
->GetChildren().IndexOf( node2
);
1518 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
1523 return (wxDataViewTreeStoreNode
*) item
.GetID();
1526 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
1529 return (wxDataViewTreeStoreContainerNode
*) m_root
;
1531 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
1533 if (!node
->IsContainer())
1536 return (wxDataViewTreeStoreContainerNode
*) node
;
1539 #endif // wxUSE_DATAVIEWCTRL