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 wxDataViewModel::wxDataViewModel()
41 m_notifiers
.DeleteContents( true );
46 bool wxDataViewModel::ItemAdded( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
50 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
53 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
54 if (!notifier
->ItemAdded( parent
, item
))
56 node
= node
->GetNext();
62 bool wxDataViewModel::ItemDeleted( const wxDataViewItem
&parent
, const wxDataViewItem
&item
)
66 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
69 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
70 if (!notifier
->ItemDeleted( parent
, item
))
72 node
= node
->GetNext();
78 bool wxDataViewModel::ItemChanged( const wxDataViewItem
&item
)
82 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
85 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
86 if (!notifier
->ItemChanged( item
))
88 node
= node
->GetNext();
94 bool wxDataViewModel::ValueChanged( const wxDataViewItem
&item
, unsigned int col
)
98 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
101 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
102 if (!notifier
->ValueChanged( item
, col
))
104 node
= node
->GetNext();
110 bool wxDataViewModel::Cleared()
114 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
117 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
118 if (!notifier
->Cleared())
120 node
= node
->GetNext();
126 void wxDataViewModel::Resort()
128 wxList::compatibility_iterator node
= m_notifiers
.GetFirst();
131 wxDataViewModelNotifier
* notifier
= (wxDataViewModelNotifier
*) node
->GetData();
133 node
= node
->GetNext();
137 void wxDataViewModel::AddNotifier( wxDataViewModelNotifier
*notifier
)
139 m_notifiers
.Append( notifier
);
140 notifier
->SetOwner( this );
143 void wxDataViewModel::RemoveNotifier( wxDataViewModelNotifier
*notifier
)
145 m_notifiers
.DeleteObject( notifier
);
148 int wxDataViewModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
)
150 // sort branches before leaves
151 bool item1_is_container
= IsContainer(item1
);
152 bool item2_is_container
= IsContainer(item2
);
154 if (item1_is_container
&& !item2_is_container
)
156 if (item2_is_container
&& !item1_is_container
)
159 wxVariant value1
,value2
;
160 GetValue( value1
, item1
, m_sortingColumn
);
161 GetValue( value2
, item2
, m_sortingColumn
);
165 wxVariant temp
= value1
;
170 if (value1
.GetType() == wxT("string"))
172 wxString str1
= value1
.GetString();
173 wxString str2
= value2
.GetString();
174 int res
= str1
.Cmp( str2
);
177 if (value1
.GetType() == wxT("long"))
179 long l1
= value1
.GetLong();
180 long l2
= value2
.GetLong();
184 if (value1
.GetType() == wxT("double"))
186 double d1
= value1
.GetDouble();
187 double d2
= value2
.GetDouble();
188 if (d1
< d2
) return 1;
189 if (d1
> d2
) return -1;
191 if (value1
.GetType() == wxT("datetime"))
193 wxDateTime dt1
= value1
.GetDateTime();
194 wxDateTime dt2
= value2
.GetDateTime();
195 if (dt1
.IsEarlierThan(dt2
)) return 1;
196 if (dt2
.IsEarlierThan(dt1
)) return -11;
199 // items must be different
200 unsigned long litem1
= (unsigned long) item1
.GetID();
201 unsigned long litem2
= (unsigned long) item2
.GetID();
204 return litem2
-litem1
;
205 return litem1
-litem2
;
208 // ---------------------------------------------------------
209 // wxDataViewIndexListModel
210 // ---------------------------------------------------------
212 wxDataViewIndexListModel::wxDataViewIndexListModel( unsigned int initial_size
)
214 // build initial index
216 for (i
= 1; i
< initial_size
+1; i
++)
217 m_hash
.Add( (void*) i
);
218 m_lastIndex
= initial_size
+ 1;
221 wxDataViewIndexListModel::~wxDataViewIndexListModel()
225 void wxDataViewIndexListModel::RowPrepended()
227 unsigned int id
= m_lastIndex
++;
228 m_hash
.Insert( (void*) id
, 0 );
229 wxDataViewItem
item( (void*) id
);
230 ItemAdded( wxDataViewItem(0), item
);
233 void wxDataViewIndexListModel::RowInserted( unsigned int before
)
235 unsigned int id
= m_lastIndex
++;
236 m_hash
.Insert( (void*) id
, before
);
237 wxDataViewItem
item( (void*) id
);
238 ItemAdded( wxDataViewItem(0), item
);
241 void wxDataViewIndexListModel::RowAppended()
243 unsigned int id
= m_lastIndex
++;
244 m_hash
.Add( (void*) id
);
245 wxDataViewItem
item( (void*) id
);
246 ItemAdded( wxDataViewItem(0), item
);
249 void wxDataViewIndexListModel::RowDeleted( unsigned int row
)
251 wxDataViewItem
item( m_hash
[row
] );
252 m_hash
.RemoveAt( row
);
253 wxDataViewModel::ItemDeleted( wxDataViewItem(0), item
);
256 void wxDataViewIndexListModel::RowChanged( unsigned int row
)
258 wxDataViewModel::ItemChanged( GetItem(row
) );
261 void wxDataViewIndexListModel::RowValueChanged( unsigned int row
, unsigned int col
)
263 wxDataViewModel::ValueChanged( GetItem(row
), col
);
266 unsigned int wxDataViewIndexListModel::GetRow( const wxDataViewItem
&item
) const
268 // assert for not found
269 return (unsigned int) m_hash
.Index( item
.GetID() );
272 wxDataViewItem
wxDataViewIndexListModel::GetItem( unsigned int row
) const
274 return wxDataViewItem( m_hash
[row
] );
277 int wxDataViewIndexListModel::Compare( const wxDataViewItem
&item1
, const wxDataViewItem
&item2
)
279 return GetRow(item1
) - GetRow(item2
);
282 void wxDataViewIndexListModel::GetValue( wxVariant
&variant
,
283 const wxDataViewItem
&item
, unsigned int col
) const
285 return GetValue( variant
, GetRow(item
), col
);
288 bool wxDataViewIndexListModel::SetValue( const wxVariant
&variant
,
289 const wxDataViewItem
&item
, unsigned int col
)
291 return SetValue( variant
, GetRow(item
), col
);
294 wxDataViewItem
wxDataViewIndexListModel::GetParent( const wxDataViewItem
&item
) const
296 return wxDataViewItem(0);
299 bool wxDataViewIndexListModel::IsContainer( const wxDataViewItem
&item
) const
301 // only the invisible root item has children
308 wxDataViewItem
wxDataViewIndexListModel::GetFirstChild( const wxDataViewItem
&parent
) const
312 if (m_hash
.GetCount() == 0)
313 return wxDataViewItem(0);
315 return wxDataViewItem( m_hash
[0]);
318 return wxDataViewItem(0);
321 wxDataViewItem
wxDataViewIndexListModel::GetNextSibling( const wxDataViewItem
&item
) const
324 return wxDataViewItem(0);
326 int pos
= m_hash
.Index( item
.GetID() );
327 if ((pos
== wxNOT_FOUND
) || (pos
== m_hash
.GetCount()-1))
328 return wxDataViewItem(0);
330 return wxDataViewItem( m_hash
[pos
+1] );
333 // ---------------------------------------------------------
334 // wxDataViewRendererBase
335 // ---------------------------------------------------------
337 IMPLEMENT_ABSTRACT_CLASS(wxDataViewRendererBase
, wxObject
)
339 wxDataViewRendererBase::wxDataViewRendererBase( const wxString
&varianttype
,
340 wxDataViewCellMode
WXUNUSED(mode
),
341 int WXUNUSED(align
) )
343 m_variantType
= varianttype
;
347 const wxDataViewCtrl
* wxDataViewRendererBase::GetView() const
349 return wx_const_cast(wxDataViewRendererBase
*, this)->GetOwner()->GetOwner();
352 bool wxDataViewRendererBase::StartEditing( const wxDataViewItem
&item
, wxRect labelRect
)
354 m_item
= item
; // remember for later
356 unsigned int col
= GetOwner()->GetModelColumn();
358 GetOwner()->GetOwner()->GetModel()->GetValue( value
, item
, col
);
360 m_editorCtrl
= CreateEditorCtrl( GetOwner()->GetOwner()->GetMainWindow(), labelRect
, value
);
362 wxDataViewEditorCtrlEvtHandler
*handler
=
363 new wxDataViewEditorCtrlEvtHandler( m_editorCtrl
, (wxDataViewRenderer
*) this );
365 m_editorCtrl
->PushEventHandler( handler
);
367 #if defined(__WXGTK20__) && !defined(wxUSE_GENERICDATAVIEWCTRL)
368 handler
->SetFocusOnIdle();
370 m_editorCtrl
->SetFocus();
376 void wxDataViewRendererBase::CancelEditing()
378 wxPendingDelete
.Append( m_editorCtrl
);
380 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
382 // m_editorCtrl->PopEventHandler( true );
385 bool wxDataViewRendererBase::FinishEditing()
388 GetValueFromEditorCtrl( m_editorCtrl
, value
);
390 wxPendingDelete
.Append( m_editorCtrl
);
392 GetOwner()->GetOwner()->GetMainWindow()->SetFocus();
394 if (!Validate(value
))
397 unsigned int col
= GetOwner()->GetModelColumn();
398 GetOwner()->GetOwner()->GetModel()->SetValue( value
, m_item
, col
);
399 GetOwner()->GetOwner()->GetModel()->ValueChanged( m_item
, col
);
401 // m_editorCtrl->PopEventHandler( true );
406 //-----------------------------------------------------------------------------
407 // wxDataViewEditorCtrlEvtHandler
408 //-----------------------------------------------------------------------------
410 BEGIN_EVENT_TABLE(wxDataViewEditorCtrlEvtHandler
, wxEvtHandler
)
411 EVT_CHAR (wxDataViewEditorCtrlEvtHandler::OnChar
)
412 EVT_KILL_FOCUS (wxDataViewEditorCtrlEvtHandler::OnKillFocus
)
413 EVT_IDLE (wxDataViewEditorCtrlEvtHandler::OnIdle
)
416 wxDataViewEditorCtrlEvtHandler::wxDataViewEditorCtrlEvtHandler(
417 wxControl
*editorCtrl
,
418 wxDataViewRenderer
*owner
)
421 m_editorCtrl
= editorCtrl
;
426 void wxDataViewEditorCtrlEvtHandler::OnIdle( wxIdleEvent
&event
)
430 m_focusOnIdle
= false;
431 if (wxWindow::FindFocus() != m_editorCtrl
)
432 m_editorCtrl
->SetFocus();
438 void wxDataViewEditorCtrlEvtHandler::OnChar( wxKeyEvent
&event
)
440 switch ( event
.m_keyCode
)
444 m_owner
->FinishEditing();
449 m_owner
->CancelEditing();
457 void wxDataViewEditorCtrlEvtHandler::OnKillFocus( wxFocusEvent
&event
)
462 m_owner
->FinishEditing();
468 // ---------------------------------------------------------
469 // wxDataViewColumnBase
470 // ---------------------------------------------------------
472 IMPLEMENT_ABSTRACT_CLASS(wxDataViewColumnBase
, wxObject
)
474 wxDataViewColumnBase::wxDataViewColumnBase(const wxString
& WXUNUSED(title
),
475 wxDataViewRenderer
*renderer
,
476 unsigned int model_column
,
478 wxAlignment
WXUNUSED(align
),
481 m_renderer
= renderer
;
482 m_model_column
= model_column
;
484 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
486 // NOTE: the wxDataViewColumn's ctor must store the width, align, flags
487 // parameters inside the native control!
490 wxDataViewColumnBase::wxDataViewColumnBase(const wxBitmap
& bitmap
,
491 wxDataViewRenderer
*renderer
,
492 unsigned int model_column
,
494 wxAlignment
WXUNUSED(align
),
495 int WXUNUSED(flags
) )
497 m_renderer
= renderer
;
498 m_model_column
= model_column
;
501 m_renderer
->SetOwner( (wxDataViewColumn
*) this );
504 wxDataViewColumnBase::~wxDataViewColumnBase()
510 int wxDataViewColumnBase::GetFlags() const
515 ret
|= wxDATAVIEW_COL_SORTABLE
;
517 ret
|= wxDATAVIEW_COL_RESIZABLE
;
519 ret
|= wxDATAVIEW_COL_HIDDEN
;
524 void wxDataViewColumnBase::SetFlags(int flags
)
526 SetSortable((flags
& wxDATAVIEW_COL_SORTABLE
) != 0);
527 SetResizeable((flags
& wxDATAVIEW_COL_RESIZABLE
) != 0);
528 SetHidden((flags
& wxDATAVIEW_COL_HIDDEN
) != 0);
531 // ---------------------------------------------------------
532 // wxDataViewCtrlBase
533 // ---------------------------------------------------------
535 IMPLEMENT_ABSTRACT_CLASS(wxDataViewCtrlBase
, wxControl
)
537 wxDataViewCtrlBase::wxDataViewCtrlBase()
540 m_cols
.DeleteContents( true );
541 m_expander_column
= 0;
545 wxDataViewCtrlBase::~wxDataViewCtrlBase()
547 // IMPORTANT: before calling DecRef() on our model (since it may
548 // result in a free() call), erase all columns (since
549 // they hold a pointer to our model)
559 bool wxDataViewCtrlBase::AssociateModel( wxDataViewModel
*model
)
563 m_model
->DecRef(); // discard old model, if any
566 // add our own reference to the new model:
576 wxDataViewModel
* wxDataViewCtrlBase::GetModel()
581 bool wxDataViewCtrlBase::AppendTextColumn( const wxString
&label
, unsigned int model_column
,
582 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
584 return AppendColumn( new wxDataViewColumn( label
,
585 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
586 model_column
, width
, align
, flags
) );
589 bool wxDataViewCtrlBase::AppendToggleColumn( const wxString
&label
, unsigned int model_column
,
590 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
592 return AppendColumn( new wxDataViewColumn( label
,
593 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
594 model_column
, width
, align
, flags
) );
597 bool wxDataViewCtrlBase::AppendProgressColumn( const wxString
&label
, unsigned int model_column
,
598 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
600 return AppendColumn( new wxDataViewColumn( label
,
601 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
602 model_column
, width
, align
, flags
) );
605 bool wxDataViewCtrlBase::AppendDateColumn( const wxString
&label
, unsigned int model_column
,
606 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
608 return AppendColumn( new wxDataViewColumn( label
,
609 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
610 model_column
, width
, align
, flags
) );
613 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxString
&label
, unsigned int model_column
,
614 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
616 return AppendColumn( new wxDataViewColumn( label
,
617 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
618 model_column
, width
, align
, flags
) );
621 bool wxDataViewCtrlBase::AppendTextColumn( const wxBitmap
&label
, unsigned int model_column
,
622 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
624 return AppendColumn( new wxDataViewColumn( label
,
625 new wxDataViewTextRenderer( wxT("string"), mode
, (int)align
),
626 model_column
, width
, align
, flags
) );
629 bool wxDataViewCtrlBase::AppendToggleColumn( const wxBitmap
&label
, unsigned int model_column
,
630 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
632 return AppendColumn( new wxDataViewColumn( label
,
633 new wxDataViewToggleRenderer( wxT("bool"), mode
, (int)align
),
634 model_column
, width
, align
, flags
) );
637 bool wxDataViewCtrlBase::AppendProgressColumn( const wxBitmap
&label
, unsigned int model_column
,
638 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
640 return AppendColumn( new wxDataViewColumn( label
,
641 new wxDataViewProgressRenderer( wxEmptyString
, wxT("long"), mode
, (int)align
),
642 model_column
, width
, align
, flags
) );
645 bool wxDataViewCtrlBase::AppendDateColumn( const wxBitmap
&label
, unsigned int model_column
,
646 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
648 return AppendColumn( new wxDataViewColumn( label
,
649 new wxDataViewDateRenderer( wxT("datetime"), mode
, (int)align
),
650 model_column
, width
, align
, flags
) );
653 bool wxDataViewCtrlBase::AppendBitmapColumn( const wxBitmap
&label
, unsigned int model_column
,
654 wxDataViewCellMode mode
, int width
, wxAlignment align
, int flags
)
656 return AppendColumn( new wxDataViewColumn( label
,
657 new wxDataViewBitmapRenderer( wxT("wxBitmap"), mode
, (int)align
),
658 model_column
, width
, align
, flags
) );
661 bool wxDataViewCtrlBase::AppendColumn( wxDataViewColumn
*col
)
663 m_cols
.Append( (wxObject
*) col
);
664 col
->SetOwner( (wxDataViewCtrl
*) this );
668 unsigned int wxDataViewCtrlBase::GetColumnCount() const
670 return m_cols
.GetCount();
673 bool wxDataViewCtrlBase::DeleteColumn( unsigned int WXUNUSED(pos
) )
678 bool wxDataViewCtrlBase::ClearColumns()
683 wxDataViewColumn
* wxDataViewCtrlBase::GetColumn( unsigned int pos
)
685 return (wxDataViewColumn
*) m_cols
[ pos
];
688 // ---------------------------------------------------------
690 // ---------------------------------------------------------
692 IMPLEMENT_DYNAMIC_CLASS(wxDataViewEvent
,wxNotifyEvent
)
694 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED
)
695 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED
)
696 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK
)
697 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK
)
698 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED
)
700 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED
)
701 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_DELETED
)
702 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_CHANGED
)
703 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_VALUE_CHANGED
)
704 DEFINE_EVENT_TYPE(wxEVT_COMMAND_DATAVIEW_MODEL_CLEARED
)