You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ImGui::InputText Max Length with Text Selection Replacement #9029
PS: clamping CursorPos (if (data->CursorPos > max_len) data->CursorPos = max_len;) is currently required but it is a bug IMHO which I will fix right away to auto-clamp.
PS: clamping CursorPos (if (data->CursorPos > max_len) data->CursorPos = max_len;) is currently required but it is a bug IMHO which I will fix right away to auto-clamp.
Pushed a small change 750c5d2 so the code above doesn't need the if (data->CursorPos > max_len) data->CursorPos = max_len; part.
However you will hit the current limitation:
If you have a 8 long string, try to paste 5 characters: pasting will be cancelled.
If you have a 10 long string, select 2 character, try to paste 5 characters: selection will be deleted, pasting will be cancelled.
For this to work we will need to rework STB_TEXTEDIT_INSERTCHARS() to return a count instead of a bool and allow partial append.
I have reverted the truncation behavior with 2a194e2 because it can truncate in the middle of a UTF-8 sequence which is quite undesirable. Will investigate.
Activity
Wangbull commentedon Oct 23, 2025
The problem:
When text is selected, replacing it does not properly respect the length limit.
I'm not sure whether to use CallbackCharFilter, CallbackEdit, or some combination to enforce max length while handling text replacement.
Looking for a solution that enforces a maximum character limit while correctly supporting selection replacement in an InputText callback.
ocornut commentedon Oct 23, 2025
FYI specifying a fixed buf size works:
But a paste that goes beyond limit is cancelled out.
ocornut commentedon Oct 23, 2025
If you want newly pasted content to have priority (always insert), that's a possibility:
PS: clamping CursorPos (
if (data->CursorPos > max_len) data->CursorPos = max_len;) is currently required but it is a bug IMHO which I will fix right away to auto-clamp.InputText: do not require anymore that CursorPos be clamped by user c…
ocornut commentedon Oct 23, 2025
Pushed a small change 750c5d2 so the code above doesn't need the
if (data->CursorPos > max_len) data->CursorPos = max_len;part.ocornut commentedon Oct 23, 2025
I guess the best solution is to use this:
for char[]:
for
std::stringrely on enforcing the limit:With this setup, you can have a 10-long string, select 5 characters and paste 5 characters.
However you will hit the current limitation:
For this to work we will need to rework
STB_TEXTEDIT_INSERTCHARS()to return a count instead of a bool and allow partial append.InputText: rework STB_TEXTEDIT_INSERTCHARS() signature to allow parti…
InputText: paste does a truncate when not fitting instead of ignoring…
ocornut commentedon Oct 23, 2025
I have decided to rework this:
I believe it is more consistent with typing text and therefore a better behavior for
InputText().To answer your precise problem, use code in #9029 (comment) and if you want clipboard paste truncating you should update the code.
InputText: revert truncation behavior from e612536 as it can truncate…
ocornut commentedon Oct 29, 2025
I have reverted the truncation behavior with 2a194e2 because it can truncate in the middle of a UTF-8 sequence which is quite undesirable. Will investigate.
InputText: restore truncating, now between UTF-8 codepoint. (#9029)
TestSuite: reenabled "widgets_inputtext_insert_truncate", added "widg…
ocornut commentedon Oct 29, 2025
Truncation is now performed between UTF-8 codepoints with 3c578fa + pushed a new
widgets_inputtext_insert_truncate_2test: ocornut/imgui_test_engine@46a98fbInputText: do not require anymore that CursorPos be clamped by user c…
InputText: rework STB_TEXTEDIT_INSERTCHARS() signature to allow parti…
InputText: paste does a truncate when not fitting instead of ignoring…
InputText: revert truncation behavior from e612536 as it can truncate…
InputText: restore truncating, now between UTF-8 codepoint. (ocornut#…
stb_textedit: return inserted counts from STB_TEXTEDIT_INSERTCHARS
stb_textedit: return inserted counts from STB_TEXTEDIT_INSERTCHARS