Skip to content

Commit

Permalink
IO: Exposed io.PlatformLocaleDecimalPoint to configure decimal point …
Browse files Browse the repository at this point in the history
…('.' or ','). (ocornut#6719, ocornut#2278)

Amend 13f7183
  • Loading branch information
ocornut committed Aug 17, 2023
1 parent a1a7a1b commit ba1fa90
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 11 deletions.
3 changes: 3 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Other changes:
of individual ImDrawList's buffer sizes when a dimming/modal background is rendered. (#6716)
- ImDrawList: Fixed OOB access in _CalcCircleAutoSegmentCount when passing excessively
large radius to AddCircle(). (#6657, #5317) [@EggsyCRO, @jdpatdiscord]
- IO: Exposed io.PlatformLocaleDecimalPoint to configure decimal point ('.' or ',') for
languages needing it. Should ideally be set to the value of '*localeconv()->decimal_point'
but our backends don't do it yet. (#6719, #2278)
- IO: Fixed io.AddMousePosEvent() and io.AddMouseButtonEvent() writing MouseSource to
wrong union section. Was semantically incorrect and accidentally had no side-effects
with default compiler alignment settings. (#6727) [@RickHuang2001]
Expand Down
1 change: 1 addition & 0 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,7 @@ ImGuiIO::ImGuiIO()
// Note: Initialize() will setup default clipboard/ime handlers.
BackendPlatformName = BackendRendererName = NULL;
BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL;
PlatformLocaleDecimalPoint = '.';

// Input (NB: we already have memset zero the entire structure!)
MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
Expand Down
3 changes: 3 additions & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,9 @@ struct ImGuiIO
void* _UnusedPadding; // Unused field to keep data structure the same size.
#endif

// Optional: Platform locale
ImWchar PlatformLocaleDecimalPoint; // '.' // [Experimental] Configure decimal point e.g. '.' or ',' useful for some languages (e.g. German), generally pulled from *localeconv()->decimal_point

//------------------------------------------------------------------
// Input - Call before calling NewFrame()
//------------------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions imgui_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,6 @@ struct ImGuiContext
// Platform support
ImGuiPlatformImeData PlatformImeData; // Data updated by current frame
ImGuiPlatformImeData PlatformImeDataPrev; // Previous frame data (when changing we will call io.SetPlatformImeDataFn
char PlatformLocaleDecimalPoint; // '.' or *localeconv()->decimal_point

// Settings
bool SettingsLoaded;
Expand Down Expand Up @@ -2212,7 +2211,6 @@ struct ImGuiContext

PlatformImeData.InputPos = ImVec2(0.0f, 0.0f);
PlatformImeDataPrev.InputPos = ImVec2(-1.0f, -1.0f); // Different to ensure initial submission
PlatformLocaleDecimalPoint = '.';

SettingsLoaded = false;
SettingsDirtyTimer = 0.0f;
Expand Down
18 changes: 9 additions & 9 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static const ImU64 IM_U64_MAX = (2ULL * 9223372036854775807LL + 1);
//-------------------------------------------------------------------------

// For InputTextEx()
static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source);
static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source);
static int InputTextCalcTextLenAndLineCount(const char* text_begin, const char** out_text_end);
static ImVec2 InputTextCalcTextSizeW(ImGuiContext* ctx, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL, ImVec2* out_offset = NULL, bool stop_on_new_line = false);

Expand Down Expand Up @@ -3905,7 +3905,7 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, const char* new_text, cons
}

// Return false to discard a character.
static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source)
static bool InputTextFilterCharacter(ImGuiContext* ctx, unsigned int* p_char, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data, ImGuiInputSource input_source)
{
IM_ASSERT(input_source == ImGuiInputSource_Keyboard || input_source == ImGuiInputSource_Clipboard);
unsigned int c = *p_char;
Expand Down Expand Up @@ -3944,10 +3944,10 @@ static bool InputTextFilterCharacter(unsigned int* p_char, ImGuiInputTextFlags f
// The standard mandate that programs starts in the "C" locale where the decimal point is '.'.
// We don't really intend to provide widespread support for it, but out of empathy for people stuck with using odd API, we support the bare minimum aka overriding the decimal point.
// Change the default decimal_point with:
// ImGui::GetCurrentContext()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point;
// ImGui::GetIO()->PlatformLocaleDecimalPoint = *localeconv()->decimal_point;
// Users of non-default decimal point (in particular ',') may be affected by word-selection logic (is_word_boundary_from_right/is_word_boundary_from_left) functions.
ImGuiContext& g = *GImGui;
const unsigned c_decimal_point = (unsigned int)g.PlatformLocaleDecimalPoint;
ImGuiContext& g = *ctx;
const unsigned c_decimal_point = (unsigned int)g.IO.PlatformLocaleDecimalPoint;
if (flags & (ImGuiInputTextFlags_CharsDecimal | ImGuiInputTextFlags_CharsScientific))
if (c == '.' || c == ',')
c = c_decimal_point;
Expand Down Expand Up @@ -4386,7 +4386,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
if ((flags & ImGuiInputTextFlags_AllowTabInput) && Shortcut(ImGuiKey_Tab, id) && !is_readonly)
{
unsigned int c = '\t'; // Insert TAB
if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
state->OnKeyPressed((int)c);
}

Expand All @@ -4402,7 +4402,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
unsigned int c = (unsigned int)io.InputQueueCharacters[n];
if (c == '\t') // Skip Tab, see above.
continue;
if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
state->OnKeyPressed((int)c);
}

Expand Down Expand Up @@ -4485,7 +4485,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
else if (!is_readonly)
{
unsigned int c = '\n'; // Insert new line
if (InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
if (InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Keyboard))
state->OnKeyPressed((int)c);
}
}
Expand Down Expand Up @@ -4552,7 +4552,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
{
unsigned int c;
s += ImTextCharFromUtf8(&c, s, NULL);
if (!InputTextFilterCharacter(&c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard))
if (!InputTextFilterCharacter(&g, &c, flags, callback, callback_user_data, ImGuiInputSource_Clipboard))
continue;
clipboard_filtered[clipboard_filtered_len++] = (ImWchar)c;
}
Expand Down

0 comments on commit ba1fa90

Please sign in to comment.