diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 932edd3e8..3b2587547 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -49,6 +49,8 @@ Other Changes: EndTable() was mistakenly restoring a wrong current table. - InputText: avoid continuously overwriting ownership of ImGuiKey_Enter/_KeypadEnter keys in order to allow e.g. external Shortcut override behavior. (#9004) +- InputText: when using a callback to reduce/manipulate the value of BufTextLen, + we do not require anymore that CursorPos be clamped by user code. (#9029) - InputTextMultiline: fixed a crash when using ImGuiInputTextFlags_WordWrap and resizing the parent window while keeping the multi-line field active (which is most typically achieved when resizing programmatically or via a docking layout diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 910c73d36..97fb208ee 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -5274,11 +5274,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ IM_ASSERT(callback_data.Buf == callback_buf); // Invalid to modify those fields IM_ASSERT(callback_data.BufSize == state->BufCapacity); IM_ASSERT(callback_data.Flags == flags); - const bool buf_dirty = callback_data.BufDirty; - if (buf_dirty || callback_data.CursorPos != state->Stb->cursor) { state->Stb->cursor = callback_data.CursorPos; state->CursorFollow = true; } - if (buf_dirty || callback_data.SelectionStart != state->Stb->select_start) { state->Stb->select_start = (callback_data.SelectionStart == callback_data.CursorPos) ? state->Stb->cursor : callback_data.SelectionStart; } - if (buf_dirty || callback_data.SelectionEnd != state->Stb->select_end) { state->Stb->select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb->select_start : callback_data.SelectionEnd; } - if (buf_dirty) + if (callback_data.BufDirty || callback_data.CursorPos != state->Stb->cursor) + state->CursorFollow = true; + state->Stb->cursor = ImClamp(callback_data.CursorPos, 0, callback_data.BufTextLen); + state->Stb->select_start = ImClamp(callback_data.SelectionStart, 0, callback_data.BufTextLen); + state->Stb->select_end = ImClamp(callback_data.SelectionEnd, 0, callback_data.BufTextLen); + if (callback_data.BufDirty) { // Callback may update buffer and thus set buf_dirty even in read-only mode. IM_ASSERT(callback_data.BufTextLen == (int)ImStrlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text!