1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/datavcmn.cpp
3 // Purpose: wxDataViewCtrl base classes and common parts
4 // Author: Robert Roebling
7 // Copyright: (c) 2006, Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
18 #if wxUSE_DATAVIEWCTRL
20 #include "wx/dataview.h"
22 #include "wx/spinctrl.h"
26 #include "wx/settings.h"
32 const wxChar wxDataViewCtrlNameStr
[] = wxT("dataviewCtrl");
35 bool operator == (const wxDataViewItem
&left
, const wxDataViewItem
&right
)
37 return (left
.GetID() == right
.GetID() );
41 void wxDataViewItem::Print(const wxString
& text
) const
43 wxPrintf("item %s: %l\n", text
, (long)m_id
);
47 // ---------------------------------------------------------
48 // wxDataViewModelNotifier
49 // ---------------------------------------------------------
51 #include "wx/listimpl.cpp"
52 WX_DEFINE_LIST(wxDataViewModelNotifiers
)
54 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
56 size_t count
= items
.GetCount();
58 for (i
= 0; i
< count
; i
++)
59 if (!ItemAdded( parent
, items
[i
] )) return false;
64 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
66 size_t count
= items
.GetCount();
68 for (i
= 0; i
< count
; i
++)
69 if (!ItemDeleted( parent
, items
[i
] )) return false;
74 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray
&items
)
76 size_t count
= items
.GetCount();
78 for (i
= 0; i
< count
; i
++)
79 if (!ItemChanged( items
[i
] )) return false;
84 // ---------------------------------------------------------
86 // ---------------------------------------------------------
88 wxDataViewModel::wxDataViewModel()
90 m_notifiers
.DeleteContents( true );
93 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
97 wxDataViewModelNotifiers::iterator iter
;
98 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
100 wxDataViewModelNotifier
* notifier
= *iter
;
101 if (!notifier
->ItemAdded( parent
, item
))
108 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
112 wxDataViewModelNotifiers::iterator iter
;
113 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
115 wxDataViewModelNotifier
* notifier
= *iter
;
116 if (!notifier
->ItemDeleted( parent
, item
))
123 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
127 wxDataViewModelNotifiers::iterator iter
;
128 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
130 wxDataViewModelNotifier
* notifier
= *iter
;
131 if (!notifier
->ItemChanged( item
))
138 bool wxDataViewModel::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
142 wxDataViewModelNotifiers::iterator iter
;
143 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
145 wxDataViewModelNotifier
* notifier
= *iter
;
146 if (!notifier
->ItemsAdded( parent
, items
))
153 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
157 wxDataViewModelNotifiers::iterator iter
;
158 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
160 wxDataViewModelNotifier
* notifier
= *iter
;
161 if (!notifier
->ItemsDeleted( parent
, items
))
168 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray
&items
)
172 wxDataViewModelNotifiers::iterator iter
;
173 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
175 wxDataViewModelNotifier
* notifier
= *iter
;
176 if (!notifier
->ItemsChanged( items
))
183 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
187 wxDataViewModelNotifiers::iterator iter
;
188 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
190 wxDataViewModelNotifier
* notifier
= *iter
;
191 if (!notifier
->ValueChanged( item
, col
))
198 bool wxDataViewModel::Cleared()
202 wxDataViewModelNotifiers::iterator iter
;
203 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
205 wxDataViewModelNotifier
* notifier
= *iter
;
206 if (!notifier
->Cleared())
213 void wxDataViewModel::Resort()
215 wxDataViewModelNotifiers::iterator iter
;
216 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
218 wxDataViewModelNotifier
* notifier
= *iter
;
223 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
225 m_notifiers
.push_back( notifier
);
226 notifier
->SetOwner( this );
229 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
231 m_notifiers
.DeleteObject( notifier
);
234 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
235 unsigned int column
, bool ascending
)
237 // sort branches before leaves
238 bool item1_is_container
= IsContainer(item1
);
239 bool item2_is_container
= IsContainer(item2
);
241 if (item1_is_container
&& !item2_is_container
)
243 if (item2_is_container
&& !item1_is_container
)
246 wxVariant value1
,value2
;
247 GetValue( value1
, item1
, column
);
248 GetValue( value2
, item2
, column
);
252 wxVariant temp
= value1
;
257 if (value1
.GetType() == wxT("string"))
259 wxString str1
= value1
.GetString();
260 wxString str2
= value2
.GetString();
261 int res
= str1
.Cmp( str2
);
264 if (value1
.GetType() == wxT("long"))
266 long l1
= value1
.GetLong();
267 long l2
= value2
.GetLong();
271 if (value1
.GetType() == wxT("double"))
273 double d1
= value1
.GetDouble();
274 double d2
= value2
.GetDouble();
275 if (d1
< d2
) return 1;
276 if (d1
> d2
) return -1;
278 if (value1
.GetType() == wxT("datetime"))
280 wxDateTime dt1
= value1
.GetDateTime();
281 wxDateTime dt2
= value2
.GetDateTime();
282 if (dt1
.IsEarlierThan(dt2
)) return 1;
283 if (dt2
.IsEarlierThan(dt1
)) return -11;
286 // items must be different
287 unsigned long litem1
= (unsigned long) item1
.GetID();
288 unsigned long litem2
= (unsigned long) item2
.GetID();
291 return litem2
-litem2
;
293 return litem1
-litem2
;
296 // ---------------------------------------------------------
297 // wxDataViewIndexListModel
298 // ---------------------------------------------------------
300 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
302 // build initial index
304 for (i
= 1; i
< initial_size
+1; i
++)
305 m_hash
.Add( (void*) i
);
306 m_lastIndex
= initial_size
+ 1;
309 wxDataViewIndexListModel::~wxDataViewIndexListModel()
313 void wxDataViewIndexListModel::RowPrepended()
315 unsigned int id
= m_lastIndex
++;
316 m_hash
.Insert( (void*) id
, 0 );
317 wxDataViewItem
item( (void*) id
);
318 ItemAdded( wxDataViewItem(0), item
);
321 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
323 unsigned int id
= m_lastIndex
++;
324 m_hash
.Insert( (void*) id
, before
);
325 wxDataViewItem
item( (void*) id
);
326 ItemAdded( wxDataViewItem(0), item
);
329 void wxDataViewIndexListModel::RowAppended()
331 unsigned int id
= m_lastIndex
++;
332 m_hash
.Add( (void*) id
);
333 wxDataViewItem
item( (void*) id
);
334 ItemAdded( wxDataViewItem(0), item
);
337 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
339 wxDataViewItem
item( m_hash
[row
] );
340 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
341 m_hash
.RemoveAt( row
);
344 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
346 wxDataViewModel::ItemChanged( GetItem(row
) );
349 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
351 wxDataViewModel::ValueChanged( GetItem(row
), col
);
354 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
356 // assert for not found
357 return (unsigned int) m_hash
.Index( item
.GetID() );
360 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
362 wxASSERT( row
< m_hash
.GetCount() );
363 return wxDataViewItem( m_hash
[row
] );
366 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
367 const wxDataViewItem
& item2
,
368 unsigned int WXUNUSED(column
),
372 return GetRow(item1
) - GetRow(item2
);
374 return GetRow(item2
) - GetRow(item1
);
377 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
378 const wxDataViewItem
&item
, unsigned int col
) const
380 GetValue( variant
, GetRow(item
), col
);
383 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
384 const wxDataViewItem
&item
, unsigned int col
)
386 return SetValue( variant
, GetRow(item
), col
);
389 bool wxDataViewIndexListModel::GetAttr( const wxDataViewItem
&item
, unsigned int col
, wxDataViewItemAttr
&attr
)
391 return GetAttr( GetRow(item
), col
, attr
);
394 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
396 return wxDataViewItem(0);
399 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
401 // only the invisible root item has children
408 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
415 return m_hash
.GetCount();
418 //-----------------------------------------------------------------------------
419 // wxDataViewIconText
420 //-----------------------------------------------------------------------------
422 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
424 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
426 bool operator == (const wxDataViewIconText
&one
, const wxDataViewIconText
&two
)
428 if (one
.GetText() != two
.GetText()) return false;
429 if (one
.IsSameAs(two
)) return false;
433 // ---------------------------------------------------------
434 // wxDataViewRendererBase
435 // ---------------------------------------------------------
437 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
439 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
440 wxDataViewCellMode
WXUNUSED(mode
),
441 int WXUNUSED(align
) )
443 m_variantType
= varianttype
;
447 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
449 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
452 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
454 m_item
= item
; // remember for later
456 unsigned int col
= GetOwner()->GetModelColumn();
458 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
460 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
462 wxDataViewEditorCtrlEvtHandler
*handler
=
463 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
465 m_editorCtrl
->PushEventHandler( handler
);
467 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
468 handler
->SetFocusOnIdle();
470 m_editorCtrl
->SetFocus();
473 // Now we should send Editing Started event
474 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, GetOwner()->GetOwner()->GetId() );
475 event
.SetDataViewColumn( GetOwner() );
476 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
477 event
.SetItem( item
);
478 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
483 void wxDataViewRendererBase::CancelEditing()
485 wxPendingDelete
.Append( m_editorCtrl
);
487 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
489 // m_editorCtrl->PopEventHandler( true );
492 bool wxDataViewRendererBase::FinishEditing()
495 GetValueFromEditorCtrl( m_editorCtrl
, value
);
497 wxPendingDelete
.Append( m_editorCtrl
);
499 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
501 if (!Validate(value
))
504 unsigned int col
= GetOwner()->GetModelColumn();
505 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
506 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
508 // m_editorCtrl->PopEventHandler( true );
510 // Now we should send Editing Done event
511 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, GetOwner()->GetOwner()->GetId() );
512 event
.SetDataViewColumn( GetOwner() );
513 event
.SetModel( GetOwner()->GetOwner()->GetModel() );
514 event
.SetItem( m_item
);
515 GetOwner()->GetOwner()->GetEventHandler()->ProcessEvent( event
);
520 //-----------------------------------------------------------------------------
521 // wxDataViewEditorCtrlEvtHandler
522 //-----------------------------------------------------------------------------
524 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
525 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
526 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
527 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
530 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
531 wxControl
*editorCtrl
,
532 wxDataViewRenderer
*owner
)
535 m_editorCtrl
= editorCtrl
;
540 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
544 m_focusOnIdle
= false;
545 if (wxWindow::FindFocus() != m_editorCtrl
)
546 m_editorCtrl
->SetFocus();
552 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
554 switch ( event
.m_keyCode
)
558 m_owner
->FinishEditing();
563 m_owner
->CancelEditing();
571 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
576 m_owner
->FinishEditing();
582 // ---------------------------------------------------------
583 // wxDataViewColumnBase
584 // ---------------------------------------------------------
586 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
588 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
589 wxDataViewRenderer
*renderer
,
590 unsigned int model_column
,
592 wxAlignment
WXUNUSED(align
),
595 m_renderer
= renderer
;
596 m_model_column
= model_column
;
598 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
600 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
601 // parameters inside the native control!
604 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
605 wxDataViewRenderer
*renderer
,
606 unsigned int model_column
,
608 wxAlignment
WXUNUSED(align
),
609 int WXUNUSED(flags
) )
611 m_renderer
= renderer
;
612 m_model_column
= model_column
;
615 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
618 wxDataViewColumnBase::~wxDataViewColumnBase()
624 int wxDataViewColumnBase::GetFlags() const
629 ret
|= wxDATAVIEW_COL_SORTABLE
;
631 ret
|= wxDATAVIEW_COL_RESIZABLE
;
633 ret
|= wxDATAVIEW_COL_HIDDEN
;
638 void wxDataViewColumnBase::SetFlags(int flags
)
640 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
641 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
642 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
645 // ---------------------------------------------------------
646 // wxDataViewCtrlBase
647 // ---------------------------------------------------------
649 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
651 wxDataViewCtrlBase::wxDataViewCtrlBase()
654 m_expander_column
= 0;
658 wxDataViewCtrlBase::~wxDataViewCtrlBase()
667 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
671 m_model
->DecRef(); // discard old model, if any
674 // add our own reference to the new model:
684 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
690 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
691 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
693 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
694 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
695 model_column
, width
, align
, flags
);
701 wxDataViewCtrlBase::AppendIconTextColumn( const wxString
&label
, unsigned int model_column
,
702 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
704 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
705 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
706 model_column
, width
, align
, flags
);
712 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
713 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
716 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
717 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
718 model_column
, width
, align
, flags
);
724 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
725 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
727 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
728 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
729 model_column
, width
, align
, flags
);
735 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
736 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
738 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
739 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
740 model_column
, width
, align
, flags
);
746 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
747 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
749 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
750 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
751 model_column
, width
, align
, flags
);
757 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
758 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
760 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
761 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
762 model_column
, width
, align
, flags
);
768 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
769 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
771 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
772 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
773 model_column
, width
, align
, flags
);
779 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
780 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
782 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
783 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
784 model_column
, width
, align
, flags
);
790 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
791 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
793 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
794 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
795 model_column
, width
, align
, flags
);
801 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
802 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
804 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
805 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
806 model_column
, width
, align
, flags
);
812 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
813 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
815 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
816 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
817 model_column
, width
, align
, flags
);
823 wxDataViewCtrlBase::PrependTextColumn( const wxString
&label
, unsigned int model_column
,
824 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
826 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
827 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
828 model_column
, width
, align
, flags
);
829 PrependColumn( ret
);
834 wxDataViewCtrlBase::PrependIconTextColumn( const wxString
&label
, unsigned int model_column
,
835 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
837 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
838 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
839 model_column
, width
, align
, flags
);
840 PrependColumn( ret
);
845 wxDataViewCtrlBase::PrependToggleColumn( const wxString
&label
, unsigned int model_column
,
846 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
849 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
850 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
851 model_column
, width
, align
, flags
);
852 PrependColumn( ret
);
857 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
858 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
860 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
861 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
862 model_column
, width
, align
, flags
);
863 PrependColumn( ret
);
868 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
869 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
871 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
872 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
873 model_column
, width
, align
, flags
);
874 PrependColumn( ret
);
879 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
880 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
882 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
883 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
884 model_column
, width
, align
, flags
);
885 PrependColumn( ret
);
890 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
891 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
893 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
894 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
895 model_column
, width
, align
, flags
);
896 PrependColumn( ret
);
901 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
902 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
904 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
905 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
, (int)align
),
906 model_column
, width
, align
, flags
);
907 PrependColumn( ret
);
912 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
913 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
915 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
916 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
917 model_column
, width
, align
, flags
);
918 PrependColumn( ret
);
923 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
924 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
926 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
927 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
928 model_column
, width
, align
, flags
);
929 PrependColumn( ret
);
934 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
935 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
937 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
938 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
939 model_column
, width
, align
, flags
);
940 PrependColumn( ret
);
945 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
946 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
948 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
949 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
950 model_column
, width
, align
, flags
);
951 PrependColumn( ret
);
956 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
958 col
->SetOwner( (wxDataViewCtrl
*) this );
963 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
965 col
->SetOwner( (wxDataViewCtrl
*) this );
969 // ---------------------------------------------------------
971 // ---------------------------------------------------------
973 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
975 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
)
977 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
978 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
979 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
980 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
981 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
982 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
)
983 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
)
984 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
)
986 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
987 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
988 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
991 // -------------------------------------
992 // wxDataViewSpinRenderer
993 // -------------------------------------
995 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
996 wxDataViewCustomRenderer( "long", mode
, alignment
)
1002 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1005 return new wxSpinCtrl( parent
, wxID_ANY
, wxEmptyString
,
1006 labelRect
.GetTopLeft(), labelRect
.GetSize(), wxSP_ARROW_KEYS
, m_min
, m_max
, l
);
1009 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1011 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1012 long l
= sc
->GetValue();
1017 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1020 str
.Printf( "%d", (int) m_data
);
1021 RenderText( str
, 0, rect
, dc
, state
);
1025 wxSize
wxDataViewSpinRenderer::GetSize() const
1027 return wxSize(80,16);
1030 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1032 m_data
= value
.GetLong();
1036 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1042 //-----------------------------------------------------------------------------
1043 // wxDataViewTreeStore
1044 //-----------------------------------------------------------------------------
1046 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1047 wxDataViewTreeStoreNode
*parent
,
1048 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1056 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1062 #include "wx/listimpl.cpp"
1063 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
)
1065 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1066 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1067 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1068 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1070 m_iconExpanded
= expanded
;
1071 m_children
.DeleteContents(true);
1074 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1078 //-----------------------------------------------------------------------------
1080 wxDataViewTreeStore::wxDataViewTreeStore()
1082 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1085 wxDataViewTreeStore::~wxDataViewTreeStore()
1090 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1091 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1093 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1094 if (!parent_node
) return wxDataViewItem(0);
1096 wxDataViewTreeStoreNode
*node
=
1097 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1098 parent_node
->GetChildren().Append( node
);
1101 ItemAdded( parent
, node
->GetItem() );
1103 return node
->GetItem();
1106 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1107 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1109 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1110 if (!parent_node
) return wxDataViewItem(0);
1112 wxDataViewTreeStoreNode
*node
=
1113 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1114 parent_node
->GetChildren().Insert( node
);
1117 ItemAdded( parent
, node
->GetItem() );
1119 return node
->GetItem();
1123 wxDataViewTreeStore::InsertItem(const wxDataViewItem
& WXUNUSED(parent
),
1124 const wxDataViewItem
& WXUNUSED(previous
),
1125 const wxString
& WXUNUSED(text
),
1126 const wxIcon
& WXUNUSED(icon
),
1127 wxClientData
* WXUNUSED(data
))
1129 return wxDataViewItem(0);
1132 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1133 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1134 wxClientData
*data
)
1136 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1137 if (!parent_node
) return wxDataViewItem(0);
1139 wxDataViewTreeStoreContainerNode
*node
=
1140 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1141 parent_node
->GetChildren().Insert( node
);
1144 ItemAdded( parent
, node
->GetItem() );
1146 return node
->GetItem();
1150 wxDataViewTreeStore::AppendContainer(const wxDataViewItem
& parent
,
1151 const wxString
&text
,
1153 const wxIcon
& expanded
,
1154 wxClientData
* data
)
1156 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1157 if (!parent_node
) return wxDataViewItem(0);
1159 wxDataViewTreeStoreContainerNode
*node
=
1160 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1161 parent_node
->GetChildren().Append( node
);
1164 ItemAdded( parent
, node
->GetItem() );
1166 return node
->GetItem();
1170 wxDataViewTreeStore::InsertContainer(const wxDataViewItem
& WXUNUSED(parent
),
1171 const wxDataViewItem
& WXUNUSED(previous
),
1172 const wxString
& WXUNUSED(text
),
1173 const wxIcon
& WXUNUSED(icon
),
1174 const wxIcon
& WXUNUSED(expanded
),
1175 wxClientData
* WXUNUSED(data
))
1177 return wxDataViewItem(0);
1180 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1182 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1183 if (!parent_node
) return wxDataViewItem(0);
1185 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1187 return node
->GetData();
1189 return wxDataViewItem(0);
1192 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1194 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1195 if (!node
) return -1;
1197 if (!node
->IsContainer())
1200 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1201 return (int) container_node
->GetChildren().GetCount();
1204 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1206 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1209 node
->SetText( text
);
1212 ValueChanged( item
, 0 );
1215 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1217 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1218 if (!node
) return wxEmptyString
;
1220 return node
->GetText();
1223 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1225 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1228 node
->SetIcon( icon
);
1231 ValueChanged( item
, 0 );
1234 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1236 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1237 if (!node
) return wxNullIcon
;
1239 return node
->GetIcon();
1242 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1244 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1247 node
->SetExpandedIcon( icon
);
1250 ValueChanged( item
, 0 );
1253 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1255 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1256 if (!node
) return wxNullIcon
;
1258 return node
->GetExpandedIcon();
1261 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1263 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1266 node
->SetData( data
);
1268 // notify control? only sensible when sorting on client data
1269 // ValueChanged( item, 0 );
1272 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1274 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1275 if (!node
) return NULL
;
1277 return node
->GetData();
1280 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1282 if (!item
.IsOk()) return;
1284 wxDataViewItem parent_item
= GetParent( item
);
1286 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1287 if (!parent_node
) return;
1289 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1292 parent_node
->GetChildren().DeleteObject( node
);
1295 ItemDeleted( parent_item
, item
);
1298 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1300 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1303 wxDataViewItemArray array
;
1304 wxDataViewTreeStoreNodeList::iterator iter
;
1305 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1307 wxDataViewTreeStoreNode
* child
= *iter
;
1308 array
.Add( child
->GetItem() );
1311 node
->GetChildren().clear();
1314 ItemsDeleted( item
, array
);
1317 void wxDataViewTreeStore::DeleteAllItems()
1323 wxDataViewTreeStore::GetValue(wxVariant
&variant
,
1324 const wxDataViewItem
&item
,
1325 unsigned int WXUNUSED(col
)) const
1327 // if (col != 0) return;
1329 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1332 wxDataViewIconText
data( node
->GetText(), node
->GetIcon() );
1338 wxDataViewTreeStore::SetValue(const wxVariant
& variant
,
1339 const wxDataViewItem
& item
,
1340 unsigned int WXUNUSED(col
))
1342 // if (col != 0) return false;
1344 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1345 if (!node
) return false;
1347 wxDataViewIconText data
;
1351 node
->SetText( data
.GetText() );
1352 node
->SetIcon( data
.GetIcon() );
1357 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1359 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1360 if (!node
) return wxDataViewItem(0);
1362 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1363 if (!parent
) return wxDataViewItem(0);
1365 if (parent
== m_root
)
1366 return wxDataViewItem(0);
1368 return parent
->GetItem();
1371 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
&item
) const
1373 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1374 if (!node
) return false;
1376 return node
->IsContainer();
1379 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1381 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1382 if (!node
) return 0;
1384 wxDataViewTreeStoreNodeList::iterator iter
;
1385 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1387 wxDataViewTreeStoreNode
* child
= *iter
;
1388 children
.Add( child
->GetItem() );
1391 return node
->GetChildren().GetCount();
1394 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
1395 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) )
1397 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
1398 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
1400 if (!node1
|| !node2
)
1403 wxDataViewTreeStoreContainerNode
* parent1
=
1404 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
1405 wxDataViewTreeStoreContainerNode
* parent2
=
1406 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
1408 if (parent1
!= parent2
)
1410 wxLogError( wxT("Comparing items with different parent.") );
1414 if (node1
->IsContainer() && !!node2
->IsContainer())
1417 if (node2
->IsContainer() && !!node1
->IsContainer())
1420 return parent1
->GetChildren().IndexOf( node1
) - parent1
->GetChildren().IndexOf( node2
);
1423 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
1428 return (wxDataViewTreeStoreNode
*) item
.GetID();
1431 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
1434 return (wxDataViewTreeStoreContainerNode
*) m_root
;
1436 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
1438 if (!node
->IsContainer())
1441 return (wxDataViewTreeStoreContainerNode
*) node
;
1444 #endif // wxUSE_DATAVIEWCTRL