From 78c1d4a92c42a5f8a327934a05543d984c9acff5 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 12 Sep 2025 15:02:09 +0200 Subject: [PATCH] InputText: Word-Wrap: moving ImGuiInputTextFlags_WordWrap to public API. Added in demo. (#3237, #952, #1062, #7363) --- docs/CHANGELOG.txt | 12 ++++++++++++ docs/TODO.txt | 1 - imgui.h | 9 +++++++++ imgui_demo.cpp | 7 ++++++- imgui_internal.h | 9 --------- 5 files changed, 27 insertions(+), 11 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index f8105a49a..b50ad6816 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -55,6 +55,18 @@ Other Changes: - Nav: fixed Ctrl+Tab window appearing as empty when the sole active and focused window has the ImGuiWindowFlags_NoNavFocus flag. (#8914) - Bullet: fixed tesselation amount which looked out of place in very large sizes. +- InputText: added ImGuiInputTextFlags_WordWrap flag to word-wrap multi-line buffers. + (#3237, #952, #1062, #7363) + - This is marked as beta because not being tested enough. + Please report any incorrect cursor movement, selection behavior etc. bug to #3237. + - Vertical scrollbar is made always visible. + - Wrapping points are not ideal. Wrapping of long words/sections (e.g. words + larger than total available width) may be particularly unpleasing. + - It is currently much slower than regular text fields. + - Ballpark estimate of cost on my 2019 desktop PC: + For a 100 KB text buffer: +~0.3 ms/+~1.0 ms (Optimized vs Debug builds). + - The CPU cost is very roughly proportional to text length, so a 10 KB buffer + should cost about ten times less. - InputText, InputInt, InputFloat: fixed an issue where using Escape to revert would not write back the reverted value during the IsItemDeactivatedAfterEdit() frame if the provided input buffer doesn't store temporary edits. diff --git a/docs/TODO.txt b/docs/TODO.txt index 90d2b35db..111852b43 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -87,7 +87,6 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i - input text multi-line: line numbers? status bar? (follow up on #200) - input text multi-line: behave better when user changes input buffer while editing is active (even though it is illegal behavior). namely, the change of buffer can create a scrollbar glitch (#725) - input text multi-line: better horizontal scrolling support (#383, #1224) - - input text multi-line: single call to AddText() should be coarse clipped on InputTextEx() end. - input number: optional range min/max for Input*() functions - input number: holding [-]/[+] buttons could increase the step speed non-linearly (or user-controlled) - input number: use mouse wheel to step up/down diff --git a/imgui.h b/imgui.h index e50ab814f..58c63f44a 100644 --- a/imgui.h +++ b/imgui.h @@ -1265,6 +1265,15 @@ enum ImGuiInputTextFlags_ ImGuiInputTextFlags_CallbackResize = 1 << 22, // Callback on buffer capacity changes request (beyond 'buf_size' parameter value), allowing the string to grow. Notify when the string wants to be resized (for string types which hold a cache of their Size). You will be provided a new BufSize in the callback and NEED to honor it. (see misc/cpp/imgui_stdlib.h for an example of using this) ImGuiInputTextFlags_CallbackEdit = 1 << 23, // Callback on any edit. Note that InputText() already returns true on edit + you can always use IsItemEdited(). The callback is useful to manipulate the underlying buffer while focus is active. + // Multi-line Word-Wrapping [BETA] + // - Not well tested yet. Please report any incorrect cursor movement, selection behavior etc. bug to https://github.com/ocornut/imgui/issues/3237. + // - Vertical scrollbar is made always visible. + // - Wrapping points are not ideal. Wrapping of long words/sections (e.g. words larger than total available width) may be particularly unpleasing. + // - It is much slower than regular text fields. + // Ballpark estimate of cost on my 2019 desktop PC: for a 100 KB text buffer: +~0.3 ms (Optimized) / +~1.0 ms (Debug build). + // The CPU cost is very roughly proportional to text length, so a 10 KB buffer should cost about ten times less. + ImGuiInputTextFlags_WordWrap = 1 << 24, // InputTextMultine(): word-wrap lines that are too long. + // Obsolete names //ImGuiInputTextFlags_AlwaysInsertMode = ImGuiInputTextFlags_AlwaysOverwrite // [renamed in 1.82] name was not matching behavior }; diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 26641f6b5..d667c8b70 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3708,6 +3708,8 @@ static void DemoWindowWidgetsTextInput() static ImGuiInputTextFlags flags = ImGuiInputTextFlags_AllowTabInput; HelpMarker("You can use the ImGuiInputTextFlags_CallbackResize facility if you need to wire InputTextMultiline() to a dynamic string type. See misc/cpp/imgui_stdlib.h for an example. (This is not demonstrated in imgui_demo.cpp because we don't want to include in here)"); ImGui::CheckboxFlags("ImGuiInputTextFlags_ReadOnly", &flags, ImGuiInputTextFlags_ReadOnly); + ImGui::CheckboxFlags("ImGuiInputTextFlags_WordWrap", &flags, ImGuiInputTextFlags_WordWrap); + ImGui::SameLine(); HelpMarker("Feature is currently in Beta. Please read comments in imgui.h"); ImGui::CheckboxFlags("ImGuiInputTextFlags_AllowTabInput", &flags, ImGuiInputTextFlags_AllowTabInput); ImGui::SameLine(); HelpMarker("When _AllowTabInput is set, passing through the widget with Tabbing doesn't automatically activate it, in order to also cycling through subsequent widgets."); ImGui::CheckboxFlags("ImGuiInputTextFlags_CtrlEnterForNewLine", &flags, ImGuiInputTextFlags_CtrlEnterForNewLine); @@ -3855,10 +3857,13 @@ static void DemoWindowWidgetsTextInput() // For this demo we are using ImVector as a string container. // Note that because we need to store a terminating zero character, our size/capacity are 1 more // than usually reported by a typical string class. + static ImGuiInputTextFlags flags = ImGuiInputTextFlags_None; + ImGui::CheckboxFlags("ImGuiInputTextFlags_WordWrap", &flags, ImGuiInputTextFlags_WordWrap); + static ImVector my_str; if (my_str.empty()) my_str.push_back(0); - Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16)); + Funcs::MyInputTextMultiline("##MyStr", &my_str, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), flags); ImGui::Text("Data: %p\nSize: %d\nCapacity: %d", (void*)my_str.begin(), my_str.size(), my_str.capacity()); ImGui::TreePop(); } diff --git a/imgui_internal.h b/imgui_internal.h index 021a24d98..8bd05c326 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1015,15 +1015,6 @@ enum ImGuiHoveredFlagsPrivate_ // Extend ImGuiInputTextFlags_ enum ImGuiInputTextFlagsPrivate_ { - // [Experimental] - // Word-wrapping caveats: - // - Not well tested yet. Please report any incorrect cursor movement, selection behavior etc. bug to https://github.com/ocornut/imgui/issues/3237. - // - With our current design it is _much_ slower than a regular text field. Editing a <50K buffer will generally be ok, but editing a 1MB buffer will waste meaningful amount of CPU. - // We are likely to not make the feature public until this is fixed (which requires bigger changes to InputText will be be generally desirable for this and other features) - // - Wrapping of long words/sections (e.g. words that are larger than available width) is currently visually not pleasing. - // - Vertical scrollbar is currently always visible. - ImGuiInputTextFlags_WordWrap = 1 << 24, // InputTextMultine(): wrap lines that are too long. (Ref #3237, #952, #1062) - // [Internal] ImGuiInputTextFlags_Multiline = 1 << 26, // For internal use by InputTextMultiline() ImGuiInputTextFlags_MergedItem = 1 << 27, // For internal use by TempInputText(), will skip calling ItemAdd(). Require bounding-box to strictly match.