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"
26 const wxChar wxDataViewCtrlNameStr
[] = wxT("dataviewCtrl");
29 // ---------------------------------------------------------
30 // wxDataViewListModel
31 // ---------------------------------------------------------
33 wxDataViewListModel::wxDataViewListModel()
35 m_viewingColumns
.DeleteContents( true );
36 m_notifiers
.DeleteContents( true );
39 wxDataViewListModel::~wxDataViewListModel()
41 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
44 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
46 node
= node
->GetNext();
50 bool wxDataViewListModel::RowAppended()
54 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
57 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
58 if (!notifier
->RowAppended())
60 node
= node
->GetNext();
66 bool wxDataViewListModel::RowPrepended()
70 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
73 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
74 if (!notifier
->RowPrepended())
76 node
= node
->GetNext();
82 bool wxDataViewListModel::RowInserted( unsigned int before
)
86 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
89 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
90 if (!notifier
->RowInserted(before
))
92 node
= node
->GetNext();
98 bool wxDataViewListModel::RowDeleted( unsigned int row
)
102 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
105 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
106 if (!notifier
->RowDeleted( row
))
108 node
= node
->GetNext();
114 bool wxDataViewListModel::RowChanged( unsigned int row
)
118 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
121 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
122 if (!notifier
->RowChanged( row
))
124 node
= node
->GetNext();
130 bool wxDataViewListModel::ValueChanged( unsigned int col
, unsigned int row
)
134 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
137 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
138 if (!notifier
->ValueChanged( col
, row
))
140 node
= node
->GetNext();
146 bool wxDataViewListModel::RowsReordered( unsigned int *new_order
)
150 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
153 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
154 if (!notifier
->RowsReordered( new_order
))
156 node
= node
->GetNext();
162 bool wxDataViewListModel::Cleared()
166 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
169 wxDataViewListModelNotifier
* notifier
= (wxDataViewListModelNotifier
*) node
->GetData();
170 if (!notifier
->Cleared())
172 node
= node
->GetNext();
178 void wxDataViewListModel::AddViewingColumn( wxDataViewColumn
*view_column
, unsigned int model_column
)
180 m_viewingColumns
.Append( new wxDataViewViewingColumn( view_column
, model_column
) );
183 void wxDataViewListModel::RemoveViewingColumn( wxDataViewColumn
*column
)
185 wxList::compatibility_iterator node
= m_viewingColumns
.GetFirst();
188 wxDataViewViewingColumn
* tmp
= (wxDataViewViewingColumn
*) node
->GetData();
190 if (tmp
->m_viewColumn
== column
)
192 m_viewingColumns
.DeleteObject( tmp
);
196 node
= node
->GetNext();
200 void wxDataViewListModel::AddNotifier( wxDataViewListModelNotifier
*notifier
)
202 m_notifiers
.Append( notifier
);
203 notifier
->SetOwner( this );
206 void wxDataViewListModel::RemoveNotifier( wxDataViewListModelNotifier
*notifier
)
208 m_notifiers
.DeleteObject( notifier
);
211 // ---------------------------------------------------------
212 // wxDataViewSortedListModelNotifier
213 // ---------------------------------------------------------
215 class wxDataViewSortedListModelNotifier
: public wxDataViewListModelNotifier
218 wxDataViewSortedListModelNotifier( wxDataViewSortedListModel
*model
)
221 virtual bool RowAppended()
222 { return m_model
->ChildRowAppended(); }
224 virtual bool RowPrepended()
225 { return m_model
->ChildRowPrepended(); }
227 virtual bool RowInserted( unsigned int before
)
228 { return m_model
->ChildRowInserted( before
); }
230 virtual bool RowDeleted( unsigned int row
)
231 { return m_model
->ChildRowDeleted( row
); }
233 virtual bool RowChanged( unsigned int row
)
234 { return m_model
->ChildRowChanged( row
); }
236 virtual bool ValueChanged( unsigned int col
, unsigned int row
)
237 { return m_model
->ChildValueChanged( col
, row
); }
239 virtual bool RowsReordered( unsigned int *new_order
)
240 { return m_model
->ChildRowsReordered( new_order
); }
242 virtual bool Cleared()
243 { return m_model
->ChildCleared(); }
246 { m_model
->m_child
= NULL
; return wxDataViewListModelNotifier::Freed(); }
248 wxDataViewSortedListModel
*m_model
;
251 // ---------------------------------------------------------
252 // wxDataViewSortedListModel compare function
253 // ---------------------------------------------------------
255 int wxCALLBACK wxDataViewListModelSortedDefaultCompare
256 (unsigned int row1
, unsigned int row2
, unsigned int col
, wxDataViewListModel
* model
)
258 wxVariant value1
,value2
;
259 model
->GetValue( value1
, col
, row1
);
260 model
->GetValue( value2
, col
, row2
);
261 if (value1
.GetType() == wxT("string"))
263 wxString str1
= value1
.GetString();
264 wxString str2
= value2
.GetString();
265 return str1
.Cmp( str2
);
267 if (value1
.GetType() == wxT("long"))
269 long l1
= value1
.GetLong();
270 long l2
= value2
.GetLong();
273 if (value1
.GetType() == wxT("double"))
275 double d1
= value1
.GetDouble();
276 double d2
= value2
.GetDouble();
277 if (d1
== d2
) return 0;
278 if (d1
< d2
) return 1;
281 if (value1
.GetType() == wxT("datetime"))
283 wxDateTime dt1
= value1
.GetDateTime();
284 wxDateTime dt2
= value2
.GetDateTime();
285 if (dt1
.IsEqualTo(dt2
)) return 0;
286 if (dt1
.IsEarlierThan(dt2
)) return 1;
293 int wxCALLBACK wxDataViewListModelSortedDefaultCompareDescending
294 (unsigned int row1
, unsigned int row2
, unsigned int col
, wxDataViewListModel
* model
)
296 return wxDataViewListModelSortedDefaultCompare( row2
, row1
, col
, model
);
299 static wxDataViewListModelCompare s_CmpFunc
;
300 static wxDataViewListModel
*s_CmpModel
;
301 static unsigned int s_CmpCol
;
303 int LINKAGEMODE
wxDataViewIntermediateCmp( unsigned int row1
, unsigned int row2
)
305 return s_CmpFunc( row1
, row2
, s_CmpCol
, s_CmpModel
);
308 // ---------------------------------------------------------
309 // wxDataViewSortedListModel
310 // ---------------------------------------------------------
312 wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel
*child
) :
313 m_array( wxDataViewIntermediateCmp
)
319 m_notifierOnChild
= new wxDataViewSortedListModelNotifier( this );
320 m_child
->AddNotifier( m_notifierOnChild
);
325 wxDataViewSortedListModel::~wxDataViewSortedListModel()
328 m_child
->RemoveNotifier( m_notifierOnChild
);
332 void wxDataViewSortedListModel::InitStatics()
335 s_CmpModel
= m_child
;
337 s_CmpFunc
= wxDataViewListModelSortedDefaultCompare
;
339 s_CmpFunc
= wxDataViewListModelSortedDefaultCompareDescending
;
342 void wxDataViewSortedListModel::Resort()
347 unsigned int n
= m_child
->GetNumberOfRows();
349 for (i
= 0; i
< n
; i
++)
354 static void Dump( wxDataViewListModel
*model
, unsigned int col
)
356 unsigned int n
= model
->GetNumberOfRows();
358 for (i
= 0; i
< n
; i
++)
361 model
->GetValue( variant
, col
, i
);
363 tmp
= variant
.GetString();
364 wxPrintf( wxT("%d: %s\n"), (int) i
, tmp
.c_str() );
369 bool wxDataViewSortedListModel::ChildRowAppended()
371 // no need to fix up array
373 unsigned int len
= m_array
.GetCount();
375 unsigned int pos
= m_array
.Add( len
);
378 return wxDataViewListModel::RowPrepended();
381 return wxDataViewListModel::RowAppended();
383 return wxDataViewListModel::RowInserted( pos
);
386 bool wxDataViewSortedListModel::ChildRowPrepended()
390 unsigned int len
= m_array
.GetCount();
391 for (i
= 0; i
< len
; i
++)
393 unsigned int value
= m_array
[i
];
394 m_array
[i
] = value
+1;
397 unsigned int pos
= m_array
.Add( 0 );
400 return wxDataViewListModel::RowPrepended();
403 return wxDataViewListModel::RowAppended();
405 return wxDataViewListModel::RowInserted( pos
);
408 bool wxDataViewSortedListModel::ChildRowInserted( unsigned int before
)
412 unsigned int len
= m_array
.GetCount();
413 for (i
= 0; i
< len
; i
++)
415 unsigned int value
= m_array
[i
];
417 m_array
[i
] = value
+1;
420 unsigned int pos
= m_array
.Add( before
);
423 return wxDataViewListModel::RowPrepended();
426 return wxDataViewListModel::RowAppended();
428 return wxDataViewListModel::RowInserted( pos
);
431 bool wxDataViewSortedListModel::ChildRowDeleted( unsigned int row
)
434 unsigned int len
= m_array
.GetCount();
436 for (i
= 0; i
< len
; i
++)
438 unsigned int value
= m_array
[i
];
448 m_array
[i
] = value
-1;
453 return false; // we should probably assert
456 m_array
.RemoveAt( (unsigned int) pos
);
458 return wxDataViewListModel::RowDeleted( (unsigned int) pos
);
461 bool wxDataViewSortedListModel::ChildRowChanged( unsigned int row
)
464 unsigned int len
= m_array
.GetCount();
466 // Remove and readd sorted. Find out at which
467 // position it was and where it ended.
468 unsigned int start_pos
= 0,end_pos
= 0;
469 for (i
= 0; i
< len
; i
++)
470 if (m_array
[i
] == row
)
475 m_array
.RemoveAt( start_pos
);
478 for (i
= 0; i
< len
; i
++)
479 if (m_array
[i
] == row
)
485 if (end_pos
== start_pos
)
486 return wxDataViewListModel::RowChanged( start_pos
);
488 // Create an array where order[old] -> new_pos, so that
489 // if nothing changed order[0] -> 0 etc.
490 unsigned int *order
= new unsigned int[ len
];
491 // Fill up initial values.
492 for (i
= 0; i
< len
; i
++)
495 if (start_pos
< end_pos
)
497 for (i
= start_pos
; i
< end_pos
; i
++)
498 order
[i
] = order
[i
+1];
499 order
[end_pos
] = start_pos
;
503 for (i
= end_pos
; i
> start_pos
; i
--)
504 order
[i
] = order
[i
-1];
505 order
[start_pos
] = end_pos
;
508 wxDataViewListModel::RowsReordered( order
);
515 bool wxDataViewSortedListModel::ChildValueChanged( unsigned int col
, unsigned int row
)
518 unsigned int len
= m_array
.GetCount();
520 // Remove and readd sorted. Find out at which
521 // position it was and where it ended.
522 unsigned int start_pos
= 0,end_pos
= 0;
523 for (i
= 0; i
< len
; i
++)
524 if (m_array
[i
] == row
)
529 m_array
.RemoveAt( start_pos
);
532 for (i
= 0; i
< len
; i
++)
533 if (m_array
[i
] == row
)
539 if (end_pos
== start_pos
)
540 return wxDataViewListModel::ValueChanged( col
, start_pos
);
542 // Create an array where order[old] -> new_pos, so that
543 // if nothing changed order[0] -> 0 etc.
544 unsigned int *order
= new unsigned int[ len
];
545 // Fill up initial values.
546 for (i
= 0; i
< len
; i
++)
549 if (start_pos
< end_pos
)
551 for (i
= start_pos
; i
< end_pos
; i
++)
552 order
[i
] = order
[i
+1];
553 order
[end_pos
] = start_pos
;
557 for (i
= end_pos
; i
> start_pos
; i
--)
558 order
[i
] = order
[i
-1];
559 order
[start_pos
] = end_pos
;
562 wxDataViewListModel::RowsReordered( order
);
569 bool wxDataViewSortedListModel::ChildRowsReordered( unsigned int *WXUNUSED(new_order
) )
571 // Nothing needs to be done. If the sort criteria
572 // of this list don't change, the order of the
573 // items of the child list isn't relevant.
577 bool wxDataViewSortedListModel::ChildCleared()
579 return wxDataViewListModel::Cleared();
582 unsigned int wxDataViewSortedListModel::GetNumberOfRows()
584 return m_array
.GetCount();
587 unsigned int wxDataViewSortedListModel::GetNumberOfCols()
589 return m_child
->GetNumberOfCols();
592 wxString
wxDataViewSortedListModel::GetColType( unsigned int col
)
594 return m_child
->GetColType( col
);
597 void wxDataViewSortedListModel::GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
599 unsigned int child_row
= m_array
[row
];
600 m_child
->GetValue( variant
, col
, child_row
);
603 bool wxDataViewSortedListModel::SetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
605 unsigned int child_row
= m_array
[row
];
606 bool ret
= m_child
->SetValue( variant
, col
, child_row
);
608 // Do nothing here as the change in the
609 // child model will be reported back.
614 bool wxDataViewSortedListModel::RowAppended()
616 // you can only append
617 bool ret
= m_child
->RowAppended();
619 // Do nothing here as the change in the
620 // child model will be reported back.
625 bool wxDataViewSortedListModel::RowPrepended()
627 // you can only append
628 bool ret
= m_child
->RowAppended();
630 // Do nothing here as the change in the
631 // child model will be reported back.
636 bool wxDataViewSortedListModel::RowInserted( unsigned int WXUNUSED(before
) )
638 // you can only append
639 bool ret
= m_child
->RowAppended();
641 // Do nothing here as the change in the
642 // child model will be reported back.
647 bool wxDataViewSortedListModel::RowDeleted( unsigned int row
)
649 unsigned int child_row
= m_array
[row
];
651 bool ret
= m_child
->RowDeleted( child_row
);
653 // Do nothing here as the change in the
654 // child model will be reported back.
659 bool wxDataViewSortedListModel::RowChanged( unsigned int row
)
661 unsigned int child_row
= m_array
[row
];
662 bool ret
= m_child
->RowChanged( child_row
);
664 // Do nothing here as the change in the
665 // child model will be reported back.
670 bool wxDataViewSortedListModel::ValueChanged( unsigned int col
, unsigned int row
)
672 unsigned int child_row
= m_array
[row
];
673 bool ret
= m_child
->ValueChanged( col
, child_row
);
675 // Do nothing here as the change in the
676 // child model will be reported back.
681 bool wxDataViewSortedListModel::RowsReordered( unsigned int *WXUNUSED(new_order
) )
683 // We sort them ourselves.
688 bool wxDataViewSortedListModel::Cleared()
690 bool ret
= m_child
->Cleared();
692 // Do nothing here as the change in the
693 // child model will be reported back.
698 // ---------------------------------------------------------
699 // wxDataViewRendererBase
700 // ---------------------------------------------------------
702 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
704 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
, wxDataViewCellMode mode
)
706 m_variantType
= varianttype
;
710 // ---------------------------------------------------------
711 // wxDataViewColumnBase
712 // ---------------------------------------------------------
714 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
716 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& title
,
717 wxDataViewRenderer
*renderer
,
718 unsigned int model_column
,
720 wxAlignment
WXUNUSED(align
),
723 m_renderer
= renderer
;
724 m_model_column
= model_column
;
728 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
731 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
732 wxDataViewRenderer
*renderer
,
733 unsigned int model_column
,
735 wxAlignment
WXUNUSED(align
),
738 m_renderer
= renderer
;
739 m_model_column
= model_column
;
743 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
746 wxDataViewColumnBase::~wxDataViewColumnBase()
753 GetOwner()->GetModel()->RemoveViewingColumn( (wxDataViewColumn
*) this );
757 void wxDataViewColumnBase::SetTitle( const wxString
&title
)
762 wxString
wxDataViewColumnBase::GetTitle()
767 void wxDataViewColumnBase::SetBitmap( const wxBitmap
&bitmap
)
772 const wxBitmap
&wxDataViewColumnBase::GetBitmap()
777 // ---------------------------------------------------------
778 // wxDataViewCtrlBase
779 // ---------------------------------------------------------
781 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
783 wxDataViewCtrlBase::wxDataViewCtrlBase()
786 m_cols
.DeleteContents( true );
789 wxDataViewCtrlBase::~wxDataViewCtrlBase()
791 // IMPORTANT: before calling DecRef() on our model (since it may
792 // result in a free() call), erase all columns (since
793 // they hold a pointer to our model)
803 bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel
*model
)
806 m_model
->DecRef(); // discard old model, if any
808 // add our own reference to the new model:
816 wxDataViewListModel
* wxDataViewCtrlBase::GetModel()
821 bool wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
822 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
824 return AppendColumn( new wxDataViewColumn( label
,
825 new wxDataViewTextRenderer( wxT("string"), mode
), model_column
,
826 width
, align
, flags
) );
829 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
830 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
832 return AppendColumn( new wxDataViewColumn( label
,
833 new wxDataViewToggleRenderer( wxT("bool"), mode
), model_column
,
834 width
, align
, flags
) );
837 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
838 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
840 return AppendColumn( new wxDataViewColumn( label
,
841 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
), model_column
,
842 width
, align
, flags
) );
845 bool wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
846 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
848 return AppendColumn( new wxDataViewColumn( label
,
849 new wxDataViewDateRenderer( wxT("datetime"), mode
), model_column
,
850 width
, align
, flags
) );
853 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
854 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
856 return AppendColumn( new wxDataViewColumn( label
,
857 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
), model_column
,
858 width
, align
, flags
) );
861 bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
862 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
864 return AppendColumn( new wxDataViewColumn( label
,
865 new wxDataViewTextRenderer( wxT("string"), mode
), model_column
,
866 width
, align
, flags
) );
869 bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
870 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
872 return AppendColumn( new wxDataViewColumn( label
,
873 new wxDataViewToggleRenderer( wxT("bool"), mode
), model_column
,
874 width
, align
, flags
) );
877 bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
878 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
880 return AppendColumn( new wxDataViewColumn( label
,
881 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
), model_column
,
882 width
, align
, flags
) );
885 bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
886 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
888 return AppendColumn( new wxDataViewColumn( label
,
889 new wxDataViewDateRenderer( wxT("datetime"), mode
), model_column
,
890 width
, align
, flags
) );
893 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
894 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
896 return AppendColumn( new wxDataViewColumn( label
,
897 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
), model_column
,
898 width
, align
, flags
) );
901 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
903 m_cols
.Append( (wxObject
*) col
);
904 col
->SetOwner( (wxDataViewCtrl
*) this );
905 m_model
->AddViewingColumn( col
, col
->GetModelColumn() );
909 unsigned int wxDataViewCtrlBase::GetNumberOfColumns()
911 return m_cols
.GetCount();
914 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos
) )
919 bool wxDataViewCtrlBase::ClearColumns()
924 wxDataViewColumn
* wxDataViewCtrlBase::GetColumn( unsigned int pos
)
926 return (wxDataViewColumn
*) m_cols
[ pos
];
929 // ---------------------------------------------------------
931 // ---------------------------------------------------------
933 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
935 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED
)
936 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED
)
937 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
938 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)