From f694244dec94ca67666d767fd5be59faf74686b9 Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 4 Feb 2020 15:19:49 +0100 Subject: [PATCH] InputText: Fix using a combination of _CallbackResize + _EnterReturnsTrue + lack of persisting user storage. (#3009) Amend 24ff25981 (#2006, #1443, #1008) --- docs/CHANGELOG.txt | 4 ++++ imgui_widgets.cpp | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 1970a63b..8ea4737f 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -78,6 +78,10 @@ Other Changes: - ColorEdit: "Copy As" content-menu tool shows hex values both with/without alpha when available. - InputText: Fix corruption or crash when executing undo after clearing input with ESC, as a byproduct we are allowing to later undo the revert with a CTRL+Z. (#3008). +- InputText: Fix using a combination of _CallbackResize (e.g. for std::string binding), along with the + _EnterReturnsTrue flag along with the rarely used property of using an InputText without persisting + user-side storage. Previously if you had e.g. a local unsaved std::string and reading result back + from the widget, the user string object wouldn't be resized when Enter key was pressed. (#3009) - MenuBar: Fix minor clipping issue where occasionally a menu text can overlap the right-most border. - Window: Fix SetNextWindowBgAlpha(1.0f) failing to override alpha component. (#3007) [@Albog] - Window: When testing for the presence of the ImGuiWindowFlags_NoBringToFrontOnFocus flag we diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 67bba187..5faa31b2 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3857,7 +3857,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ } // When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame. - // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. Also this allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage. + // If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. + // This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize). bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0); if (apply_edit_back_to_user_buffer) { @@ -3951,8 +3952,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ // Copy result to user buffer if (apply_new_text) { + // We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size + // of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used + // without any storage on user's side. IM_ASSERT(apply_new_text_length >= 0); - if (backup_current_text_length != apply_new_text_length && is_resizable) + if (is_resizable) { ImGuiInputTextCallbackData callback_data; callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize; @@ -3967,6 +3971,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_ apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1); IM_ASSERT(apply_new_text_length <= buf_size); } + //IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length); // If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size. ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size));