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.
This commit is contained in:
@@ -25,6 +25,7 @@
|
|||||||
#include "wx/checkbox.h"
|
#include "wx/checkbox.h"
|
||||||
#endif // WX_PRECOMP
|
#endif // WX_PRECOMP
|
||||||
|
|
||||||
|
#include "wx/numformatter.h"
|
||||||
#include "wx/tokenzr.h"
|
#include "wx/tokenzr.h"
|
||||||
#include "wx/renderer.h"
|
#include "wx/renderer.h"
|
||||||
|
|
||||||
@@ -839,7 +840,7 @@ wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
text = table->GetValue(row, col);
|
text = table->GetValue(row, col);
|
||||||
hasDouble = text.ToDouble(&val);
|
hasDouble = wxNumberFormatter::FromString(text, &val);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( hasDouble )
|
if ( hasDouble )
|
||||||
@@ -877,8 +878,7 @@ wxString wxGridCellFloatRenderer::GetString(const wxGrid& grid, int row, int col
|
|||||||
m_format += wxT('f');
|
m_format += wxT('f');
|
||||||
}
|
}
|
||||||
|
|
||||||
text.Printf(m_format, val);
|
text = wxNumberFormatter::Format(m_format, val);
|
||||||
|
|
||||||
}
|
}
|
||||||
//else: text already contains the string
|
//else: text already contains the string
|
||||||
|
|
||||||
|
@@ -29,6 +29,7 @@
|
|||||||
#include "wx/listbox.h"
|
#include "wx/listbox.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include "wx/numformatter.h"
|
||||||
#include "wx/valnum.h"
|
#include "wx/valnum.h"
|
||||||
#include "wx/textfile.h"
|
#include "wx/textfile.h"
|
||||||
#include "wx/spinctrl.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);
|
const wxString value = table->GetValue(row, col);
|
||||||
if ( !value.empty() )
|
if ( !value.empty() )
|
||||||
{
|
{
|
||||||
if ( !value.ToDouble(&m_value) )
|
if ( !wxNumberFormatter::FromString(value, &m_value) )
|
||||||
{
|
{
|
||||||
wxFAIL_MSG( wxT("this cell doesn't have float value") );
|
wxFAIL_MSG( wxT("this cell doesn't have float value") );
|
||||||
return;
|
return;
|
||||||
@@ -1018,7 +1019,7 @@ bool wxGridCellFloatEditor::EndEdit(int WXUNUSED(row),
|
|||||||
double value;
|
double value;
|
||||||
if ( !text.empty() )
|
if ( !text.empty() )
|
||||||
{
|
{
|
||||||
if ( !text.ToDouble(&value) )
|
if ( !wxNumberFormatter::FromString(text, &value) )
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else // new value is empty string
|
else // new value is empty string
|
||||||
@@ -1060,20 +1061,9 @@ void wxGridCellFloatEditor::Reset()
|
|||||||
void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
|
void wxGridCellFloatEditor::StartingKey(wxKeyEvent& event)
|
||||||
{
|
{
|
||||||
int keycode = event.GetKeyCode();
|
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 == '-'
|
if ( wxIsdigit(keycode) || keycode == '+' || keycode == '-'
|
||||||
|| is_decimal_point )
|
|| keycode == wxNumberFormatter::GetDecimalSeparator() )
|
||||||
{
|
{
|
||||||
wxGridCellTextEditor::StartingKey(event);
|
wxGridCellTextEditor::StartingKey(event);
|
||||||
|
|
||||||
@@ -1197,7 +1187,7 @@ wxString wxGridCellFloatEditor::GetString()
|
|||||||
m_format += wxT('f');
|
m_format += wxT('f');
|
||||||
}
|
}
|
||||||
|
|
||||||
return wxString::Format(m_format, m_value);
|
return wxNumberFormatter::Format(m_format, m_value);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
|
bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
|
||||||
@@ -1207,17 +1197,10 @@ bool wxGridCellFloatEditor::IsAcceptedKey(wxKeyEvent& event)
|
|||||||
const int keycode = event.GetKeyCode();
|
const int keycode = event.GetKeyCode();
|
||||||
if ( wxIsascii(keycode) )
|
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 '.'
|
// accept digits, 'e' as in '1e+6', also '-', '+', and '.'
|
||||||
if ( wxIsdigit(keycode) ||
|
if ( wxIsdigit(keycode) ||
|
||||||
tolower(keycode) == 'e' ||
|
tolower(keycode) == 'e' ||
|
||||||
keycode == decimalPoint ||
|
keycode == wxNumberFormatter::GetDecimalSeparator() ||
|
||||||
keycode == '+' ||
|
keycode == '+' ||
|
||||||
keycode == '-' )
|
keycode == '-' )
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user