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");
28 // ---------------------------------------------------------
30 // ---------------------------------------------------------
32 IMPLEMENT_ABSTRACT_CLASS(wxDataViewModel
, wxObject
)
34 // ---------------------------------------------------------
35 // wxDataViewListModel
36 // ---------------------------------------------------------
38 IMPLEMENT_ABSTRACT_CLASS(wxDataViewListModel
, wxDataViewModel
)
40 wxDataViewListModel::wxDataViewListModel()
42 m_viewingColumns
.DeleteContents( true );
43 m_notifiers
.DeleteContents( true );
46 wxDataViewListModel::~wxDataViewListModel()
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(); }
245 wxDataViewSortedListModel
*m_model
;
248 // ---------------------------------------------------------
249 // wxDataViewSortedListModel compare function
250 // ---------------------------------------------------------
252 int wxCALLBACK wxDataViewListModelSortedDefaultCompare
253 (unsigned int row1
, unsigned int row2
, unsigned int col
, wxDataViewListModel
* model
)
255 wxVariant value1
,value2
;
256 model
->GetValue( value1
, col
, row1
);
257 model
->GetValue( value2
, col
, row2
);
258 if (value1
.GetType() == wxT("string"))
260 wxString str1
= value1
.GetString();
261 wxString str2
= value2
.GetString();
262 return str1
.Cmp( str2
);
264 if (value1
.GetType() == wxT("long"))
266 long l1
= value1
.GetLong();
267 long l2
= value2
.GetLong();
270 if (value1
.GetType() == wxT("double"))
272 double d1
= value1
.GetDouble();
273 double d2
= value2
.GetDouble();
274 if (d1
== d2
) return 0;
275 if (d1
< d2
) return 1;
278 if (value1
.GetType() == wxT("datetime"))
280 wxDateTime dt1
= value1
.GetDateTime();
281 wxDateTime dt2
= value2
.GetDateTime();
282 if (dt1
.IsEqualTo(dt2
)) return 0;
283 if (dt1
.IsEarlierThan(dt2
)) return 1;
290 static wxDataViewListModelCompare s_CmpFunc
;
291 static wxDataViewListModel
*s_CmpModel
;
292 static unsigned int s_CmpCol
;
294 int LINKAGEMODE
wxDataViewIntermediateCmp( unsigned int row1
, unsigned int row2
)
296 return s_CmpFunc( row1
, row2
, s_CmpCol
, s_CmpModel
);
299 // ---------------------------------------------------------
300 // wxDataViewSortedListModel
301 // ---------------------------------------------------------
303 IMPLEMENT_ABSTRACT_CLASS(wxDataViewSortedListModel
, wxDataViewListModel
)
305 wxDataViewSortedListModel::wxDataViewSortedListModel( wxDataViewListModel
*child
) :
306 m_array( wxDataViewIntermediateCmp
)
311 s_CmpFunc
= wxDataViewListModelSortedDefaultCompare
;
313 m_notifierOnChild
= new wxDataViewSortedListModelNotifier( this );
314 m_child
->AddNotifier( m_notifierOnChild
);
319 wxDataViewSortedListModel::~wxDataViewSortedListModel()
321 m_child
->RemoveNotifier( m_notifierOnChild
);
324 void wxDataViewSortedListModel::Resort()
327 unsigned int n
= m_child
->GetNumberOfRows();
329 for (i
= 0; i
< n
; i
++)
334 static void Dump( wxDataViewListModel
*model
, unsigned int col
)
336 unsigned int n
= model
->GetNumberOfRows();
338 for (i
= 0; i
< n
; i
++)
341 model
->GetValue( variant
, col
, i
);
343 tmp
= variant
.GetString();
344 wxPrintf( wxT("%d: %s\n"), (int) i
, tmp
.c_str() );
349 bool wxDataViewSortedListModel::ChildRowAppended()
351 // no need to fix up array
353 unsigned int len
= m_array
.GetCount();
355 unsigned int pos
= m_array
.Add( len
);
358 return wxDataViewListModel::RowPrepended();
361 return wxDataViewListModel::RowAppended();
363 return wxDataViewListModel::RowInserted( pos
);
366 bool wxDataViewSortedListModel::ChildRowPrepended()
370 unsigned int len
= m_array
.GetCount();
371 for (i
= 0; i
< len
; i
++)
373 unsigned int value
= m_array
[i
];
374 m_array
[i
] = value
+1;
377 unsigned int pos
= m_array
.Add( 0 );
380 return wxDataViewListModel::RowPrepended();
383 return wxDataViewListModel::RowAppended();
385 return wxDataViewListModel::RowInserted( pos
);
388 bool wxDataViewSortedListModel::ChildRowInserted( unsigned int before
)
392 unsigned int len
= m_array
.GetCount();
393 for (i
= 0; i
< len
; i
++)
395 unsigned int value
= m_array
[i
];
397 m_array
[i
] = value
+1;
400 unsigned int pos
= m_array
.Add( before
);
403 return wxDataViewListModel::RowPrepended();
406 return wxDataViewListModel::RowAppended();
408 return wxDataViewListModel::RowInserted( pos
);
411 bool wxDataViewSortedListModel::ChildRowDeleted( unsigned int row
)
414 unsigned int len
= m_array
.GetCount();
416 for (i
= 0; i
< len
; i
++)
418 unsigned int value
= m_array
[i
];
428 m_array
[i
] = value
-1;
433 return false; // we should probably assert
436 m_array
.RemoveAt( (unsigned int) pos
);
438 return wxDataViewListModel::RowDeleted( (unsigned int) pos
);
441 bool wxDataViewSortedListModel::ChildRowChanged( unsigned int row
)
444 unsigned int len
= m_array
.GetCount();
446 // Remove and readd sorted. Find out at which
447 // position it was and where it ended.
448 unsigned int start_pos
= 0,end_pos
= 0;
449 for (i
= 0; i
< len
; i
++)
450 if (m_array
[i
] == row
)
455 m_array
.RemoveAt( start_pos
);
458 for (i
= 0; i
< len
; i
++)
459 if (m_array
[i
] == row
)
465 if (end_pos
== start_pos
)
466 return wxDataViewListModel::RowChanged( start_pos
);
468 // Create an array where order[old] -> new_pos, so that
469 // if nothing changed order[0] -> 0 etc.
470 unsigned int *order
= new unsigned int[ len
];
471 // Fill up initial values.
472 for (i
= 0; i
< len
; i
++)
475 if (start_pos
< end_pos
)
477 for (i
= start_pos
; i
< end_pos
; i
++)
478 order
[i
] = order
[i
+1];
479 order
[end_pos
] = start_pos
;
483 for (i
= end_pos
; i
> start_pos
; i
--)
484 order
[i
] = order
[i
-1];
485 order
[start_pos
] = end_pos
;
488 wxDataViewListModel::RowsReordered( order
);
495 bool wxDataViewSortedListModel::ChildValueChanged( unsigned int col
, unsigned int row
)
498 unsigned int len
= m_array
.GetCount();
500 // Remove and readd sorted. Find out at which
501 // position it was and where it ended.
502 unsigned int start_pos
= 0,end_pos
= 0;
503 for (i
= 0; i
< len
; i
++)
504 if (m_array
[i
] == row
)
509 m_array
.RemoveAt( start_pos
);
512 for (i
= 0; i
< len
; i
++)
513 if (m_array
[i
] == row
)
519 if (end_pos
== start_pos
)
520 return wxDataViewListModel::ValueChanged( col
, start_pos
);
522 // Create an array where order[old] -> new_pos, so that
523 // if nothing changed order[0] -> 0 etc.
524 unsigned int *order
= new unsigned int[ len
];
525 // Fill up initial values.
526 for (i
= 0; i
< len
; i
++)
529 if (start_pos
< end_pos
)
531 for (i
= start_pos
; i
< end_pos
; i
++)
532 order
[i
] = order
[i
+1];
533 order
[end_pos
] = start_pos
;
537 for (i
= end_pos
; i
> start_pos
; i
--)
538 order
[i
] = order
[i
-1];
539 order
[start_pos
] = end_pos
;
542 wxDataViewListModel::RowsReordered( order
);
549 bool wxDataViewSortedListModel::ChildRowsReordered( unsigned int *WXUNUSED(new_order
) )
551 // Nothing needs to be done. If the sort criteria
552 // of this list don't change, the order of the
553 // items of the child list isn't relevant.
557 bool wxDataViewSortedListModel::ChildCleared()
559 return wxDataViewListModel::Cleared();
562 unsigned int wxDataViewSortedListModel::GetNumberOfRows()
564 return m_array
.GetCount();
567 unsigned int wxDataViewSortedListModel::GetNumberOfCols()
569 return m_child
->GetNumberOfCols();
572 wxString
wxDataViewSortedListModel::GetColType( unsigned int col
)
574 return m_child
->GetColType( col
);
577 void wxDataViewSortedListModel::GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
579 unsigned int child_row
= m_array
[row
];
580 m_child
->GetValue( variant
, col
, child_row
);
583 bool wxDataViewSortedListModel::SetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
585 unsigned int child_row
= m_array
[row
];
586 bool ret
= m_child
->SetValue( variant
, col
, child_row
);
588 // Do nothing here as the change in the
589 // child model will be reported back.
594 bool wxDataViewSortedListModel::RowAppended()
596 // you can only append
597 bool ret
= m_child
->RowAppended();
599 // Do nothing here as the change in the
600 // child model will be reported back.
605 bool wxDataViewSortedListModel::RowPrepended()
607 // you can only append
608 bool ret
= m_child
->RowAppended();
610 // Do nothing here as the change in the
611 // child model will be reported back.
616 bool wxDataViewSortedListModel::RowInserted( unsigned int WXUNUSED(before
) )
618 // you can only append
619 bool ret
= m_child
->RowAppended();
621 // Do nothing here as the change in the
622 // child model will be reported back.
627 bool wxDataViewSortedListModel::RowDeleted( unsigned int row
)
629 unsigned int child_row
= m_array
[row
];
631 bool ret
= m_child
->RowDeleted( child_row
);
633 // Do nothing here as the change in the
634 // child model will be reported back.
639 bool wxDataViewSortedListModel::RowChanged( unsigned int row
)
641 unsigned int child_row
= m_array
[row
];
642 bool ret
= m_child
->RowChanged( child_row
);
644 // Do nothing here as the change in the
645 // child model will be reported back.
650 bool wxDataViewSortedListModel::ValueChanged( unsigned int col
, unsigned int row
)
652 unsigned int child_row
= m_array
[row
];
653 bool ret
= m_child
->ValueChanged( col
, child_row
);
655 // Do nothing here as the change in the
656 // child model will be reported back.
661 bool wxDataViewSortedListModel::RowsReordered( unsigned int *WXUNUSED(new_order
) )
663 // We sort them ourselves.
668 bool wxDataViewSortedListModel::Cleared()
670 bool ret
= m_child
->Cleared();
672 // Do nothing here as the change in the
673 // child model will be reported back.
678 // ---------------------------------------------------------
679 // wxDataViewRendererBase
680 // ---------------------------------------------------------
682 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
684 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
, wxDataViewCellMode mode
)
686 m_variantType
= varianttype
;
690 // ---------------------------------------------------------
691 // wxDataViewColumnBase
692 // ---------------------------------------------------------
694 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
696 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& title
,
697 wxDataViewRenderer
*renderer
,
698 unsigned int model_column
,
702 m_renderer
= renderer
;
703 m_model_column
= model_column
;
707 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
710 wxDataViewColumnBase::~wxDataViewColumnBase()
717 GetOwner()->GetModel()->RemoveViewingColumn( (wxDataViewColumn
*) this );
721 void wxDataViewColumnBase::SetTitle( const wxString
&title
)
726 wxString
wxDataViewColumnBase::GetTitle()
731 // ---------------------------------------------------------
732 // wxDataViewCtrlBase
733 // ---------------------------------------------------------
735 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
737 wxDataViewCtrlBase::wxDataViewCtrlBase()
740 m_cols
.DeleteContents( true );
743 wxDataViewCtrlBase::~wxDataViewCtrlBase()
747 bool wxDataViewCtrlBase::AssociateModel( wxDataViewListModel
*model
)
754 wxDataViewListModel
* wxDataViewCtrlBase::GetModel()
759 bool wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
)
761 return AppendColumn( new wxDataViewColumn( label
, new wxDataViewTextRenderer(), model_column
) );
764 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
)
766 return AppendColumn( new wxDataViewColumn( label
, new wxDataViewToggleRenderer(), model_column
, 30 ) );
769 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
)
771 return AppendColumn( new wxDataViewColumn( label
, new wxDataViewProgressRenderer(), model_column
, 70 ) );
774 bool wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
)
776 return AppendColumn( new wxDataViewColumn( label
, new wxDataViewDateRenderer(), model_column
) );
779 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
781 m_cols
.Append( (wxObject
*) col
);
782 col
->SetOwner( (wxDataViewCtrl
*) this );
783 m_model
->AddViewingColumn( col
, col
->GetModelColumn() );
787 unsigned int wxDataViewCtrlBase::GetNumberOfColumns()
789 return m_cols
.GetCount();
792 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos
) )
797 bool wxDataViewCtrlBase::ClearColumns()
802 wxDataViewColumn
* wxDataViewCtrlBase::GetColumn( unsigned int pos
)
804 return (wxDataViewColumn
*) m_cols
[ pos
];
807 // ---------------------------------------------------------
809 // ---------------------------------------------------------
811 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
813 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_SELECTED
)
814 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ROW_ACTIVATED
)