From e438f9a496b7026d531d3398ab6cc1df34b3fa4b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 14 Aug 2021 14:02:16 +0100 Subject: [PATCH] Use wxNumberFormatter in wxGrid number renderer and editor This ensures that the correct, i.e. matching the users default, decimal point separator is used even if setlocale() is not called. Also simplify the code a bit by removing checks for wxUSE_INTL already present in wxNumberFormatter itself. --- src/generic/gridctrl.cpp | 6 +++--- src/generic/grideditors.cpp | 29 ++++++----------------------- 2 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/generic/gridctrl.cpp b/src/generic/gridctrl.cpp index 8ba3582b2c..0732fd4520 100644 --- a/src/generic/gridctrl.cpp +++ b/src/generic/gridctrl.cpp @@ -25,6 +25,7 @@ #include "wx/checkbox.h" #endif // WX_PRECOMP +#include "wx/numformatter.h" #include "wx/tokenzr.h" #include "wx/renderer.h" @@ -839,7 +840,7 @@ wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col else { text = table->GetValue(row, col); - hasDouble = text.ToDouble(&val); + hasDouble = wxNumberFormatter::FromString(text, &val); } if ( hasDouble ) @@ -877,8 +878,7 @@ wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col m_format += wxT('f'); } - text.Printf(m_format, val); - + text = wxNumberFormatter::Format(m_format, val); } //else: text already contains the string diff --git a/src/generic/grideditors.cpp b/src/generic/grideditors.cpp index 6cdce725eb..b4fcbce48b 100644 --- a/src/generic/grideditors.cpp +++ b/src/generic/grideditors.cpp @@ -29,6 +29,7 @@ #include "wx/listbox.h" #endif +#include "wx/numformatter.h" #include "wx/valnum.h" #include "wx/textfile.h" #include "wx/spinctrl.h" @@ -997,7 +998,7 @@ void wxGridCellFloatEditor::BeginEdit(int row, int col, wxGrid* grid) const wxString value = table->GetValue(row, col); if ( !value.empty() ) { - if ( !value.ToDouble(&m_value) ) + if ( !wxNumberFormatter::FromString(value, &m_value) ) { wxFAIL_MSG( wxT("this cell doesn't have float value") ); return; @@ -1018,7 +1019,7 @@ bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row), double value; if ( !text.empty() ) { - if ( !text.ToDouble(&value) ) + if ( !wxNumberFormatter::FromString(text, &value) ) return false; } else // new value is empty string @@ -1060,20 +1061,9 @@ void wxGridCellFloatEditor::Reset() void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event) { int keycode = event.GetKeyCode(); - char tmpbuf[2]; - tmpbuf[0] = (char) keycode; - tmpbuf[1] = '\0'; - wxString strbuf(tmpbuf, *wxConvCurrent); - -#if wxUSE_INTL - bool is_decimal_point = ( strbuf == - wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER) ); -#else - bool is_decimal_point = ( strbuf == wxT(".") ); -#endif if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-' - || is_decimal_point ) + || keycode == wxNumberFormatter::GetDecimalSeparator() ) { wxGridCellTextEditor::StartingKey(event); @@ -1197,7 +1187,7 @@ wxString wxGridCellFloatEditor::GetString() m_format += wxT('f'); } - return wxString::Format(m_format, m_value); + return wxNumberFormatter::Format(m_format, m_value); } bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) @@ -1207,17 +1197,10 @@ bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event) const int keycode = event.GetKeyCode(); if ( wxIsascii(keycode) ) { -#if wxUSE_INTL - const wxString decimalPoint = - wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER); -#else - const wxString decimalPoint(wxT('.')); -#endif - // accept digits, 'e' as in '1e+6', also '-', '+', and '.' if ( wxIsdigit(keycode) || tolower(keycode) == 'e' || - keycode == decimalPoint || + keycode == wxNumberFormatter::GetDecimalSeparator() || keycode == '+' || keycode == '-' ) {