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 bool operator == (const wxDataViewItem
&left
, const wxDataViewItem
&right
)
31 return (left
.GetID() == right
.GetID() );
35 // ---------------------------------------------------------
37 // ---------------------------------------------------------
39 #include <wx/listimpl.cpp>
40 WX_DEFINE_LIST(wxDataViewModelNotifiers
);
42 wxDataViewModel::wxDataViewModel()
44 m_notifiers
.DeleteContents( true );
47 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
51 wxDataViewModelNotifiers::iterator iter
;
52 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
54 wxDataViewModelNotifier
* notifier
= *iter
;
55 if (!notifier
->ItemAdded( parent
, item
))
62 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
66 wxDataViewModelNotifiers::iterator iter
;
67 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
69 wxDataViewModelNotifier
* notifier
= *iter
;
70 if (!notifier
->ItemDeleted( parent
, item
))
77 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
81 wxDataViewModelNotifiers::iterator iter
;
82 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
84 wxDataViewModelNotifier
* notifier
= *iter
;
85 if (!notifier
->ItemChanged( item
))
92 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
96 wxDataViewModelNotifiers::iterator iter
;
97 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
99 wxDataViewModelNotifier
* notifier
= *iter
;
100 if (!notifier
->ValueChanged( item
, col
))
107 bool wxDataViewModel::Cleared()
111 wxDataViewModelNotifiers::iterator iter
;
112 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
114 wxDataViewModelNotifier
* notifier
= *iter
;
115 if (!notifier
->Cleared())
122 void wxDataViewModel::Resort()
124 wxDataViewModelNotifiers::iterator iter
;
125 for (iter
= m_notifiers
.begin(); iter
!= m_notifiers
.end(); ++iter
)
127 wxDataViewModelNotifier
* notifier
= *iter
;
132 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
134 m_notifiers
.push_back( notifier
);
135 notifier
->SetOwner( this );
138 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
140 m_notifiers
.DeleteObject( notifier
);
143 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
144 unsigned int column
, bool ascending
)
146 // sort branches before leaves
147 bool item1_is_container
= IsContainer(item1
);
148 bool item2_is_container
= IsContainer(item2
);
150 if (item1_is_container
&& !item2_is_container
)
152 if (item2_is_container
&& !item1_is_container
)
155 wxVariant value1
,value2
;
156 GetValue( value1
, item1
, column
);
157 GetValue( value2
, item2
, column
);
161 wxVariant temp
= value1
;
166 if (value1
.GetType() == wxT("string"))
168 wxString str1
= value1
.GetString();
169 wxString str2
= value2
.GetString();
170 int res
= str1
.Cmp( str2
);
173 if (value1
.GetType() == wxT("long"))
175 long l1
= value1
.GetLong();
176 long l2
= value2
.GetLong();
180 if (value1
.GetType() == wxT("double"))
182 double d1
= value1
.GetDouble();
183 double d2
= value2
.GetDouble();
184 if (d1
< d2
) return 1;
185 if (d1
> d2
) return -1;
187 if (value1
.GetType() == wxT("datetime"))
189 wxDateTime dt1
= value1
.GetDateTime();
190 wxDateTime dt2
= value2
.GetDateTime();
191 if (dt1
.IsEarlierThan(dt2
)) return 1;
192 if (dt2
.IsEarlierThan(dt1
)) return -11;
195 // items must be different
196 unsigned long litem1
= (unsigned long) item1
.GetID();
197 unsigned long litem2
= (unsigned long) item2
.GetID();
200 return litem2
-litem2
;
202 return litem1
-litem2
;
205 // ---------------------------------------------------------
206 // wxDataViewIndexListModel
207 // ---------------------------------------------------------
209 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
211 // build initial index
213 for (i
= 1; i
< initial_size
+1; i
++)
214 m_hash
.Add( (void*) i
);
215 m_lastIndex
= initial_size
+ 1;
218 wxDataViewIndexListModel::~wxDataViewIndexListModel()
222 void wxDataViewIndexListModel::RowPrepended()
224 unsigned int id
= m_lastIndex
++;
225 m_hash
.Insert( (void*) id
, 0 );
226 wxDataViewItem
item( (void*) id
);
227 ItemAdded( wxDataViewItem(0), item
);
230 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
232 unsigned int id
= m_lastIndex
++;
233 m_hash
.Insert( (void*) id
, before
);
234 wxDataViewItem
item( (void*) id
);
235 ItemAdded( wxDataViewItem(0), item
);
238 void wxDataViewIndexListModel::RowAppended()
240 unsigned int id
= m_lastIndex
++;
241 m_hash
.Add( (void*) id
);
242 wxDataViewItem
item( (void*) id
);
243 ItemAdded( wxDataViewItem(0), item
);
246 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
248 wxDataViewItem
item( m_hash
[row
] );
249 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
250 m_hash
.RemoveAt( row
);
253 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
255 wxDataViewModel::ItemChanged( GetItem(row
) );
258 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
260 wxDataViewModel::ValueChanged( GetItem(row
), col
);
263 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
265 // assert for not found
266 return (unsigned int) m_hash
.Index( item
.GetID() );
269 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
271 return wxDataViewItem( m_hash
[row
] );
274 int wxDataViewIndexListModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
,
275 unsigned int column
, bool ascending
)
278 return GetRow(item1
) - GetRow(item2
);
280 return GetRow(item2
) - GetRow(item1
);
283 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
284 const wxDataViewItem
&item
, unsigned int col
) const
286 GetValue( variant
, GetRow(item
), col
);
289 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
290 const wxDataViewItem
&item
, unsigned int col
)
292 return SetValue( variant
, GetRow(item
), col
);
295 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
& WXUNUSED(item
) ) const
297 return wxDataViewItem(0);
300 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
302 // only the invisible root item has children
309 wxDataViewItem
wxDataViewIndexListModel::GetFirstChild( const wxDataViewItem
&parent
) const
313 if (m_hash
.GetCount() == 0)
314 return wxDataViewItem(0);
316 return wxDataViewItem( m_hash
[0]);
319 return wxDataViewItem(0);
322 wxDataViewItem
wxDataViewIndexListModel::GetNextSibling( const wxDataViewItem
&item
) const
325 return wxDataViewItem(0);
327 int pos
= m_hash
.Index( item
.GetID() );
328 if ((pos
== wxNOT_FOUND
) || (pos
== (int) (m_hash
.GetCount()-1)))
329 return wxDataViewItem(0);
331 return wxDataViewItem( m_hash
[pos
+1] );
334 // ---------------------------------------------------------
335 // wxDataViewRendererBase
336 // ---------------------------------------------------------
338 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
340 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
341 wxDataViewCellMode
WXUNUSED(mode
),
342 int WXUNUSED(align
) )
344 m_variantType
= varianttype
;
348 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
350 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
353 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
355 m_item
= item
; // remember for later
357 unsigned int col
= GetOwner()->GetModelColumn();
359 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
361 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
363 wxDataViewEditorCtrlEvtHandler
*handler
=
364 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
366 m_editorCtrl
->PushEventHandler( handler
);
368 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
369 handler
->SetFocusOnIdle();
371 m_editorCtrl
->SetFocus();
377 void wxDataViewRendererBase::CancelEditing()
379 wxPendingDelete
.Append( m_editorCtrl
);
381 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
383 // m_editorCtrl->PopEventHandler( true );
386 bool wxDataViewRendererBase::FinishEditing()
389 GetValueFromEditorCtrl( m_editorCtrl
, value
);
391 wxPendingDelete
.Append( m_editorCtrl
);
393 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
395 if (!Validate(value
))
398 unsigned int col
= GetOwner()->GetModelColumn();
399 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
400 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
402 // m_editorCtrl->PopEventHandler( true );
407 //-----------------------------------------------------------------------------
408 // wxDataViewEditorCtrlEvtHandler
409 //-----------------------------------------------------------------------------
411 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
412 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
413 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
414 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
417 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
418 wxControl
*editorCtrl
,
419 wxDataViewRenderer
*owner
)
422 m_editorCtrl
= editorCtrl
;
427 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
431 m_focusOnIdle
= false;
432 if (wxWindow::FindFocus() != m_editorCtrl
)
433 m_editorCtrl
->SetFocus();
439 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
441 switch ( event
.m_keyCode
)
445 m_owner
->FinishEditing();
450 m_owner
->CancelEditing();
458 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
463 m_owner
->FinishEditing();
469 // ---------------------------------------------------------
470 // wxDataViewColumnBase
471 // ---------------------------------------------------------
473 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
475 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
476 wxDataViewRenderer
*renderer
,
477 unsigned int model_column
,
479 wxAlignment
WXUNUSED(align
),
482 m_renderer
= renderer
;
483 m_model_column
= model_column
;
485 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
487 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
488 // parameters inside the native control!
491 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
492 wxDataViewRenderer
*renderer
,
493 unsigned int model_column
,
495 wxAlignment
WXUNUSED(align
),
496 int WXUNUSED(flags
) )
498 m_renderer
= renderer
;
499 m_model_column
= model_column
;
502 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
505 wxDataViewColumnBase::~wxDataViewColumnBase()
511 int wxDataViewColumnBase::GetFlags() const
516 ret
|= wxDATAVIEW_COL_SORTABLE
;
518 ret
|= wxDATAVIEW_COL_RESIZABLE
;
520 ret
|= wxDATAVIEW_COL_HIDDEN
;
525 void wxDataViewColumnBase::SetFlags(int flags
)
527 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
528 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
529 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
532 // ---------------------------------------------------------
533 // wxDataViewCtrlBase
534 // ---------------------------------------------------------
536 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
538 wxDataViewCtrlBase::wxDataViewCtrlBase()
541 m_cols
.DeleteContents( true );
542 m_expander_column
= 0;
546 wxDataViewCtrlBase::~wxDataViewCtrlBase()
548 // IMPORTANT: before calling DecRef() on our model (since it may
549 // result in a free() call), erase all columns (since
550 // they hold a pointer to our model)
560 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
564 m_model
->DecRef(); // discard old model, if any
567 // add our own reference to the new model:
577 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
582 bool wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
583 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
585 return AppendColumn( new wxDataViewColumn( label
,
586 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
587 model_column
, width
, align
, flags
) );
590 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
591 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
593 return AppendColumn( new wxDataViewColumn( label
,
594 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
595 model_column
, width
, align
, flags
) );
598 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
599 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
601 return AppendColumn( new wxDataViewColumn( label
,
602 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
603 model_column
, width
, align
, flags
) );
606 bool wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
607 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
609 return AppendColumn( new wxDataViewColumn( label
,
610 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
611 model_column
, width
, align
, flags
) );
614 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
615 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
617 return AppendColumn( new wxDataViewColumn( label
,
618 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
619 model_column
, width
, align
, flags
) );
622 bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
623 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
625 return AppendColumn( new wxDataViewColumn( label
,
626 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
627 model_column
, width
, align
, flags
) );
630 bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
631 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
633 return AppendColumn( new wxDataViewColumn( label
,
634 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
635 model_column
, width
, align
, flags
) );
638 bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
639 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
641 return AppendColumn( new wxDataViewColumn( label
,
642 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
643 model_column
, width
, align
, flags
) );
646 bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
647 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
649 return AppendColumn( new wxDataViewColumn( label
,
650 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
651 model_column
, width
, align
, flags
) );
654 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
655 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
657 return AppendColumn( new wxDataViewColumn( label
,
658 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
659 model_column
, width
, align
, flags
) );
662 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
664 m_cols
.Append( (wxObject
*) col
);
665 col
->SetOwner( (wxDataViewCtrl
*) this );
669 unsigned int wxDataViewCtrlBase::GetColumnCount() const
671 return m_cols
.GetCount();
674 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos
) )
679 bool wxDataViewCtrlBase::ClearColumns()
684 wxDataViewColumn
* wxDataViewCtrlBase::GetColumn( unsigned int pos
)
686 if( pos
>= m_cols
.GetCount() )
689 return (wxDataViewColumn
*) m_cols
[ pos
];
692 // ---------------------------------------------------------
694 // ---------------------------------------------------------
696 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
698 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
)
699 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_DESELECTED
)
700 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
701 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING
)
702 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED
)
703 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING
)
704 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED
)
705 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
706 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
707 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
709 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
)
710 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
)
711 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
)
712 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
)
713 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
)