diff --git a/include/wx/generic/grideditors.h b/include/wx/generic/grideditors.h index afa5b2d9c8..3b4d47b72e 100644 --- a/include/wx/generic/grideditors.h +++ b/include/wx/generic/grideditors.h @@ -275,6 +275,15 @@ protected: wxCheckBox *CBox() const { return (wxCheckBox *)m_control; } private: + // These functions modify or use m_value. + void SetValueFromGrid(int row, int col, wxGrid* grid); + void SetGridFromValue(int row, int col, wxGrid* grid) const; + + wxString GetStringValue() const { return GetStringValue(m_value); } + + static + wxString GetStringValue(bool value) { return ms_stringValues[value]; } + bool m_value; static wxString ms_stringValues[2]; diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 1bc48c46e2..288b080b2a 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -1312,27 +1312,7 @@ void wxGridCellBoolEditor::BeginEdit(int row, int col, wxGrid* grid) wxASSERT_MSG(m_control, wxT("The wxGridCellEditor must be created first!")); - if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) - { - m_value = grid->GetTable()->GetValueAsBool(row, col); - } - else - { - wxString cellval( grid->GetTable()->GetValue(row, col) ); - - if ( cellval == ms_stringValues[false] ) - m_value = false; - else if ( cellval == ms_stringValues[true] ) - m_value = true; - else - { - // do not try to be smart here and convert it to true or false - // because we'll still overwrite it with something different and - // this risks to be very surprising for the user code, let them - // know about it - wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") ); - } - } + SetValueFromGrid(row, col, grid); CBox()->SetValue(m_value); CBox()->SetFocus(); @@ -1358,11 +1338,7 @@ bool wxGridCellBoolEditor::EndEdit(int WXUNUSED(row), void wxGridCellBoolEditor::ApplyEdit(int row, int col, wxGrid* grid) { - wxGridTableBase * const table = grid->GetTable(); - if ( table->CanSetValueAs(row, col, wxGRID_VALUE_BOOL) ) - table->SetValueAsBool(row, col, m_value); - else - table->SetValue(row, col, GetValue()); + SetGridFromValue(row, col, grid); } void wxGridCellBoolEditor::Reset() @@ -1416,7 +1392,7 @@ void wxGridCellBoolEditor::StartingKey(wxKeyEvent& event) wxString wxGridCellBoolEditor::GetValue() const { - return ms_stringValues[CBox()->GetValue()]; + return GetStringValue(CBox()->GetValue()); } /* static */ void @@ -1433,6 +1409,40 @@ wxGridCellBoolEditor::IsTrueValue(const wxString& value) return value == ms_stringValues[true]; } +void wxGridCellBoolEditor::SetValueFromGrid(int row, int col, wxGrid* grid) +{ + if (grid->GetTable()->CanGetValueAs(row, col, wxGRID_VALUE_BOOL)) + { + m_value = grid->GetTable()->GetValueAsBool(row, col); + } + else + { + wxString cellval( grid->GetTable()->GetValue(row, col) ); + + if ( cellval == ms_stringValues[false] ) + m_value = false; + else if ( cellval == ms_stringValues[true] ) + m_value = true; + else + { + // do not try to be smart here and convert it to true or false + // because we'll still overwrite it with something different and + // this risks to be very surprising for the user code, let them + // know about it + wxFAIL_MSG( wxT("invalid value for a cell with bool editor!") ); + } + } +} + +void wxGridCellBoolEditor::SetGridFromValue(int row, int col, wxGrid* grid) const +{ + wxGridTableBase * const table = grid->GetTable(); + if ( table->CanSetValueAs(row, col, wxGRID_VALUE_BOOL) ) + table->SetValueAsBool(row, col, m_value); + else + table->SetValue(row, col, GetStringValue()); +} + #endif // wxUSE_CHECKBOX #if wxUSE_COMBOBOX