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"
24 #include "wx/settings.h"
31 const wxChar wxDataViewCtrlNameStr
[] = wxT("dataviewCtrl");
34 bool operator == (const wxDataViewItem
&left
, const wxDataViewItem
&right
)
36 return (left
.GetID() == right
.GetID() );
40 // ---------------------------------------------------------
41 // wxDataViewModelNotifier
42 // ---------------------------------------------------------
44 #include "wx/listimpl.cpp"
45 WX_DEFINE_LIST(wxDataViewModelNotifiers
);
47 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
49 size_t count
= items
.GetCount();
51 for (i
= 0; i
< count
; i
++)
52 if (!ItemAdded( parent
, items
[i
] )) return false;
57 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
59 size_t count
= items
.GetCount();
61 for (i
= 0; i
< count
; i
++)
62 if (!ItemDeleted( parent
, items
[i
] )) return false;
67 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray
&items
)
69 size_t count
= items
.GetCount();
71 for (i
= 0; i
< count
; i
++)
72 if (!ItemChanged( items
[i
] )) return false;
77 // ---------------------------------------------------------
79 // ---------------------------------------------------------
81 wxDataViewModel::wxDataViewModel()
83 m_notifiers
.DeleteContents( true );
86 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
90 wxDataViewModelNotifiers::iterator iter
;
91 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
93 wxDataViewModelNotifier
* notifier
= *iter
;
94 if (!notifier
->ItemAdded( parent
, item
))
101 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
105 wxDataViewModelNotifiers::iterator iter
;
106 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
108 wxDataViewModelNotifier
* notifier
= *iter
;
109 if (!notifier
->ItemDeleted( parent
, item
))
116 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
120 wxDataViewModelNotifiers::iterator iter
;
121 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
123 wxDataViewModelNotifier
* notifier
= *iter
;
124 if (!notifier
->ItemChanged( item
))
131 bool wxDataViewModel::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
135 wxDataViewModelNotifiers::iterator iter
;
136 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
138 wxDataViewModelNotifier
* notifier
= *iter
;
139 if (!notifier
->ItemsAdded( parent
, items
))
146 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
150 wxDataViewModelNotifiers::iterator iter
;
151 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
153 wxDataViewModelNotifier
* notifier
= *iter
;
154 if (!notifier
->ItemsDeleted( parent
, items
))
161 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray
&items
)
165 wxDataViewModelNotifiers::iterator iter
;
166 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
168 wxDataViewModelNotifier
* notifier
= *iter
;
169 if (!notifier
->ItemsChanged( items
))
176 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
180 wxDataViewModelNotifiers::iterator iter
;
181 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
183 wxDataViewModelNotifier
* notifier
= *iter
;
184 if (!notifier
->ValueChanged( item
, col
))
191 bool wxDataViewModel::Cleared()
195 wxDataViewModelNotifiers::iterator iter
;
196 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
198 wxDataViewModelNotifier
* notifier
= *iter
;
199 if (!notifier
->Cleared())
206 void wxDataViewModel::Resort()
208 wxDataViewModelNotifiers::iterator iter
;
209 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
211 wxDataViewModelNotifier
* notifier
= *iter
;
216 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
218 m_notifiers
.push_back( notifier
);
219 notifier
->SetOwner( this );
222 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
224 m_notifiers
.DeleteObject( notifier
);
227 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
228 unsigned int column
, bool ascending
)
230 // sort branches before leaves
231 bool item1_is_container
= IsContainer(item1
);
232 bool item2_is_container
= IsContainer(item2
);
234 if (item1_is_container
&& !item2_is_container
)
236 if (item2_is_container
&& !item1_is_container
)
239 wxVariant value1
,value2
;
240 GetValue( value1
, item1
, column
);
241 GetValue( value2
, item2
, column
);
245 wxVariant temp
= value1
;
250 if (value1
.GetType() == wxT("string"))
252 wxString str1
= value1
.GetString();
253 wxString str2
= value2
.GetString();
254 int res
= str1
.Cmp( str2
);
257 if (value1
.GetType() == wxT("long"))
259 long l1
= value1
.GetLong();
260 long l2
= value2
.GetLong();
264 if (value1
.GetType() == wxT("double"))
266 double d1
= value1
.GetDouble();
267 double d2
= value2
.GetDouble();
268 if (d1
< d2
) return 1;
269 if (d1
> d2
) return -1;
271 if (value1
.GetType() == wxT("datetime"))
273 wxDateTime dt1
= value1
.GetDateTime();
274 wxDateTime dt2
= value2
.GetDateTime();
275 if (dt1
.IsEarlierThan(dt2
)) return 1;
276 if (dt2
.IsEarlierThan(dt1
)) return -11;
279 // items must be different
280 unsigned long litem1
= (unsigned long) item1
.GetID();
281 unsigned long litem2
= (unsigned long) item2
.GetID();
284 return litem2
-litem2
;
286 return litem1
-litem2
;
289 // ---------------------------------------------------------
290 // wxDataViewIndexListModel
291 // ---------------------------------------------------------
293 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
295 // build initial index
297 for (i
= 1; i
< initial_size
+1; i
++)
298 m_hash
.Add( (void*) i
);
299 m_lastIndex
= initial_size
+ 1;
302 wxDataViewIndexListModel::~wxDataViewIndexListModel()
306 void wxDataViewIndexListModel::RowPrepended()
308 unsigned int id
= m_lastIndex
++;
309 m_hash
.Insert( (void*) id
, 0 );
310 wxDataViewItem
item( (void*) id
);
311 ItemAdded( wxDataViewItem(0), item
);
314 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
316 unsigned int id
= m_lastIndex
++;
317 m_hash
.Insert( (void*) id
, before
);
318 wxDataViewItem
item( (void*) id
);
319 ItemAdded( wxDataViewItem(0), item
);
322 void wxDataViewIndexListModel::RowAppended()
324 unsigned int id
= m_lastIndex
++;
325 m_hash
.Add( (void*) id
);
326 wxDataViewItem
item( (void*) id
);
327 ItemAdded( wxDataViewItem(0), item
);
330 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
332 wxDataViewItem
item( m_hash
[row
] );
333 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
334 m_hash
.RemoveAt( row
);
337 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
339 wxDataViewModel::ItemChanged( GetItem(row
) );
342 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
344 wxDataViewModel::ValueChanged( GetItem(row
), col
);
347 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
349 // assert for not found
350 return (unsigned int) m_hash
.Index( item
.GetID() );
353 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
355 wxASSERT( row
< m_hash
.GetCount() );
356 return wxDataViewItem( m_hash
[row
] );
359 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
360 const wxDataViewItem
& item2
,
361 unsigned int WXUNUSED(column
),
365 return GetRow(item1
) - GetRow(item2
);
367 return GetRow(item2
) - GetRow(item1
);
370 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
371 const wxDataViewItem
&item
, unsigned int col
) const
373 GetValue( variant
, GetRow(item
), col
);
376 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
377 const wxDataViewItem
&item
, unsigned int col
)
379 return SetValue( variant
, GetRow(item
), col
);
382 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
384 return wxDataViewItem(0);
387 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
389 // only the invisible root item has children
396 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
403 return m_hash
.GetCount();
406 //-----------------------------------------------------------------------------
407 // wxDataViewIconText
408 //-----------------------------------------------------------------------------
410 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
412 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
414 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
416 if (one
.GetText() != two
.GetText()) return false;
417 if (one
.IsSameAs(two
)) return false;
421 // ---------------------------------------------------------
422 // wxDataViewRendererBase
423 // ---------------------------------------------------------
425 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
427 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
428 wxDataViewCellMode
WXUNUSED(mode
),
429 int WXUNUSED(align
) )
431 m_variantType
= varianttype
;
435 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
437 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
440 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
442 m_item
= item
; // remember for later
444 unsigned int col
= GetOwner()->GetModelColumn();
446 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
448 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
450 wxDataViewEditorCtrlEvtHandler
*handler
=
451 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
453 m_editorCtrl
->PushEventHandler( handler
);
455 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
456 handler
->SetFocusOnIdle();
458 m_editorCtrl
->SetFocus();
461 // Now we should send Editing Started event
462 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
463 event
.SetDataViewColumn( GetOwner() );
464 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
465 event
.SetItem( item
);
466 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
471 void wxDataViewRendererBase::CancelEditing()
473 wxPendingDelete
.Append( m_editorCtrl
);
475 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
477 // m_editorCtrl->PopEventHandler( true );
480 bool wxDataViewRendererBase::FinishEditing()
483 GetValueFromEditorCtrl( m_editorCtrl
, value
);
485 wxPendingDelete
.Append( m_editorCtrl
);
487 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
489 if (!Validate(value
))
492 unsigned int col
= GetOwner()->GetModelColumn();
493 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
494 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
496 // m_editorCtrl->PopEventHandler( true );
498 // Now we should send Editing Done event
499 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
500 event
.SetDataViewColumn( GetOwner() );
501 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
502 event
.SetItem( m_item
);
503 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
508 //-----------------------------------------------------------------------------
509 // wxDataViewEditorCtrlEvtHandler
510 //-----------------------------------------------------------------------------
512 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
513 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
514 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
515 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
518 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
519 wxControl
*editorCtrl
,
520 wxDataViewRenderer
*owner
)
523 m_editorCtrl
= editorCtrl
;
528 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
532 m_focusOnIdle
= false;
533 if (wxWindow::FindFocus() != m_editorCtrl
)
534 m_editorCtrl
->SetFocus();
540 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
542 switch ( event
.m_keyCode
)
546 m_owner
->FinishEditing();
551 m_owner
->CancelEditing();
559 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
564 m_owner
->FinishEditing();
570 // ---------------------------------------------------------
571 // wxDataViewColumnBase
572 // ---------------------------------------------------------
574 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
576 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
577 wxDataViewRenderer
*renderer
,
578 unsigned int model_column
,
580 wxAlignment
WXUNUSED(align
),
583 m_renderer
= renderer
;
584 m_model_column
= model_column
;
586 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
588 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
589 // parameters inside the native control!
592 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
593 wxDataViewRenderer
*renderer
,
594 unsigned int model_column
,
596 wxAlignment
WXUNUSED(align
),
597 int WXUNUSED(flags
) )
599 m_renderer
= renderer
;
600 m_model_column
= model_column
;
603 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
606 wxDataViewColumnBase::~wxDataViewColumnBase()
612 int wxDataViewColumnBase::GetFlags() const
617 ret
|= wxDATAVIEW_COL_SORTABLE
;
619 ret
|= wxDATAVIEW_COL_RESIZABLE
;
621 ret
|= wxDATAVIEW_COL_HIDDEN
;
626 void wxDataViewColumnBase::SetFlags(int flags
)
628 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
629 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
630 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
633 // ---------------------------------------------------------
634 // wxDataViewCtrlBase
635 // ---------------------------------------------------------
637 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
639 wxDataViewCtrlBase::wxDataViewCtrlBase()
642 m_expander_column
= 0;
646 wxDataViewCtrlBase::~wxDataViewCtrlBase()
655 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
659 m_model
->DecRef(); // discard old model, if any
662 // add our own reference to the new model:
672 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
678 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
679 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
681 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
682 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
683 model_column
, width
, align
, flags
);
689 wxDataViewCtrlBase::AppendIconTextColumn( const wxString
&label
, unsigned int model_column
,
690 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
692 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
693 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
694 model_column
, width
, align
, flags
);
700 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
701 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
704 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
705 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
706 model_column
, width
, align
, flags
);
712 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
713 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
715 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
716 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
717 model_column
, width
, align
, flags
);
723 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
724 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
726 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
727 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
728 model_column
, width
, align
, flags
);
734 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
735 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
737 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
738 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
739 model_column
, width
, align
, flags
);
745 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
746 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
748 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
749 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
750 model_column
, width
, align
, flags
);
756 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
757 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
759 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
760 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
761 model_column
, width
, align
, flags
);
767 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
768 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
770 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
771 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
772 model_column
, width
, align
, flags
);
778 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
779 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
781 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
782 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
783 model_column
, width
, align
, flags
);
789 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
790 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
792 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
793 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
794 model_column
, width
, align
, flags
);
800 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
801 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
803 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
804 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
805 model_column
, width
, align
, flags
);
811 wxDataViewCtrlBase::PrependTextColumn( const wxString
&label
, unsigned int model_column
,
812 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
814 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
815 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
816 model_column
, width
, align
, flags
);
817 PrependColumn( ret
);
822 wxDataViewCtrlBase::PrependIconTextColumn( const wxString
&label
, unsigned int model_column
,
823 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
825 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
826 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
827 model_column
, width
, align
, flags
);
828 PrependColumn( ret
);
833 wxDataViewCtrlBase::PrependToggleColumn( const wxString
&label
, unsigned int model_column
,
834 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
837 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
838 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
839 model_column
, width
, align
, flags
);
840 PrependColumn( ret
);
845 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
846 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
848 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
849 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
850 model_column
, width
, align
, flags
);
851 PrependColumn( ret
);
856 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
857 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
859 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
860 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
861 model_column
, width
, align
, flags
);
862 PrependColumn( ret
);
867 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
868 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
870 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
871 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
872 model_column
, width
, align
, flags
);
873 PrependColumn( ret
);
878 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
879 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
881 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
882 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
883 model_column
, width
, align
, flags
);
884 PrependColumn( ret
);
889 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
890 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
892 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
893 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
894 model_column
, width
, align
, flags
);
895 PrependColumn( ret
);
900 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
901 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
903 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
904 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
905 model_column
, width
, align
, flags
);
906 PrependColumn( ret
);
911 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
912 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
914 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
915 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
916 model_column
, width
, align
, flags
);
917 PrependColumn( ret
);
922 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
923 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
925 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
926 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
927 model_column
, width
, align
, flags
);
928 PrependColumn( ret
);
933 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
934 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
936 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
937 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
938 model_column
, width
, align
, flags
);
939 PrependColumn( ret
);
944 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
946 col
->SetOwner( (wxDataViewCtrl
*) this );
951 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
953 col
->SetOwner( (wxDataViewCtrl
*) this );
957 // ---------------------------------------------------------
959 // ---------------------------------------------------------
961 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
963 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
)
965 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
966 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
967 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
968 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
969 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
970 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
971 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
972 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
)
974 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
975 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
976 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
979 // -------------------------------------
980 // wxDataViewSpinRenderer
981 // -------------------------------------
983 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
984 wxDataViewCustomRenderer( "long", mode
, alignment
)
990 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
993 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
994 labelRect
.GetTopLeft(), labelRect
.GetSize(), wxSP_ARROW_KEYS
, m_min
, m_max
, l
);
997 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
999 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1000 long l
= sc
->GetValue();
1005 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1008 str
.Printf( "%d", (int) m_data
);
1009 RenderText( str
, 0, rect
, dc
, state
);
1013 wxSize
wxDataViewSpinRenderer::GetSize() const
1015 return wxSize(80,16);
1018 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1020 m_data
= value
.GetLong();
1024 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1030 //-----------------------------------------------------------------------------
1031 // wxDataViewTreeStore
1032 //-----------------------------------------------------------------------------
1034 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1035 wxDataViewTreeStoreNode
*parent
,
1036 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1044 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1050 #include "wx/listimpl.cpp"
1051 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
);
1053 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1054 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1055 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1056 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1058 m_iconExpanded
= expanded
;
1059 m_children
.DeleteContents(true);
1062 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1066 //-----------------------------------------------------------------------------
1068 wxDataViewTreeStore::wxDataViewTreeStore()
1070 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1073 wxDataViewTreeStore::~wxDataViewTreeStore()
1078 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1079 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1081 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1082 if (!parent_node
) return wxDataViewItem(0);
1084 wxDataViewTreeStoreNode
*node
=
1085 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1086 parent_node
->GetChildren().Append( node
);
1089 ItemAdded( parent
, node
->GetItem() );
1091 return node
->GetItem();
1094 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1095 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1097 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1098 if (!parent_node
) return wxDataViewItem(0);
1100 wxDataViewTreeStoreNode
*node
=
1101 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1102 parent_node
->GetChildren().Insert( node
);
1105 ItemAdded( parent
, node
->GetItem() );
1107 return node
->GetItem();
1110 wxDataViewItem
wxDataViewTreeStore::InsertItem( const wxDataViewItem
& parent
, const wxDataViewItem
& previous
,
1111 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1113 return wxDataViewItem(0);
1116 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1117 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1118 wxClientData
*data
)
1120 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1121 if (!parent_node
) return wxDataViewItem(0);
1123 wxDataViewTreeStoreContainerNode
*node
=
1124 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1125 parent_node
->GetChildren().Insert( node
);
1128 ItemAdded( parent
, node
->GetItem() );
1130 return node
->GetItem();
1133 wxDataViewItem
wxDataViewTreeStore::AppendContainer( const wxDataViewItem
& parent
,
1134 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1135 wxClientData
*data
)
1137 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1138 if (!parent_node
) return wxDataViewItem(0);
1140 wxDataViewTreeStoreContainerNode
*node
=
1141 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1142 parent_node
->GetChildren().Append( node
);
1145 ItemAdded( parent
, node
->GetItem() );
1147 return node
->GetItem();
1150 wxDataViewItem
wxDataViewTreeStore::InsertContainer( const wxDataViewItem
& parent
, const wxDataViewItem
& previous
,
1151 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1152 wxClientData
*data
)
1154 return wxDataViewItem(0);
1157 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1159 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1160 if (!parent_node
) return wxDataViewItem(0);
1162 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1164 return node
->GetData();
1166 return wxDataViewItem(0);
1169 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1171 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1172 if (!node
) return -1;
1174 if (!node
->IsContainer())
1177 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1178 return (int) container_node
->GetChildren().GetCount();
1181 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1183 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1186 node
->SetText( text
);
1189 ValueChanged( item
, 0 );
1192 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1194 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1195 if (!node
) return wxEmptyString
;
1197 return node
->GetText();
1200 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1202 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1205 node
->SetIcon( icon
);
1208 ValueChanged( item
, 0 );
1211 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1213 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1214 if (!node
) return wxNullIcon
;
1216 return node
->GetIcon();
1219 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1221 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1224 node
->SetExpandedIcon( icon
);
1227 ValueChanged( item
, 0 );
1230 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1232 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1233 if (!node
) return wxNullIcon
;
1235 return node
->GetExpandedIcon();
1238 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1240 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1243 node
->SetData( data
);
1245 // notify control? only sensible when sorting on client data
1246 // ValueChanged( item, 0 );
1249 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1251 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1252 if (!node
) return NULL
;
1254 return node
->GetData();
1257 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1259 if (!item
.IsOk()) return;
1261 wxDataViewItem parent_item
= GetParent( item
);
1263 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1264 if (!parent_node
) return;
1266 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1269 parent_node
->GetChildren().DeleteObject( node
);
1272 ItemDeleted( parent_item
, item
);
1275 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1277 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1280 wxDataViewItemArray array
;
1281 wxDataViewTreeStoreNodeList::iterator iter
;
1282 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1284 wxDataViewTreeStoreNode
* child
= *iter
;
1285 array
.Add( child
->GetItem() );
1288 node
->GetChildren().clear();
1291 ItemsDeleted( item
, array
);
1294 void wxDataViewTreeStore::DeleteAllItems()
1299 void wxDataViewTreeStore::GetValue( wxVariant
&variant
,
1300 const wxDataViewItem
&item
, unsigned int col
) const
1302 // if (col != 0) return;
1304 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1307 wxDataViewIconText
data( node
->GetText(), node
->GetIcon() );
1312 bool wxDataViewTreeStore::SetValue( const wxVariant
&variant
,
1313 const wxDataViewItem
&item
, unsigned int col
)
1315 // if (col != 0) return false;
1317 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1318 if (!node
) return false;
1320 wxDataViewIconText data
;
1324 node
->SetText( data
.GetText() );
1325 node
->SetIcon( data
.GetIcon() );
1330 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1332 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1333 if (!node
) return wxDataViewItem(0);
1335 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1336 if (!parent
) return wxDataViewItem(0);
1338 if (parent
== m_root
)
1339 return wxDataViewItem(0);
1341 return parent
->GetItem();
1344 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
&item
) const
1346 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1347 if (!node
) return false;
1349 return node
->IsContainer();
1352 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1354 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1355 if (!node
) return 0;
1357 wxDataViewTreeStoreNodeList::iterator iter
;
1358 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1360 wxDataViewTreeStoreNode
* child
= *iter
;
1361 children
.Add( child
->GetItem() );
1364 return node
->GetChildren().GetCount();
1367 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
1368 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) )
1370 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
1371 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
1373 if (!node1
|| !node2
)
1376 wxDataViewTreeStoreContainerNode
* parent1
=
1377 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
1378 wxDataViewTreeStoreContainerNode
* parent2
=
1379 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
1381 if (parent1
!= parent2
)
1383 wxLogError( wxT("Comparing items with different parent.") );
1387 if (node1
->IsContainer() && !!node2
->IsContainer())
1390 if (node2
->IsContainer() && !!node1
->IsContainer())
1393 return parent1
->GetChildren().IndexOf( node1
) - parent1
->GetChildren().IndexOf( node2
);
1396 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
1401 return (wxDataViewTreeStoreNode
*) item
.GetID();
1404 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
1407 return (wxDataViewTreeStoreContainerNode
*) m_root
;
1409 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
1411 if (!node
->IsContainer())
1414 return (wxDataViewTreeStoreContainerNode
*) node
;