diff --git a/include/wx/dataview.h b/include/wx/dataview.h index ef5cc45dbd..f02ad00a19 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -186,9 +186,20 @@ public: virtual void GetValue( wxVariant &variant, const wxDataViewItem &item, unsigned int col ) const = 0; - // set value, call ValueChanged() afterwards! - virtual bool SetValue( const wxVariant &variant, - const wxDataViewItem &item, unsigned int col ) = 0; + // usually ValueChanged() should be called after changing the value in the + // model to update the control, ChangeValue() does it on its own while + // SetValue() does not -- so while you will override SetValue(), you should + // be usually calling ChangeValue() + virtual bool SetValue(const wxVariant &variant, + const wxDataViewItem &item, + unsigned int col) = 0; + + bool ChangeValue(const wxVariant& variant, + const wxDataViewItem& item, + unsigned int col) + { + return SetValue(variant, item, col) && ValueChanged(item, col); + } // Get text attribute, return false of default attributes should be used virtual bool GetAttr( const wxDataViewItem &WXUNUSED(item), unsigned int WXUNUSED(col), wxDataViewItemAttr &WXUNUSED(attr) ) diff --git a/interface/wx/dataview.h b/interface/wx/dataview.h index a76c28ae67..debcecdb43 100644 --- a/interface/wx/dataview.h +++ b/interface/wx/dataview.h @@ -23,7 +23,7 @@ Since you will usually also allow the wxDataViewCtrl to change your data through its graphical interface, you will also have to override wxDataViewModel::SetValue which the wxDataViewCtrl will call when a change - to some data has been commited. + to some data has been committed. wxDataViewModel (as indeed the entire wxDataViewCtrl code) is using wxVariant to store data and its type in a generic way. wxVariant can be extended to contain @@ -91,6 +91,28 @@ public: */ void AddNotifier(wxDataViewModelNotifier* notifier); + /** + Change the value of the given item and update the control to reflect + it. + + This function simply calls SetValue() and, if it succeeded, + ValueChanged(). + + @since 2.9.1 + + @param variable + The new value. + @param item + The item (row) to update. + @param col + The column to update. + @return + @true if both SetValue() and ValueChanged() returned @true. + */ + bool ChangeValue(const wxVariant& variant, + const wxDataViewItem& item, + unsigned int col); + /** Called to inform the model that all data has been cleared. The control will reread the data from the model again. @@ -229,12 +251,17 @@ public: /** This gets called in order to set a value in the data model. + The most common scenario is that the wxDataViewCtrl calls this method after the user changed some data in the view. - Afterwards ValueChanged() has to be called! + This is the function you need to override in your derived class but if + you want to call it, ChangeValue() is usually more convenient as + otherwise you need to manually call ValueChanged() to update the + control itself. */ - virtual bool SetValue(const wxVariant& variant, const wxDataViewItem& item, + virtual bool SetValue(const wxVariant& variant, + const wxDataViewItem& item, unsigned int col) = 0; /** diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index fd3ccde0b8..e37e6d38c7 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -751,8 +751,7 @@ bool wxDataViewRendererBase::FinishEditing() return false; unsigned int col = GetOwner()->GetModelColumn(); - dv_ctrl->GetModel()->SetValue( value, m_item, col ); - dv_ctrl->GetModel()->ValueChanged( m_item, col ); + dv_ctrl->GetModel()->ChangeValue(value, m_item, col); // Now we should send Editing Done event wxDataViewEvent event( wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() ); diff --git a/src/generic/datavgen.cpp b/src/generic/datavgen.cpp index 1eeff556ad..74aba29bd9 100644 --- a/src/generic/datavgen.cpp +++ b/src/generic/datavgen.cpp @@ -933,10 +933,7 @@ bool wxDataViewToggleRenderer::Activate( wxRect WXUNUSED(cell), wxDataViewModel *model, const wxDataViewItem & item, unsigned int col) { - bool value = !m_toggle; - wxVariant variant = value; - model->SetValue( variant, item, col); - model->ValueChanged( item, col ); + model->ChangeValue(!m_toggle, item, col); return true; } @@ -1051,10 +1048,7 @@ END_EVENT_TABLE() void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) { - wxDateTime date = event.GetDate(); - wxVariant value = date; - m_model->SetValue( value, m_item, m_col ); - m_model->ValueChanged( m_item, m_col ); + m_model->ChangeValue( event.GetDate(), m_item, m_col ); DismissAndNotify(); } diff --git a/src/gtk/dataview.cpp b/src/gtk/dataview.cpp index 3f8eb008c1..62fbd52fe2 100644 --- a/src/gtk/dataview.cpp +++ b/src/gtk/dataview.cpp @@ -1742,8 +1742,7 @@ wxDataViewRenderer::GtkOnCellChanged(const wxVariant& value, unsigned col) { wxDataViewModel *model = GetOwner()->GetOwner()->GetModel(); - model->SetValue( value, item, col ); - model->ValueChanged( item, col ); + model->ChangeValue( value, item, col ); } // --------------------------------------------------------- @@ -1933,8 +1932,7 @@ static void wxGtkToggleRendererToggledCallback( GtkCellRendererToggle *renderer, unsigned int model_col = cell->GetOwner()->GetModelColumn(); - model->SetValue( value, item, model_col ); - model->ValueChanged( item, model_col ); + model->ChangeValue( value, item, model_col ); } IMPLEMENT_CLASS(wxDataViewToggleRenderer, wxDataViewRenderer) @@ -2371,10 +2369,7 @@ END_EVENT_TABLE() void wxDataViewDateRendererPopupTransient::OnCalendar( wxCalendarEvent &event ) { - wxDateTime date = event.GetDate(); - wxVariant value = date; - m_model->SetValue( value, m_item, m_col ); - m_model->ValueChanged( m_item, m_col ); + m_model->ChangeValue( event.GetDate(), m_item, m_col ); DismissAndNotify(); } diff --git a/src/osx/carbon/dataview.cpp b/src/osx/carbon/dataview.cpp index 38f94678c9..30d3d20ef1 100644 --- a/src/osx/carbon/dataview.cpp +++ b/src/osx/carbon/dataview.cpp @@ -1318,8 +1318,7 @@ OSStatus wxMacDataViewDataBrowserListViewControl::DataBrowserGetSetItemDataProc( // variable definition and initialization: wxVariant modifiedData(true); - if (dataViewCtrlPtr->GetModel()->SetValue(modifiedData, dvItem, col) && - dataViewCtrlPtr->GetModel()->ValueChanged(dvItem, col)) + if (dataViewCtrlPtr->GetModel()->ChangeValue(modifiedData, dvItem, col)) return noErr; else return errDataBrowserInvalidPropertyData; @@ -1329,8 +1328,7 @@ OSStatus wxMacDataViewDataBrowserListViewControl::DataBrowserGetSetItemDataProc( // variable definition and initialization: wxVariant modifiedData(false); - if (dataViewCtrlPtr->GetModel()->SetValue(modifiedData, dvItem, col) && - dataViewCtrlPtr->GetModel()->ValueChanged(dvItem, col)) + if (dataViewCtrlPtr->GetModel()->ChangeValue(modifiedData, dvItem, col)) return noErr; else return errDataBrowserInvalidPropertyData; @@ -1357,8 +1355,7 @@ OSStatus wxMacDataViewDataBrowserListViewControl::DataBrowserGetSetItemDataProc( #endif wxVariant modifiedData(modifiedString.AsString()); - if (dataViewCtrlPtr->GetModel()->SetValue(modifiedData, dvItem, col) && - dataViewCtrlPtr->GetModel()->ValueChanged(dvItem, col)) + if (dataViewCtrlPtr->GetModel()->ChangeValue(modifiedData, dvItem, col)) return noErr; else return errDataBrowserInvalidPropertyData; diff --git a/src/osx/cocoa/dataview.mm b/src/osx/cocoa/dataview.mm index 243ac2515c..1d63bdf4fa 100644 --- a/src/osx/cocoa/dataview.mm +++ b/src/osx/cocoa/dataview.mm @@ -2236,8 +2236,7 @@ void wxDataViewRenderer::OSXOnCellChanged(const wxVariant& value, unsigned col) { wxDataViewModel *model = GetOwner()->GetOwner()->GetModel(); - model->SetValue(value, item, col); - model->ValueChanged(item, col); + model->ChangeValue(value, item, col); } IMPLEMENT_ABSTRACT_CLASS(wxDataViewRenderer,wxDataViewRendererBase)