Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using localized decimal point in InputTextFilterCharacter #2278

Closed
HaSa1002 opened this issue Jan 13, 2019 · 7 comments
Closed

Using localized decimal point in InputTextFilterCharacter #2278

HaSa1002 opened this issue Jan 13, 2019 · 7 comments

Comments

@HaSa1002
Copy link

Version/Branch of Dear ImGui:

Version: 1.64
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: imgui-sfml
Operating System: Windows 10

My Issue/Question:
I use the InputDouble on a German machine. In Germany the numeric point is a comma. When I use the ImGui::InputDouble(...) then there is the ImGuiInputTextFlags_CharsScientific apllied. The InputTextFilterCharacter, then checks for the entered char, but discards a legal comma. It also appears that inserting a point does not work since it is not interpreted as decimal point by the sscanf function.

double v = 0;
std::setlocale(LC_NUMERIC, "de_DE.UTF-8");
ImGui::Begin("Input Validation");
ImGui::InputDouble("Try entering ','",&v );
ImGui::End();

Proposal
If would use the (unsigned int)*localeconv()->decimal_point instead, then the correct decimal point would be automatically selected.

//imgui_widgets.cpp
#include <clocale>
//...
static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) {
//...
if (flags & ImGuiInputTextFlags_CharsDecimal)
            if (!(c >= '0' && c <= '9') && (c != (unsigned int)*localeconv()->decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
                return false;

        if (flags & ImGuiInputTextFlags_CharsScientific)
            if (!(c >= '0' && c <= '9') && (c != (unsigned int)*localeconv()->decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E'))
                return false;

@ocornut
Copy link
Owner

ocornut commented Jan 13, 2019

Hello,

Which I appreciate the idea I think this is going to be a slowly never-ending pit of adjustment and side effects in other parts of the code, the responsability of which I would rather avoid.

Can't you set the locale to something English-based for the duration of the imgui code?

@HaSa1002
Copy link
Author

HaSa1002 commented Jan 13, 2019 via email

@ocornut
Copy link
Owner

ocornut commented Jan 13, 2019 via email

ocornut added a commit that referenced this issue Aug 28, 2020
…ented. (#2278) + Misc doc update.

Doc: Mention IMGUI_VERSION_NUM in recent api breaking changes + textwrap some demo code.
@ocornut
Copy link
Owner

ocornut commented Aug 28, 2020

Hello,

I finally looked at this today. Honestly I would advise switching (even temporarily) back to the default "C" locale while using dear imgui functions, but since you state those changes may be enough to get by (and a quick inspection here seemed to confirm it), I added suitable code to perform that override without modifying imgui source codes. I'm not however calling localeconv() because god knows the kind of thing this may pull into people projects.

Instead you can now add this in your app code:

ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point;

Please note that I don't expect to support that more widely. I think it has potential to complexify library code and betray the expectation of lots of user code to so. People may have entire data parsers assuming dot as a separator, etc. Consider the general not-very-internationalization-friendly stance of dear imgui I don't imagine this evolving.

I don't know if said project is still active with the de_DE.UTF-8 numeric locale, but if you end up updating and using it let me know if you find other parts of the code where locale could have side-effects.

Cheers.

@adamsepp
Copy link

adamsepp commented Aug 2, 2023

Hello,

Version: v1.89 WIP
Branch: Docking
Operating System: Win10 / Win11

Question / Suggestion:
I support both inputs: english '.' and german ',' decimal signs
but output always to english '.' decimal sign.

My current solution is the line:
if ((c == '.') || (c == ',')) *p_char = c = c_decimal_point; // cheeting for both decimal sign types...

Complete code part:

// Allow 0-9 . - + * /
if (flags & ImGuiInputTextFlags_CharsDecimal)
{
     if ((c == '.') || (c == ',')) *p_char = c = c_decimal_point; // cheeting for both decimal sign types...
       if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/'))
          return false;
}

// Allow 0-9 . - + * / e E
if (flags & ImGuiInputTextFlags_CharsScientific)
{
     if ((c == '.') || (c == ',')) *p_char = c = c_decimal_point; // cheeting for both decimal sign types...
        if (!(c >= '0' && c <= '9') && (c != c_decimal_point) && (c != '-') && (c != '+') && (c != '*') && (c != '/') && (c != 'e') && (c != 'E'))
         return false;
}

So far this is working out great for both user types (german and english keyboard).

@ocornut
first of all: ImGui is really awesome!
It would be nice if something like this already in the ImGui project, but I also understand that this might be too specific...?

ocornut added a commit that referenced this issue Aug 17, 2023
@ocornut
Copy link
Owner

ocornut commented Aug 17, 2023

FYI following discussions in #6719 I have moved PlatformLocaleDecimalPoint to the public ImGuiIO structure, allowing backends to potentially set it up correctly. This effectively breaks code that used to set it using internal, but breakage should only apply to few people and be hopefully obvious.

@ocornut
Copy link
Owner

ocornut commented Aug 22, 2024

FYI in today's change the storage of io.PlatformLocaleDecimalPoint has moved:
GetIO().PlatformLocaleDecimalPoint --> GetPlatformIO().Platform_LocaleDecimalPoint

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants