wxSpinCtrlDouble with comma Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

wxSpinCtrlDouble with comma

Post by mael15 » Fri Apr 17, 2020 8:31 am

hello everyone,
when it comes to floating point numbers there always is the problem of internationalization. I want users to be able to use their numbers block to enter a floating point number, in Germany that means typing a comma instead of a dot. I could check every keyboard entry for a comma and replace it with a dot, but is there an easier way to make wxSpinCtrlDouble use the user´s local decimal point? Maybe even when displaying the number?
And another issue: can I make wxSpinCtrlDouble always display i.e. "5.0" instead of "5"? It is shown in the docs under "appearance", but I cannot see it like this on wxMSW 3.1.3? EDIT: this is acchieved by SetDigits(1).
Thank you.

User avatar
doublemax
Moderator
Moderator
Posts: 15282
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSpinCtrlDouble with comma

Post by doublemax » Fri Apr 17, 2020 8:53 am

I want users to be able to use their numbers block to enter a floating point number, in Germany that means typing a comma instead of a dot. I could check every keyboard entry for a comma and replace it with a dot, but is there an easier way to make wxSpinCtrlDouble use the user´s local decimal point? Maybe even when displaying the number?
That should work out of the box if you're using wxLocale.
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSpinCtrlDouble with comma

Post by mael15 » Fri Apr 17, 2020 9:23 am

I see what you mean, in my first post I used a minimal sample without wxLocale to be able to post code quickly. It does work as expected in my full app with wxLocale.
The specific problem is, I have two possible languages in my app: the language of the app and the language of pdfs created by the app. I set the language of the pdf with wxLocale and the language of the app with wxTranslations. this new wxSpinCtrlDoule is part of the app, but now uses the language specific decimal point of the pdfs. Do I have to switch wxLocale/wxTranslation or is there a way to tell this one wxSpinCtrlDouble to use a certain wxLocale? That would save me a lot of work.

User avatar
doublemax
Moderator
Moderator
Posts: 15282
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSpinCtrlDouble with comma

Post by doublemax » Fri Apr 17, 2020 9:55 am

I set the language of the pdf with wxLocale and the language of the app with wxTranslations.
Can you reverse this? That would make more sense to me.

And what do you mean with "language of the pdf"?

I don't know if it helps in your case, but there are two wxString methods that explicitly use the C locale, therefore ignoring the current locale: wxString::ToCDouble() and wxString::FromCDouble().
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSpinCtrlDouble with comma

Post by mael15 » Fri Apr 17, 2020 10:16 am

doublemax wrote:
Fri Apr 17, 2020 9:55 am
Can you reverse this? That would make more sense to me.
That is what would make a lot of work, and only for a comma/dot in this one control.
doublemax wrote:
Fri Apr 17, 2020 9:55 am
And what do you mean with "language of the pdf"?
My app can be in one of four languages and create pdfs in some other of these languages.
doublemax wrote:
Fri Apr 17, 2020 9:55 am
I don't know if it helps in your case, but there are two wxString methods that explicitly use the C locale, therefore ignoring the current locale: wxString::ToCDouble() and wxString::FromCDouble().
But how could I get the entered value as a wxString? GetValue returns a double and seems to be called after correcting the user entered value, so it is too late?

User avatar
doublemax
Moderator
Moderator
Posts: 15282
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: wxSpinCtrlDouble with comma

Post by doublemax » Fri Apr 17, 2020 10:24 am

My general idea was that you let wxLocale handle the control and use these two functions to control the content of the PDFs. But apparently that's not an option.

But i also don't have any better idea for your problem.
is there a way to tell this one wxSpinCtrlDouble to use a certain wxLocale? That would save me a lot of work.
I don't think so.
Use the source, Luke!

mael15
Super wx Problem Solver
Super wx Problem Solver
Posts: 449
Joined: Fri May 22, 2009 8:52 am
Location: Bremen, Germany

Re: wxSpinCtrlDouble with comma

Post by mael15 » Fri Apr 17, 2020 10:44 am

doublemax wrote:
Fri Apr 17, 2020 10:24 am
My general idea was that you let wxLocale handle the control and use these two functions to control the content of the PDFs. But apparently that's not an option.
I understand, that is certainly the cleanest option, but like I said a LOT of work in my specific case.
This works, it is kind of dirty but the best option for me at the moment:

Code: Select all

Bind(wxEVT_TEXT, &MyCtrl::onTextEntered, this);
and handle it like this

Code: Select all

void onTextEntered(wxCommandEvent& evt) {
		wxString decPnt = wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT, wxLOCALE_CAT_NUMBER);
		wxString str = evt.GetString();
		wxString enteredChar = str.Last();
		if ((enteredChar == ',' || enteredChar == '.') && !enteredChar.IsSameAs(decPnt)) {
			SetDigits(1);
			SetValue(wxAtof(str.BeforeFirst(decPnt.IsSameAs('.') ? ',' : '.')));
			str = wxString::Format(wxT("%.1f"), GetValue());
			SetSelection(str.length() - 1, str.length());
		}
	}

Post Reply