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"
24 #include "wx/settings.h"
29 #include "wx/spinctrl.h"
30 #include "wx/choice.h"
31 #include "wx/imaglist.h"
33 const char wxDataViewCtrlNameStr
[] = "dataviewCtrl";
35 // ---------------------------------------------------------
36 // wxDataViewModelNotifier
37 // ---------------------------------------------------------
39 #include "wx/listimpl.cpp"
40 WX_DEFINE_LIST(wxDataViewModelNotifiers
)
42 bool wxDataViewModelNotifier::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
44 size_t count
= items
.GetCount();
46 for (i
= 0; i
< count
; i
++)
47 if (!ItemAdded( parent
, items
[i
] )) return false;
52 bool wxDataViewModelNotifier::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
54 size_t count
= items
.GetCount();
56 for (i
= 0; i
< count
; i
++)
57 if (!ItemDeleted( parent
, items
[i
] )) return false;
62 bool wxDataViewModelNotifier::ItemsChanged( const wxDataViewItemArray
&items
)
64 size_t count
= items
.GetCount();
66 for (i
= 0; i
< count
; i
++)
67 if (!ItemChanged( items
[i
] )) return false;
72 // ---------------------------------------------------------
74 // ---------------------------------------------------------
76 wxDataViewModel::wxDataViewModel()
78 m_notifiers
.DeleteContents( true );
81 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
85 wxDataViewModelNotifiers::iterator iter
;
86 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
88 wxDataViewModelNotifier
* notifier
= *iter
;
89 if (!notifier
->ItemAdded( parent
, item
))
96 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
100 wxDataViewModelNotifiers::iterator iter
;
101 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
103 wxDataViewModelNotifier
* notifier
= *iter
;
104 if (!notifier
->ItemDeleted( parent
, item
))
111 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
115 wxDataViewModelNotifiers::iterator iter
;
116 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
118 wxDataViewModelNotifier
* notifier
= *iter
;
119 if (!notifier
->ItemChanged( item
))
126 bool wxDataViewModel::ItemsAdded( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
130 wxDataViewModelNotifiers::iterator iter
;
131 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
133 wxDataViewModelNotifier
* notifier
= *iter
;
134 if (!notifier
->ItemsAdded( parent
, items
))
141 bool wxDataViewModel::ItemsDeleted( const wxDataViewItem
&parent
, const wxDataViewItemArray
&items
)
145 wxDataViewModelNotifiers::iterator iter
;
146 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
148 wxDataViewModelNotifier
* notifier
= *iter
;
149 if (!notifier
->ItemsDeleted( parent
, items
))
156 bool wxDataViewModel::ItemsChanged( const wxDataViewItemArray
&items
)
160 wxDataViewModelNotifiers::iterator iter
;
161 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
163 wxDataViewModelNotifier
* notifier
= *iter
;
164 if (!notifier
->ItemsChanged( items
))
171 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
175 wxDataViewModelNotifiers::iterator iter
;
176 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
178 wxDataViewModelNotifier
* notifier
= *iter
;
179 if (!notifier
->ValueChanged( item
, col
))
186 bool wxDataViewModel::Cleared()
190 wxDataViewModelNotifiers::iterator iter
;
191 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
193 wxDataViewModelNotifier
* notifier
= *iter
;
194 if (!notifier
->Cleared())
201 void wxDataViewModel::Resort()
203 wxDataViewModelNotifiers::iterator iter
;
204 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
206 wxDataViewModelNotifier
* notifier
= *iter
;
211 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
213 m_notifiers
.push_back( notifier
);
214 notifier
->SetOwner( this );
217 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
219 m_notifiers
.DeleteObject( notifier
);
222 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
223 unsigned int column
, bool ascending
) const
225 // sort branches before leaves
226 bool item1_is_container
= IsContainer(item1
);
227 bool item2_is_container
= IsContainer(item2
);
229 if (item1_is_container
&& !item2_is_container
)
231 if (item2_is_container
&& !item1_is_container
)
234 wxVariant value1
,value2
;
235 GetValue( value1
, item1
, column
);
236 GetValue( value2
, item2
, column
);
240 wxVariant temp
= value1
;
245 if (value1
.GetType() == wxT("string"))
247 wxString str1
= value1
.GetString();
248 wxString str2
= value2
.GetString();
249 int res
= str1
.Cmp( str2
);
253 else if (value1
.GetType() == wxT("long"))
255 long l1
= value1
.GetLong();
256 long l2
= value2
.GetLong();
261 else if (value1
.GetType() == wxT("double"))
263 double d1
= value1
.GetDouble();
264 double d2
= value2
.GetDouble();
270 else if (value1
.GetType() == wxT("datetime"))
272 wxDateTime dt1
= value1
.GetDateTime();
273 wxDateTime dt2
= value2
.GetDateTime();
274 if (dt1
.IsEarlierThan(dt2
))
276 if (dt2
.IsEarlierThan(dt1
))
280 // items must be different
281 wxUIntPtr id1
= wxPtrToUInt(item1
.GetID()),
282 id2
= wxPtrToUInt(item2
.GetID());
284 return ascending
? id1
- id2
: id2
- id1
;
287 // ---------------------------------------------------------
288 // wxDataViewIndexListModel
289 // ---------------------------------------------------------
291 static int my_sort( int *v1
, int *v2
)
297 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
299 // IDs are ordered until an item gets deleted or inserted
302 // build initial index
304 for (i
= 1; i
< initial_size
+1; i
++)
305 m_hash
.Add( wxUIntToPtr(i
) );
306 m_nextFreeID
= initial_size
+ 1;
309 void wxDataViewIndexListModel::Reset( unsigned int new_size
)
313 // IDs are ordered until an item gets deleted or inserted
316 // build initial index
318 for (i
= 1; i
< new_size
+1; i
++)
319 m_hash
.Add( wxUIntToPtr(i
) );
321 m_nextFreeID
= new_size
+ 1;
323 wxDataViewModel::Cleared();
326 void wxDataViewIndexListModel::RowPrepended()
330 unsigned int id
= m_nextFreeID
;
333 m_hash
.Insert( wxUIntToPtr(id
), 0 );
334 wxDataViewItem
item( wxUIntToPtr(id
) );
335 ItemAdded( wxDataViewItem(0), item
);
339 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
343 unsigned int id
= m_nextFreeID
;
346 m_hash
.Insert( wxUIntToPtr(id
), before
);
347 wxDataViewItem
item( wxUIntToPtr(id
) );
348 ItemAdded( wxDataViewItem(0), item
);
351 void wxDataViewIndexListModel::RowAppended()
353 unsigned int id
= m_nextFreeID
;
356 m_hash
.Add( wxUIntToPtr(id
) );
357 wxDataViewItem
item( wxUIntToPtr(id
) );
358 ItemAdded( wxDataViewItem(0), item
);
361 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
365 wxDataViewItem
item( m_hash
[row
] );
366 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
367 m_hash
.RemoveAt( row
);
370 void wxDataViewIndexListModel::RowsDeleted( const wxArrayInt
&rows
)
372 wxArrayInt sorted
= rows
;
373 sorted
.Sort( my_sort
);
377 wxDataViewItemArray array
;
379 for (i
= 0; i
< rows
.GetCount(); i
++)
381 wxDataViewItem
item( m_hash
[rows
[i
]] );
384 wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array
);
386 for (i
= 0; i
< sorted
.GetCount(); i
++)
387 m_hash
.RemoveAt( sorted
[i
] );
390 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
392 wxDataViewModel::ItemChanged( GetItem(row
) );
395 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
397 wxDataViewModel::ValueChanged( GetItem(row
), col
);
400 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
403 return wxPtrToUInt(item
.GetID())-1;
405 // assert for not found
406 return (unsigned int) m_hash
.Index( item
.GetID() );
409 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
411 wxASSERT( row
< m_hash
.GetCount() );
412 return wxDataViewItem( m_hash
[row
] );
415 bool wxDataViewIndexListModel::HasDefaultCompare() const
420 int wxDataViewIndexListModel::Compare(const wxDataViewItem
& item1
,
421 const wxDataViewItem
& item2
,
422 unsigned int WXUNUSED(column
),
423 bool ascending
) const
427 unsigned int pos1
= wxPtrToUInt(item1
.GetID()); // -1 not needed here
428 unsigned int pos2
= wxPtrToUInt(item2
.GetID()); // -1 not needed here
437 return GetRow(item1
) - GetRow(item2
);
439 return GetRow(item2
) - GetRow(item1
);
442 unsigned int wxDataViewIndexListModel::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
449 return m_hash
.GetCount();
452 // ---------------------------------------------------------
453 // wxDataViewVirtualListModel
454 // ---------------------------------------------------------
458 wxDataViewVirtualListModel::wxDataViewVirtualListModel( unsigned int initial_size
)
460 m_size
= initial_size
;
463 void wxDataViewVirtualListModel::Reset( unsigned int new_size
)
467 wxDataViewModel::Cleared();
470 void wxDataViewVirtualListModel::RowPrepended()
473 wxDataViewItem
item( wxUIntToPtr(1) );
474 ItemAdded( wxDataViewItem(0), item
);
477 void wxDataViewVirtualListModel::RowInserted( unsigned int before
)
480 wxDataViewItem
item( wxUIntToPtr(before
+1) );
481 ItemAdded( wxDataViewItem(0), item
);
484 void wxDataViewVirtualListModel::RowAppended()
487 wxDataViewItem
item( wxUIntToPtr(m_size
) );
488 ItemAdded( wxDataViewItem(0), item
);
491 void wxDataViewVirtualListModel::RowDeleted( unsigned int row
)
494 wxDataViewItem
item( wxUIntToPtr(row
+1) );
495 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
498 void wxDataViewVirtualListModel::RowsDeleted( const wxArrayInt
&rows
)
500 m_size
-= rows
.GetCount();
502 wxArrayInt sorted
= rows
;
503 sorted
.Sort( my_sort
);
505 wxDataViewItemArray array
;
507 for (i
= 0; i
< sorted
.GetCount(); i
++)
509 wxDataViewItem
item( wxUIntToPtr(sorted
[i
]+1) );
512 wxDataViewModel::ItemsDeleted( wxDataViewItem(0), array
);
515 void wxDataViewVirtualListModel::RowChanged( unsigned int row
)
517 wxDataViewModel::ItemChanged( GetItem(row
) );
520 void wxDataViewVirtualListModel::RowValueChanged( unsigned int row
, unsigned int col
)
522 wxDataViewModel::ValueChanged( GetItem(row
), col
);
525 unsigned int wxDataViewVirtualListModel::GetRow( const wxDataViewItem
&item
) const
527 return wxPtrToUInt( item
.GetID() ) -1;
530 wxDataViewItem
wxDataViewVirtualListModel::GetItem( unsigned int row
) const
532 return wxDataViewItem( wxUIntToPtr(row
+1) );
535 bool wxDataViewVirtualListModel::HasDefaultCompare() const
540 int wxDataViewVirtualListModel::Compare(const wxDataViewItem
& item1
,
541 const wxDataViewItem
& item2
,
542 unsigned int WXUNUSED(column
),
543 bool ascending
) const
545 unsigned int pos1
= wxPtrToUInt(item1
.GetID()); // -1 not needed here
546 unsigned int pos2
= wxPtrToUInt(item2
.GetID()); // -1 not needed here
554 unsigned int wxDataViewVirtualListModel::GetChildren( const wxDataViewItem
&WXUNUSED(item
), wxDataViewItemArray
&WXUNUSED(children
) ) const
556 return 0; // should we report an error ?
561 //-----------------------------------------------------------------------------
562 // wxDataViewIconText
563 //-----------------------------------------------------------------------------
565 IMPLEMENT_DYNAMIC_CLASS(wxDataViewIconText
,wxObject
)
567 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxDataViewIconText
, WXDLLIMPEXP_ADV
)
569 // ---------------------------------------------------------
570 // wxDataViewRendererBase
571 // ---------------------------------------------------------
573 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
575 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
576 wxDataViewCellMode
WXUNUSED(mode
),
577 int WXUNUSED(align
) )
579 m_variantType
= varianttype
;
583 wxDataViewRendererBase::~wxDataViewRendererBase()
587 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
589 return const_cast<wxDataViewRendererBase
*>(this)->GetOwner()->GetOwner();
592 class wxKillRef
: public wxWindowRef
595 wxKillRef( wxWindow
*win
) : wxWindowRef( win
) { }
596 virtual void OnObjectDestroy()
598 get()->PopEventHandler( true );
604 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
606 wxDataViewCtrl
* dv_ctrl
= GetOwner()->GetOwner();
608 // Before doing anything we send an event asking if editing of this item is really wanted.
609 wxDataViewEvent
start_event( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, dv_ctrl
->GetId() );
610 start_event
.SetDataViewColumn( GetOwner() );
611 start_event
.SetModel( dv_ctrl
->GetModel() );
612 start_event
.SetItem( item
);
613 start_event
.SetEventObject( dv_ctrl
);
614 dv_ctrl
->GetEventHandler()->ProcessEvent( start_event
);
615 if( !start_event
.IsAllowed() )
618 m_item
= item
; // remember for later
620 unsigned int col
= GetOwner()->GetModelColumn();
622 dv_ctrl
->GetModel()->GetValue( value
, item
, col
);
624 m_editorCtrl
= CreateEditorCtrl( dv_ctrl
->GetMainWindow(), labelRect
, value
);
626 // there might be no editor control for the given item
630 (void) new wxKillRef( m_editorCtrl
.get() );
632 wxDataViewEditorCtrlEvtHandler
*handler
=
633 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
635 m_editorCtrl
->PushEventHandler( handler
);
637 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
638 handler
->SetFocusOnIdle();
640 m_editorCtrl
->SetFocus();
643 // Now we should send Editing Started event
644 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, dv_ctrl
->GetId() );
645 event
.SetDataViewColumn( GetOwner() );
646 event
.SetModel( dv_ctrl
->GetModel() );
647 event
.SetItem( item
);
648 event
.SetEventObject( dv_ctrl
);
649 dv_ctrl
->GetEventHandler()->ProcessEvent( event
);
654 void wxDataViewRendererBase::CancelEditing()
659 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
661 m_editorCtrl
->Hide();
662 wxPendingDelete
.Append( m_editorCtrl
);
665 bool wxDataViewRendererBase::FinishEditing()
671 GetValueFromEditorCtrl( m_editorCtrl
, value
);
673 wxDataViewCtrl
* dv_ctrl
= GetOwner()->GetOwner();
675 dv_ctrl
->GetMainWindow()->SetFocus();
677 m_editorCtrl
->Hide();
678 wxPendingDelete
.Append( m_editorCtrl
);
680 if (!Validate(value
))
683 unsigned int col
= GetOwner()->GetModelColumn();
684 dv_ctrl
->GetModel()->ChangeValue(value
, m_item
, col
);
686 // Now we should send Editing Done event
687 wxDataViewEvent
event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, dv_ctrl
->GetId() );
688 event
.SetDataViewColumn( GetOwner() );
689 event
.SetModel( dv_ctrl
->GetModel() );
690 event
.SetItem( m_item
);
691 event
.SetEventObject( dv_ctrl
);
692 dv_ctrl
->GetEventHandler()->ProcessEvent( event
);
697 // ----------------------------------------------------------------------------
698 // wxDataViewCustomRendererBase
699 // ----------------------------------------------------------------------------
702 wxDataViewCustomRendererBase::WXCallRender(wxRect rectCell
, wxDC
*dc
, int state
)
704 wxCHECK_RET( dc
, "no DC to draw on in custom renderer?" );
706 // adjust the rectangle ourselves to account for the alignment
707 wxRect rectItem
= rectCell
;
708 const int align
= GetAlignment();
709 if ( align
!= wxDVR_DEFAULT_ALIGNMENT
)
711 const wxSize size
= GetSize();
713 // take alignment into account only if there is enough space, otherwise
714 // show as much contents as possible
716 // notice that many existing renderers (e.g. wxDataViewSpinRenderer)
717 // return hard-coded size which can be more than they need and if we
718 // trusted their GetSize() we'd draw the text out of cell bounds
721 if ( size
.x
>= 0 && size
.x
< rectCell
.width
)
723 if ( align
& wxALIGN_CENTER_HORIZONTAL
)
724 rectItem
.x
+= (rectCell
.width
- size
.x
)/2;
725 else if ( align
& wxALIGN_RIGHT
)
726 rectItem
.x
+= rectCell
.width
- size
.x
;
727 // else: wxALIGN_LEFT is the default
729 rectItem
.width
= size
.x
;
732 if ( size
.y
>= 0 && size
.y
< rectCell
.height
)
734 if ( align
& wxALIGN_CENTER_VERTICAL
)
735 rectItem
.y
+= (rectCell
.height
- size
.y
)/2;
736 else if ( align
& wxALIGN_BOTTOM
)
737 rectItem
.y
+= rectCell
.height
- size
.y
;
738 // else: wxALIGN_TOP is the default
740 rectItem
.height
= size
.y
;
745 // set up the DC attributes
747 // override custom foreground with the standard one for the selected items
748 // because we currently don't allow changing the selection background and
749 // custom colours may be unreadable on it
751 if ( state
& wxDATAVIEW_CELL_SELECTED
)
752 col
= wxSystemSettings::GetColour(wxSYS_COLOUR_HIGHLIGHTTEXT
);
753 else if ( m_attr
.HasColour() )
754 col
= m_attr
.GetColour();
755 else // use default foreground
756 col
= GetOwner()->GetOwner()->GetForegroundColour();
758 wxDCTextColourChanger
changeFg(*dc
, col
);
760 wxDCFontChanger
changeFont(*dc
);
761 if ( m_attr
.HasFont() )
763 wxFont
font(dc
->GetFont());
764 if ( m_attr
.GetBold() )
766 if ( m_attr
.GetItalic() )
769 changeFont
.Set(font
);
772 Render(rectItem
, dc
, state
);
776 wxDataViewCustomRendererBase::RenderText(const wxString
& text
,
782 wxRect rectText
= rect
;
783 rectText
.x
+= xoffset
;
784 rectText
.width
-= xoffset
;
786 // check if we want to ellipsize the text if it doesn't fit
787 wxString ellipsizedText
;
788 if ( GetEllipsizeMode() != wxELLIPSIZE_NONE
)
790 ellipsizedText
= wxControl::Ellipsize
796 wxELLIPSIZE_FLAGS_NONE
800 // get the alignment to use
801 int align
= GetAlignment();
802 if ( align
== wxDVR_DEFAULT_ALIGNMENT
)
804 // if we don't have an explicit alignment ourselves, use that of the
805 // column in horizontal direction and default vertical alignment
806 align
= GetOwner()->GetAlignment() | wxALIGN_CENTRE_VERTICAL
;
809 dc
->DrawLabel(ellipsizedText
.empty() ? text
: ellipsizedText
,
813 //-----------------------------------------------------------------------------
814 // wxDataViewEditorCtrlEvtHandler
815 //-----------------------------------------------------------------------------
817 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
818 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
819 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
820 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
821 EVT_TEXT_ENTER (-1, wxDataViewEditorCtrlEvtHandler::OnTextEnter
)
824 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
825 wxControl
*editorCtrl
,
826 wxDataViewRenderer
*owner
)
829 m_editorCtrl
= editorCtrl
;
834 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
838 m_focusOnIdle
= false;
839 if (wxWindow::FindFocus() != m_editorCtrl
)
840 m_editorCtrl
->SetFocus();
846 void wxDataViewEditorCtrlEvtHandler::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
849 m_owner
->FinishEditing();
852 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
854 switch ( event
.m_keyCode
)
858 m_owner
->FinishEditing();
864 m_owner
->CancelEditing();
872 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
877 m_owner
->FinishEditing();
883 // ---------------------------------------------------------
884 // wxDataViewColumnBase
885 // ---------------------------------------------------------
887 void wxDataViewColumnBase::Init(wxDataViewRenderer
*renderer
,
888 unsigned int model_column
)
890 m_renderer
= renderer
;
891 m_model_column
= model_column
;
893 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
896 wxDataViewColumnBase::~wxDataViewColumnBase()
901 // ---------------------------------------------------------
902 // wxDataViewCtrlBase
903 // ---------------------------------------------------------
905 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
907 wxDataViewCtrlBase::wxDataViewCtrlBase()
910 m_expander_column
= 0;
914 wxDataViewCtrlBase::~wxDataViewCtrlBase()
923 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
927 m_model
->DecRef(); // discard old model, if any
930 // add our own reference to the new model:
940 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
945 const wxDataViewModel
* wxDataViewCtrlBase::GetModel() const
950 void wxDataViewCtrlBase::ExpandAncestors( const wxDataViewItem
& item
)
952 if (!m_model
) return;
954 if (!item
.IsOk()) return;
956 wxVector
<wxDataViewItem
> parentChain
;
958 // at first we get all the parents of the selected item
959 wxDataViewItem parent
= m_model
->GetParent(item
);
960 while (parent
.IsOk())
962 parentChain
.push_back(parent
);
963 parent
= m_model
->GetParent(parent
);
966 // then we expand the parents, starting at the root
967 while (!parentChain
.empty())
969 Expand(parentChain
.back());
970 parentChain
.pop_back();
975 wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
976 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
978 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
979 new wxDataViewTextRenderer( wxT("string"), mode
),
980 model_column
, width
, align
, flags
);
986 wxDataViewCtrlBase::AppendIconTextColumn( const wxString
&label
, unsigned int model_column
,
987 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
989 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
990 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
),
991 model_column
, width
, align
, flags
);
997 wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
998 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1000 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1001 new wxDataViewToggleRenderer( wxT("bool"), mode
),
1002 model_column
, width
, align
, flags
);
1003 AppendColumn( ret
);
1008 wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
1009 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1011 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1012 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
),
1013 model_column
, width
, align
, flags
);
1014 AppendColumn( ret
);
1019 wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
1020 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1022 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1023 new wxDataViewDateRenderer( wxT("datetime"), mode
),
1024 model_column
, width
, align
, flags
);
1025 AppendColumn( ret
);
1030 wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
1031 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1033 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1034 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
),
1035 model_column
, width
, align
, flags
);
1036 AppendColumn( ret
);
1041 wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
1042 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1044 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1045 new wxDataViewTextRenderer( wxT("string"), mode
),
1046 model_column
, width
, align
, flags
);
1047 AppendColumn( ret
);
1052 wxDataViewCtrlBase::AppendIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
1053 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1055 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1056 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
),
1057 model_column
, width
, align
, flags
);
1058 AppendColumn( ret
);
1063 wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
1064 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1066 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1067 new wxDataViewToggleRenderer( wxT("bool"), mode
),
1068 model_column
, width
, align
, flags
);
1069 AppendColumn( ret
);
1074 wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
1075 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1077 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1078 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
),
1079 model_column
, width
, align
, flags
);
1080 AppendColumn( ret
);
1085 wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
1086 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1088 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1089 new wxDataViewDateRenderer( wxT("datetime"), mode
),
1090 model_column
, width
, align
, flags
);
1091 AppendColumn( ret
);
1096 wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
1097 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1099 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1100 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
),
1101 model_column
, width
, align
, flags
);
1102 AppendColumn( ret
);
1107 wxDataViewCtrlBase::PrependTextColumn( const wxString
&label
, unsigned int model_column
,
1108 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1110 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1111 new wxDataViewTextRenderer( wxT("string"), mode
),
1112 model_column
, width
, align
, flags
);
1113 PrependColumn( ret
);
1118 wxDataViewCtrlBase::PrependIconTextColumn( const wxString
&label
, unsigned int model_column
,
1119 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1121 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1122 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
),
1123 model_column
, width
, align
, flags
);
1124 PrependColumn( ret
);
1129 wxDataViewCtrlBase::PrependToggleColumn( const wxString
&label
, unsigned int model_column
,
1130 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1133 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1134 new wxDataViewToggleRenderer( wxT("bool"), mode
),
1135 model_column
, width
, align
, flags
);
1136 PrependColumn( ret
);
1141 wxDataViewCtrlBase::PrependProgressColumn( const wxString
&label
, unsigned int model_column
,
1142 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1144 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1145 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
),
1146 model_column
, width
, align
, flags
);
1147 PrependColumn( ret
);
1152 wxDataViewCtrlBase::PrependDateColumn( const wxString
&label
, unsigned int model_column
,
1153 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1155 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1156 new wxDataViewDateRenderer( wxT("datetime"), mode
),
1157 model_column
, width
, align
, flags
);
1158 PrependColumn( ret
);
1163 wxDataViewCtrlBase::PrependBitmapColumn( const wxString
&label
, unsigned int model_column
,
1164 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1166 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1167 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
),
1168 model_column
, width
, align
, flags
);
1169 PrependColumn( ret
);
1174 wxDataViewCtrlBase::PrependTextColumn( const wxBitmap
&label
, unsigned int model_column
,
1175 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1177 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1178 new wxDataViewTextRenderer( wxT("string"), mode
),
1179 model_column
, width
, align
, flags
);
1180 PrependColumn( ret
);
1185 wxDataViewCtrlBase::PrependIconTextColumn( const wxBitmap
&label
, unsigned int model_column
,
1186 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1188 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1189 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
),
1190 model_column
, width
, align
, flags
);
1191 PrependColumn( ret
);
1196 wxDataViewCtrlBase::PrependToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
1197 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1199 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1200 new wxDataViewToggleRenderer( wxT("bool"), mode
),
1201 model_column
, width
, align
, flags
);
1202 PrependColumn( ret
);
1207 wxDataViewCtrlBase::PrependProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
1208 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1210 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1211 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
),
1212 model_column
, width
, align
, flags
);
1213 PrependColumn( ret
);
1218 wxDataViewCtrlBase::PrependDateColumn( const wxBitmap
&label
, unsigned int model_column
,
1219 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1221 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1222 new wxDataViewDateRenderer( wxT("datetime"), mode
),
1223 model_column
, width
, align
, flags
);
1224 PrependColumn( ret
);
1229 wxDataViewCtrlBase::PrependBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
1230 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1232 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1233 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
),
1234 model_column
, width
, align
, flags
);
1235 PrependColumn( ret
);
1240 wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
1242 col
->SetOwner( (wxDataViewCtrl
*) this );
1247 wxDataViewCtrlBase::PrependColumn( wxDataViewColumn
*col
)
1249 col
->SetOwner( (wxDataViewCtrl
*) this );
1254 wxDataViewCtrlBase::InsertColumn( unsigned int WXUNUSED(pos
), wxDataViewColumn
*col
)
1256 col
->SetOwner( (wxDataViewCtrl
*) this );
1260 // ---------------------------------------------------------
1262 // ---------------------------------------------------------
1264 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
1266 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED
, wxDataViewEvent
);
1268 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
, wxDataViewEvent
);
1269 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
, wxDataViewEvent
);
1270 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
, wxDataViewEvent
);
1271 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
, wxDataViewEvent
);
1272 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
, wxDataViewEvent
);
1273 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED
, wxDataViewEvent
);
1274 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_START_EDITING
, wxDataViewEvent
);
1275 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE
, wxDataViewEvent
);
1276 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_VALUE_CHANGED
, wxDataViewEvent
);
1278 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU
, wxDataViewEvent
);
1280 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
, wxDataViewEvent
);
1281 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
, wxDataViewEvent
);
1282 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
, wxDataViewEvent
);
1283 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_COLUMN_REORDERED
, wxDataViewEvent
);
1285 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_CACHE_HINT
, wxDataViewEvent
);
1287 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_BEGIN_DRAG
, wxDataViewEvent
);
1288 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP_POSSIBLE
, wxDataViewEvent
);
1289 wxDEFINE_EVENT( wxEVT_COMMAND_DATAVIEW_ITEM_DROP
, wxDataViewEvent
);
1293 // -------------------------------------
1294 // wxDataViewSpinRenderer
1295 // -------------------------------------
1297 wxDataViewSpinRenderer::wxDataViewSpinRenderer( int min
, int max
, wxDataViewCellMode mode
, int alignment
) :
1298 wxDataViewCustomRenderer(wxT("long"), mode
, alignment
)
1304 wxControl
* wxDataViewSpinRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1307 wxSize size
= labelRect
.GetSize();
1309 size
= wxSize( wxMax(70,labelRect
.width
), -1 );
1312 str
.Printf( wxT("%d"), (int) l
);
1313 wxSpinCtrl
*sc
= new wxSpinCtrl( parent
, wxID_ANY
, str
,
1314 labelRect
.GetTopLeft(), size
, wxSP_ARROW_KEYS
|wxTE_PROCESS_ENTER
, m_min
, m_max
, l
);
1316 size
= sc
->GetSize();
1317 wxPoint pt
= sc
->GetPosition();
1318 sc
->SetSize( pt
.x
- 4, pt
.y
- 4, size
.x
, size
.y
);
1324 bool wxDataViewSpinRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1326 wxSpinCtrl
*sc
= (wxSpinCtrl
*) editor
;
1327 long l
= sc
->GetValue();
1332 bool wxDataViewSpinRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1335 str
.Printf(wxT("%d"), (int) m_data
);
1336 RenderText( str
, 0, rect
, dc
, state
);
1340 wxSize
wxDataViewSpinRenderer::GetSize() const
1342 return wxSize(80,16);
1345 bool wxDataViewSpinRenderer::SetValue( const wxVariant
&value
)
1347 m_data
= value
.GetLong();
1351 bool wxDataViewSpinRenderer::GetValue( wxVariant
&value
) const
1357 // -------------------------------------
1358 // wxDataViewChoiceRenderer
1359 // -------------------------------------
1361 #if defined(wxHAS_GENERIC_DATAVIEWCTRL) || defined(__WXOSX_CARBON__)
1363 wxDataViewChoiceRenderer::wxDataViewChoiceRenderer( const wxArrayString
& choices
, wxDataViewCellMode mode
, int alignment
) :
1364 wxDataViewCustomRenderer(wxT("string"), mode
, alignment
)
1366 m_choices
= choices
;
1369 wxControl
* wxDataViewChoiceRenderer::CreateEditorCtrl( wxWindow
*parent
, wxRect labelRect
, const wxVariant
&value
)
1372 wxSize size
= labelRect
.GetSize();
1374 size
= wxSize( wxMax(70,labelRect
.width
), -1 );
1376 wxChoice
*c
= new wxChoice( parent
, wxID_ANY
, labelRect
.GetTopLeft(), size
, m_choices
);
1377 c
->SetStringSelection( value
.GetString() );
1382 bool wxDataViewChoiceRenderer::GetValueFromEditorCtrl( wxControl
* editor
, wxVariant
&value
)
1384 wxChoice
*c
= (wxChoice
*) editor
;
1385 wxString s
= c
->GetStringSelection();
1390 bool wxDataViewChoiceRenderer::Render( wxRect rect
, wxDC
*dc
, int state
)
1392 RenderText( m_data
, 0, rect
, dc
, state
);
1396 wxSize
wxDataViewChoiceRenderer::GetSize() const
1398 return wxSize(80,16);
1401 bool wxDataViewChoiceRenderer::SetValue( const wxVariant
&value
)
1403 m_data
= value
.GetString();
1407 bool wxDataViewChoiceRenderer::GetValue( wxVariant
&value
) const
1415 //-----------------------------------------------------------------------------
1416 // wxDataViewListStore
1417 //-----------------------------------------------------------------------------
1419 wxDataViewListStore::wxDataViewListStore()
1423 wxDataViewListStore::~wxDataViewListStore()
1425 wxVector
<wxDataViewListStoreLine
*>::iterator it
;
1426 for (it
= m_data
.begin(); it
!= m_data
.end(); ++it
)
1428 wxDataViewListStoreLine
* line
= *it
;
1433 void wxDataViewListStore::PrependColumn( const wxString
&varianttype
)
1435 m_cols
.Insert( varianttype
, 0 );
1438 void wxDataViewListStore::InsertColumn( unsigned int pos
, const wxString
&varianttype
)
1440 m_cols
.Insert( varianttype
, pos
);
1443 void wxDataViewListStore::AppendColumn( const wxString
&varianttype
)
1445 m_cols
.Add( varianttype
);
1448 unsigned int wxDataViewListStore::GetColumnCount() const
1450 return m_cols
.GetCount();
1453 wxString
wxDataViewListStore::GetColumnType( unsigned int pos
) const
1458 void wxDataViewListStore::AppendItem( const wxVector
<wxVariant
> &values
, wxClientData
*data
)
1460 wxDataViewListStoreLine
*line
= new wxDataViewListStoreLine( data
);
1461 line
->m_values
= values
;
1462 m_data
.push_back( line
);
1467 void wxDataViewListStore::PrependItem( const wxVector
<wxVariant
> &values
, wxClientData
*data
)
1469 wxDataViewListStoreLine
*line
= new wxDataViewListStoreLine( data
);
1470 line
->m_values
= values
;
1471 m_data
.insert( m_data
.begin(), line
);
1476 void wxDataViewListStore::InsertItem( unsigned int row
, const wxVector
<wxVariant
> &values
,
1477 wxClientData
*data
)
1479 wxDataViewListStoreLine
*line
= new wxDataViewListStoreLine( data
);
1480 line
->m_values
= values
;
1481 m_data
.insert( m_data
.begin()+row
, line
);
1486 void wxDataViewListStore::DeleteItem( unsigned int row
)
1488 wxVector
<wxDataViewListStoreLine
*>::iterator it
= m_data
.begin() + row
;
1495 void wxDataViewListStore::DeleteAllItems()
1497 wxVector
<wxDataViewListStoreLine
*>::iterator it
;
1498 for (it
= m_data
.begin(); it
!= m_data
.end(); ++it
)
1500 wxDataViewListStoreLine
* line
= *it
;
1509 void wxDataViewListStore::GetValueByRow( wxVariant
&value
, unsigned int row
, unsigned int col
) const
1511 wxDataViewListStoreLine
*line
= m_data
[row
];
1512 value
= line
->m_values
[col
];
1515 bool wxDataViewListStore::SetValueByRow( const wxVariant
&value
, unsigned int row
, unsigned int col
)
1517 wxDataViewListStoreLine
*line
= m_data
[row
];
1518 line
->m_values
[col
] = value
;
1523 //-----------------------------------------------------------------------------
1524 // wxDataViewListCtrl
1525 //-----------------------------------------------------------------------------
1527 IMPLEMENT_DYNAMIC_CLASS(wxDataViewListCtrl
,wxDataViewCtrl
)
1529 BEGIN_EVENT_TABLE(wxDataViewListCtrl
,wxDataViewCtrl
)
1530 EVT_SIZE( wxDataViewListCtrl::OnSize
)
1533 wxDataViewListCtrl::wxDataViewListCtrl()
1537 wxDataViewListCtrl::wxDataViewListCtrl( wxWindow
*parent
, wxWindowID id
,
1538 const wxPoint
& pos
, const wxSize
& size
, long style
,
1539 const wxValidator
& validator
)
1541 Create( parent
, id
, pos
, size
, style
, validator
);
1543 wxDataViewListStore
*store
= new wxDataViewListStore
;
1544 AssociateModel( store
);
1548 wxDataViewListCtrl::~wxDataViewListCtrl()
1553 bool wxDataViewListCtrl::Create( wxWindow
*parent
, wxWindowID id
,
1554 const wxPoint
& pos
, const wxSize
& size
, long style
,
1555 const wxValidator
& validator
)
1557 return wxDataViewCtrl::Create( parent
, id
, pos
, size
, style
, validator
);
1560 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn
*column
, const wxString
&varianttype
)
1562 GetStore()->AppendColumn( varianttype
);
1563 return wxDataViewCtrl::AppendColumn( column
);
1566 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn
*column
, const wxString
&varianttype
)
1568 GetStore()->PrependColumn( varianttype
);
1569 return wxDataViewCtrl::PrependColumn( column
);
1572 bool wxDataViewListCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*column
, const wxString
&varianttype
)
1574 GetStore()->InsertColumn( pos
, varianttype
);
1575 return wxDataViewCtrl::InsertColumn( pos
, column
);
1578 bool wxDataViewListCtrl::PrependColumn( wxDataViewColumn
*col
)
1580 return PrependColumn( col
, "string" );
1583 bool wxDataViewListCtrl::InsertColumn( unsigned int pos
, wxDataViewColumn
*col
)
1585 return InsertColumn( pos
, col
, "string" );
1588 bool wxDataViewListCtrl::AppendColumn( wxDataViewColumn
*col
)
1590 return AppendColumn( col
, "string" );
1593 wxDataViewColumn
*wxDataViewListCtrl::AppendTextColumn( const wxString
&label
,
1594 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1596 GetStore()->AppendColumn( wxT("string") );
1598 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1599 new wxDataViewTextRenderer( wxT("string"), mode
),
1600 GetStore()->GetColumnCount()-1, width
, align
, flags
);
1602 wxDataViewCtrl::AppendColumn( ret
);
1607 wxDataViewColumn
*wxDataViewListCtrl::AppendToggleColumn( const wxString
&label
,
1608 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1610 GetStore()->AppendColumn( wxT("bool") );
1612 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1613 new wxDataViewToggleRenderer( wxT("bool"), mode
),
1614 GetStore()->GetColumnCount()-1, width
, align
, flags
);
1616 wxDataViewCtrl::AppendColumn( ret
);
1621 wxDataViewColumn
*wxDataViewListCtrl::AppendProgressColumn( const wxString
&label
,
1622 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1624 GetStore()->AppendColumn( wxT("long") );
1626 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1627 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
),
1628 GetStore()->GetColumnCount()-1, width
, align
, flags
);
1630 wxDataViewCtrl::AppendColumn( ret
);
1635 wxDataViewColumn
*wxDataViewListCtrl::AppendIconTextColumn( const wxString
&label
,
1636 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
1638 GetStore()->AppendColumn( wxT("wxDataViewIconText") );
1640 wxDataViewColumn
*ret
= new wxDataViewColumn( label
,
1641 new wxDataViewIconTextRenderer( wxT("wxDataViewIconText"), mode
),
1642 GetStore()->GetColumnCount()-1, width
, align
, flags
);
1644 wxDataViewCtrl::AppendColumn( ret
);
1649 void wxDataViewListCtrl::OnSize( wxSizeEvent
&event
)
1654 //-----------------------------------------------------------------------------
1655 // wxDataViewTreeStore
1656 //-----------------------------------------------------------------------------
1658 wxDataViewTreeStoreNode::wxDataViewTreeStoreNode(
1659 wxDataViewTreeStoreNode
*parent
,
1660 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1668 wxDataViewTreeStoreNode::~wxDataViewTreeStoreNode()
1674 #include "wx/listimpl.cpp"
1675 WX_DEFINE_LIST(wxDataViewTreeStoreNodeList
)
1677 wxDataViewTreeStoreContainerNode::wxDataViewTreeStoreContainerNode(
1678 wxDataViewTreeStoreNode
*parent
, const wxString
&text
,
1679 const wxIcon
&icon
, const wxIcon
&expanded
, wxClientData
*data
) :
1680 wxDataViewTreeStoreNode( parent
, text
, icon
, data
)
1682 m_iconExpanded
= expanded
;
1683 m_isExpanded
= false;
1684 m_children
.DeleteContents(true);
1687 wxDataViewTreeStoreContainerNode::~wxDataViewTreeStoreContainerNode()
1691 //-----------------------------------------------------------------------------
1693 wxDataViewTreeStore::wxDataViewTreeStore()
1695 m_root
= new wxDataViewTreeStoreContainerNode( NULL
, wxEmptyString
);
1698 wxDataViewTreeStore::~wxDataViewTreeStore()
1703 wxDataViewItem
wxDataViewTreeStore::AppendItem( const wxDataViewItem
& parent
,
1704 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1706 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1707 if (!parent_node
) return wxDataViewItem(0);
1709 wxDataViewTreeStoreNode
*node
=
1710 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1711 parent_node
->GetChildren().Append( node
);
1713 return node
->GetItem();
1716 wxDataViewItem
wxDataViewTreeStore::PrependItem( const wxDataViewItem
& parent
,
1717 const wxString
&text
, const wxIcon
&icon
, wxClientData
*data
)
1719 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1720 if (!parent_node
) return wxDataViewItem(0);
1722 wxDataViewTreeStoreNode
*node
=
1723 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1724 parent_node
->GetChildren().Insert( node
);
1726 return node
->GetItem();
1730 wxDataViewTreeStore::InsertItem(const wxDataViewItem
& parent
,
1731 const wxDataViewItem
& previous
,
1732 const wxString
& text
,
1736 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1737 if (!parent_node
) return wxDataViewItem(0);
1739 wxDataViewTreeStoreNode
*previous_node
= FindNode( previous
);
1740 int pos
= parent_node
->GetChildren().IndexOf( previous_node
);
1741 if (pos
== wxNOT_FOUND
) return wxDataViewItem(0);
1743 wxDataViewTreeStoreNode
*node
=
1744 new wxDataViewTreeStoreNode( parent_node
, text
, icon
, data
);
1745 parent_node
->GetChildren().Insert( (size_t) pos
, node
);
1747 return node
->GetItem();
1750 wxDataViewItem
wxDataViewTreeStore::PrependContainer( const wxDataViewItem
& parent
,
1751 const wxString
&text
, const wxIcon
&icon
, const wxIcon
&expanded
,
1752 wxClientData
*data
)
1754 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1755 if (!parent_node
) return wxDataViewItem(0);
1757 wxDataViewTreeStoreContainerNode
*node
=
1758 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1759 parent_node
->GetChildren().Insert( node
);
1761 return node
->GetItem();
1765 wxDataViewTreeStore::AppendContainer(const wxDataViewItem
& parent
,
1766 const wxString
&text
,
1768 const wxIcon
& expanded
,
1769 wxClientData
* data
)
1771 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1772 if (!parent_node
) return wxDataViewItem(0);
1774 wxDataViewTreeStoreContainerNode
*node
=
1775 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1776 parent_node
->GetChildren().Append( node
);
1778 return node
->GetItem();
1782 wxDataViewTreeStore::InsertContainer(const wxDataViewItem
& parent
,
1783 const wxDataViewItem
& previous
,
1784 const wxString
& text
,
1786 const wxIcon
& expanded
,
1787 wxClientData
* data
)
1789 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1790 if (!parent_node
) return wxDataViewItem(0);
1792 wxDataViewTreeStoreNode
*previous_node
= FindNode( previous
);
1793 int pos
= parent_node
->GetChildren().IndexOf( previous_node
);
1794 if (pos
== wxNOT_FOUND
) return wxDataViewItem(0);
1796 wxDataViewTreeStoreContainerNode
*node
=
1797 new wxDataViewTreeStoreContainerNode( parent_node
, text
, icon
, expanded
, data
);
1798 parent_node
->GetChildren().Insert( (size_t) pos
, node
);
1800 return node
->GetItem();
1803 bool wxDataViewTreeStore::IsContainer( const wxDataViewItem
& item
) const
1805 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1806 if (!node
) return false;
1808 return node
->IsContainer();
1811 wxDataViewItem
wxDataViewTreeStore::GetNthChild( const wxDataViewItem
& parent
, unsigned int pos
) const
1813 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent
);
1814 if (!parent_node
) return wxDataViewItem(0);
1816 wxDataViewTreeStoreNodeList::compatibility_iterator node
= parent_node
->GetChildren().Item( pos
);
1818 return node
->GetData();
1820 return wxDataViewItem(0);
1823 int wxDataViewTreeStore::GetChildCount( const wxDataViewItem
& parent
) const
1825 wxDataViewTreeStoreNode
*node
= FindNode( parent
);
1826 if (!node
) return -1;
1828 if (!node
->IsContainer())
1831 wxDataViewTreeStoreContainerNode
*container_node
= (wxDataViewTreeStoreContainerNode
*) node
;
1832 return (int) container_node
->GetChildren().GetCount();
1835 void wxDataViewTreeStore::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
1837 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1840 node
->SetText( text
);
1843 wxString
wxDataViewTreeStore::GetItemText( const wxDataViewItem
& item
) const
1845 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1846 if (!node
) return wxEmptyString
;
1848 return node
->GetText();
1851 void wxDataViewTreeStore::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1853 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1856 node
->SetIcon( icon
);
1859 const wxIcon
&wxDataViewTreeStore::GetItemIcon( const wxDataViewItem
& item
) const
1861 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1862 if (!node
) return wxNullIcon
;
1864 return node
->GetIcon();
1867 void wxDataViewTreeStore::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
1869 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1872 node
->SetExpandedIcon( icon
);
1875 const wxIcon
&wxDataViewTreeStore::GetItemExpandedIcon( const wxDataViewItem
& item
) const
1877 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1878 if (!node
) return wxNullIcon
;
1880 return node
->GetExpandedIcon();
1883 void wxDataViewTreeStore::SetItemData( const wxDataViewItem
& item
, wxClientData
*data
)
1885 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1888 node
->SetData( data
);
1891 wxClientData
*wxDataViewTreeStore::GetItemData( const wxDataViewItem
& item
) const
1893 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1894 if (!node
) return NULL
;
1896 return node
->GetData();
1899 void wxDataViewTreeStore::DeleteItem( const wxDataViewItem
& item
)
1901 if (!item
.IsOk()) return;
1903 wxDataViewItem parent_item
= GetParent( item
);
1905 wxDataViewTreeStoreContainerNode
*parent_node
= FindContainerNode( parent_item
);
1906 if (!parent_node
) return;
1908 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1911 parent_node
->GetChildren().DeleteObject( node
);
1914 void wxDataViewTreeStore::DeleteChildren( const wxDataViewItem
& item
)
1916 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1919 node
->GetChildren().clear();
1922 void wxDataViewTreeStore::DeleteAllItems()
1924 DeleteChildren(m_root
);
1928 wxDataViewTreeStore::GetValue(wxVariant
&variant
,
1929 const wxDataViewItem
&item
,
1930 unsigned int WXUNUSED(col
)) const
1932 // if (col != 0) return;
1934 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1937 wxIcon
icon( node
->GetIcon());
1938 if (node
->IsContainer())
1940 wxDataViewTreeStoreContainerNode
*container
= (wxDataViewTreeStoreContainerNode
*) node
;
1941 if (container
->IsExpanded() && container
->GetExpandedIcon().IsOk())
1942 icon
= container
->GetExpandedIcon();
1945 wxDataViewIconText
data( node
->GetText(), icon
);
1951 wxDataViewTreeStore::SetValue(const wxVariant
& variant
,
1952 const wxDataViewItem
& item
,
1953 unsigned int WXUNUSED(col
))
1955 // if (col != 0) return false;
1957 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1958 if (!node
) return false;
1960 wxDataViewIconText data
;
1964 node
->SetText( data
.GetText() );
1965 node
->SetIcon( data
.GetIcon() );
1970 wxDataViewItem
wxDataViewTreeStore::GetParent( const wxDataViewItem
&item
) const
1972 wxDataViewTreeStoreNode
*node
= FindNode( item
);
1973 if (!node
) return wxDataViewItem(0);
1975 wxDataViewTreeStoreNode
*parent
= node
->GetParent();
1976 if (!parent
) return wxDataViewItem(0);
1978 if (parent
== m_root
)
1979 return wxDataViewItem(0);
1981 return parent
->GetItem();
1984 unsigned int wxDataViewTreeStore::GetChildren( const wxDataViewItem
&item
, wxDataViewItemArray
&children
) const
1986 wxDataViewTreeStoreContainerNode
*node
= FindContainerNode( item
);
1987 if (!node
) return 0;
1989 wxDataViewTreeStoreNodeList::iterator iter
;
1990 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
1992 wxDataViewTreeStoreNode
* child
= *iter
;
1993 children
.Add( child
->GetItem() );
1996 return node
->GetChildren().GetCount();
1999 int wxDataViewTreeStore::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
2000 unsigned int WXUNUSED(column
), bool WXUNUSED(ascending
) ) const
2002 wxDataViewTreeStoreNode
*node1
= FindNode( item1
);
2003 wxDataViewTreeStoreNode
*node2
= FindNode( item2
);
2005 if (!node1
|| !node2
)
2008 wxDataViewTreeStoreContainerNode
* parent1
=
2009 (wxDataViewTreeStoreContainerNode
*) node1
->GetParent();
2010 wxDataViewTreeStoreContainerNode
* parent2
=
2011 (wxDataViewTreeStoreContainerNode
*) node2
->GetParent();
2013 if (parent1
!= parent2
)
2015 wxLogError( wxT("Comparing items with different parent.") );
2019 if (node1
->IsContainer() && !node2
->IsContainer())
2022 if (node2
->IsContainer() && !node1
->IsContainer())
2025 return parent1
->GetChildren().IndexOf( node1
) - parent2
->GetChildren().IndexOf( node2
);
2028 wxDataViewTreeStoreNode
*wxDataViewTreeStore::FindNode( const wxDataViewItem
&item
) const
2033 return (wxDataViewTreeStoreNode
*) item
.GetID();
2036 wxDataViewTreeStoreContainerNode
*wxDataViewTreeStore::FindContainerNode( const wxDataViewItem
&item
) const
2039 return (wxDataViewTreeStoreContainerNode
*) m_root
;
2041 wxDataViewTreeStoreNode
* node
= (wxDataViewTreeStoreNode
*) item
.GetID();
2043 if (!node
->IsContainer())
2046 return (wxDataViewTreeStoreContainerNode
*) node
;
2049 //-----------------------------------------------------------------------------
2050 // wxDataViewTreeCtrl
2051 //-----------------------------------------------------------------------------
2053 IMPLEMENT_DYNAMIC_CLASS(wxDataViewTreeCtrl
,wxDataViewCtrl
)
2055 BEGIN_EVENT_TABLE(wxDataViewTreeCtrl
,wxDataViewCtrl
)
2056 EVT_DATAVIEW_ITEM_EXPANDED(-1, wxDataViewTreeCtrl::OnExpanded
)
2057 EVT_DATAVIEW_ITEM_COLLAPSED(-1, wxDataViewTreeCtrl::OnCollapsed
)
2058 EVT_SIZE( wxDataViewTreeCtrl::OnSize
)
2061 wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
2066 bool wxDataViewTreeCtrl::Create( wxWindow
*parent
, wxWindowID id
,
2067 const wxPoint
& pos
, const wxSize
& size
, long style
, const wxValidator
& validator
)
2069 if ( !wxDataViewCtrl::Create( parent
, id
, pos
, size
, style
, validator
) )
2072 // create the standard model and a column in the tree
2073 wxDataViewTreeStore
*store
= new wxDataViewTreeStore
;
2074 AssociateModel( store
);
2077 AppendIconTextColumn
2079 wxString(), // no label (header is not shown anyhow)
2080 0, // the only model column
2081 wxDATAVIEW_CELL_EDITABLE
,
2082 -1, // default width
2083 wxALIGN_NOT
, // and alignment
2090 void wxDataViewTreeCtrl::SetImageList( wxImageList
*imagelist
)
2094 m_imageList
= imagelist
;
2097 wxDataViewItem
wxDataViewTreeCtrl::AppendItem( const wxDataViewItem
& parent
,
2098 const wxString
&text
, int iconIndex
, wxClientData
*data
)
2100 wxIcon icon
= wxNullIcon
;
2101 if (m_imageList
&& (iconIndex
!= -1))
2102 icon
= m_imageList
->GetIcon( iconIndex
);
2104 wxDataViewItem res
= GetStore()->AppendItem( parent
, text
, icon
, data
);
2106 GetStore()->ItemAdded( parent
, res
);
2111 wxDataViewItem
wxDataViewTreeCtrl::PrependItem( const wxDataViewItem
& parent
,
2112 const wxString
&text
, int iconIndex
, wxClientData
*data
)
2114 wxIcon icon
= wxNullIcon
;
2115 if (m_imageList
&& (iconIndex
!= -1))
2116 icon
= m_imageList
->GetIcon( iconIndex
);
2118 wxDataViewItem res
= GetStore()->PrependItem( parent
, text
, icon
, data
);
2120 GetStore()->ItemAdded( parent
, res
);
2125 wxDataViewItem
wxDataViewTreeCtrl::InsertItem( const wxDataViewItem
& parent
, const wxDataViewItem
& previous
,
2126 const wxString
&text
, int iconIndex
, wxClientData
*data
)
2128 wxIcon icon
= wxNullIcon
;
2129 if (m_imageList
&& (iconIndex
!= -1))
2130 icon
= m_imageList
->GetIcon( iconIndex
);
2132 wxDataViewItem res
= GetStore()->InsertItem( parent
, previous
, text
, icon
, data
);
2134 GetStore()->ItemAdded( parent
, res
);
2139 wxDataViewItem
wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem
& parent
,
2140 const wxString
&text
, int iconIndex
, int expandedIndex
, wxClientData
*data
)
2142 wxIcon icon
= wxNullIcon
;
2143 if (m_imageList
&& (iconIndex
!= -1))
2144 icon
= m_imageList
->GetIcon( iconIndex
);
2146 wxIcon expanded
= wxNullIcon
;
2147 if (m_imageList
&& (expandedIndex
!= -1))
2148 expanded
= m_imageList
->GetIcon( expandedIndex
);
2150 wxDataViewItem res
= GetStore()->PrependContainer( parent
, text
, icon
, expanded
, data
);
2152 GetStore()->ItemAdded( parent
, res
);
2157 wxDataViewItem
wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem
& parent
,
2158 const wxString
&text
, int iconIndex
, int expandedIndex
, wxClientData
*data
)
2160 wxIcon icon
= wxNullIcon
;
2161 if (m_imageList
&& (iconIndex
!= -1))
2162 icon
= m_imageList
->GetIcon( iconIndex
);
2164 wxIcon expanded
= wxNullIcon
;
2165 if (m_imageList
&& (expandedIndex
!= -1))
2166 expanded
= m_imageList
->GetIcon( expandedIndex
);
2168 wxDataViewItem res
= GetStore()->AppendContainer( parent
, text
, icon
, expanded
, data
);
2170 GetStore()->ItemAdded( parent
, res
);
2175 wxDataViewItem
wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem
& parent
, const wxDataViewItem
& previous
,
2176 const wxString
&text
, int iconIndex
, int expandedIndex
, wxClientData
*data
)
2178 wxIcon icon
= wxNullIcon
;
2179 if (m_imageList
&& (iconIndex
!= -1))
2180 icon
= m_imageList
->GetIcon( iconIndex
);
2182 wxIcon expanded
= wxNullIcon
;
2183 if (m_imageList
&& (expandedIndex
!= -1))
2184 expanded
= m_imageList
->GetIcon( expandedIndex
);
2186 wxDataViewItem res
= GetStore()->InsertContainer( parent
, previous
, text
, icon
, expanded
, data
);
2188 GetStore()->ItemAdded( parent
, res
);
2193 void wxDataViewTreeCtrl::SetItemText( const wxDataViewItem
& item
, const wxString
&text
)
2195 GetStore()->SetItemText(item
,text
);
2198 GetStore()->ValueChanged( item
, 0 );
2201 void wxDataViewTreeCtrl::SetItemIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
2203 GetStore()->SetItemIcon(item
,icon
);
2206 GetStore()->ValueChanged( item
, 0 );
2209 void wxDataViewTreeCtrl::SetItemExpandedIcon( const wxDataViewItem
& item
, const wxIcon
&icon
)
2211 GetStore()->SetItemExpandedIcon(item
,icon
);
2214 GetStore()->ValueChanged( item
, 0 );
2217 void wxDataViewTreeCtrl::DeleteItem( const wxDataViewItem
& item
)
2219 wxDataViewItem parent_item
= GetStore()->GetParent( item
);
2221 GetStore()->DeleteItem(item
);
2224 GetStore()->ItemDeleted( parent_item
, item
);
2227 void wxDataViewTreeCtrl::DeleteChildren( const wxDataViewItem
& item
)
2229 wxDataViewTreeStoreContainerNode
*node
= GetStore()->FindContainerNode( item
);
2232 wxDataViewItemArray array
;
2233 wxDataViewTreeStoreNodeList::iterator iter
;
2234 for (iter
= node
->GetChildren().begin(); iter
!= node
->GetChildren().end(); iter
++)
2236 wxDataViewTreeStoreNode
* child
= *iter
;
2237 array
.Add( child
->GetItem() );
2240 GetStore()->DeleteChildren( item
);
2243 GetStore()->ItemsDeleted( item
, array
);
2246 void wxDataViewTreeCtrl::DeleteAllItems()
2248 GetStore()->DeleteAllItems();
2250 GetStore()->Cleared();
2253 void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent
&event
)
2255 if (m_imageList
) return;
2257 wxDataViewTreeStoreContainerNode
* container
= GetStore()->FindContainerNode( event
.GetItem() );
2258 if (!container
) return;
2260 container
->SetExpanded( true );
2262 GetStore()->ItemChanged( event
.GetItem() );
2265 void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent
&event
)
2267 if (m_imageList
) return;
2269 wxDataViewTreeStoreContainerNode
* container
= GetStore()->FindContainerNode( event
.GetItem() );
2270 if (!container
) return;
2272 container
->SetExpanded( false );
2274 GetStore()->ItemChanged( event
.GetItem() );
2277 void wxDataViewTreeCtrl::OnSize( wxSizeEvent
&event
)
2279 #if defined(wxUSE_GENERICDATAVIEWCTRL)
2280 // automatically resize our only column to take the entire control width
2281 if ( GetColumnCount() )
2283 wxSize size
= GetClientSize();
2284 GetColumn(0)->SetWidth(size
.x
);
2290 #endif // wxUSE_DATAVIEWCTRL