Repository navigation
Description
Admin-edited: Added ImGuiInputTextFlags_WordWrap on 2025/09
Original message below:
First of all, I am aware of the other posts regarding this issue: #952 #1062
This is a little bit different, I would like to attempt to implement this myself since this is not a high priority but I have a couple of questions.
Digging through the source code, I found that the editor is a slightly modified version of https://github.com/nothings/stb/blob/master/stb_textedit.h and, there is this particular paragraph commenting on word-wrapping:
// STB_TEXTEDIT_LAYOUTROW returns information about the shape of one displayed
// row of characters assuming they start on the i'th character--the width and
// the height and the number of characters consumed. This allows this library
// to traverse the entire layout incrementally. You need to compute word-wrapping
// here.
I see that that function is indeed implemented in imgui, more specifically in imgui_widgets.cpp:
static void STB_TEXTEDIT_LAYOUTROW(StbTexteditRow* r, STB_TEXTEDIT_STRING* obj, int line_start_idx)
{
const ImWchar* text = obj->TextW.Data;
const ImWchar* text_remaining = NULL;
const ImVec2 size = InputTextCalcTextSizeW(text + line_start_idx, text + obj->CurLenW, &text_remaining, NULL, true);
r->x0 = 0.0f;
r->x1 = size.x;
r->baseline_y_delta = size.y;
r->ymin = 0.0f;
r->ymax = size.y;
r->num_chars = (int)(text_remaining - (text + line_start_idx));
}
So if my understanding is correct, that function gets called when the cursor moves on the text (either by mouse or keyboard), but then I do not understand how that function would aid text wrapping.
That function does not get called when inserting new text, which should be were the wrapping should be checked.
So more specifically:
- Does the function STB_TEXTEDIT_LAYOUTROW have anything to do with how text is rendered or is only used to move the cursor even in wrapped text?
- What would be the overall steps to be able to achieve word wrapping? Is a total rewrite necessary or is it possible to achieve this by overriding a function?
- I see that
ImDrawList::AddTexthas awrap_widthparameter, I guess that if as a first step I should try to modify the calls to that function inImGui::InputTextEx, is that correct?
Thank you for your understanding, I want to build a note taking application and, for me, this is a requirement so I would like this feature, if someone can help I would greatly appreciate that.
Activity
ocornut commentedon May 16, 2020
Why not using the full featured text editor widgets listed in the wiki ?
pablojimenezmateo commentedon May 16, 2020
I have checked those projects and I do not require that much. Also adding this to the current implementation would help many developers such as myself.
I have been digging through the code and added the correct wrap_size to both AddText calls and the text does indeed get wrapped:
draw_window->DrawList->AddText(g.Font, g.FontSize, draw_pos - draw_scroll, col, buf_display, buf_display_end, inner_size.x, is_multiline ? NULL : &clip_rect);But, the cursor is not updated correctly. I am trying to update the cursor myself when I detect that the text has been wrapped, I want to add a copy of
ImFont::CalcWordWrapPositionAthat would return the number of "wrapped lines" and update the cursor accordingly.Although I have correctly ported the function (as static in the meantime) I am not 100% sure where the cursor is stored to update it.
I will try to dig around a little bit more, but any help will be welcomed :)
pablojimenezmateo commentedon May 18, 2020
I got some advances but the code is very complex and I did not have enough time to look at it over the weekend.
https://youtu.be/BBTho4ZnMIc
I have a better grasp on the complexity of the modifications:
ocornut commentedon May 18, 2020
Looking good!
I haven't answered your questions above because I don't know what the answers are without digging deeper in that. My whole impression is that we should ditch stb_textedit and rewrite the whole thing anyway without the utf-8<>utf16 round trips.
You'll notice they are various shortcuts involved in InputText inner-loops, namely to allow for very large text and because we don't retain much data. If we rewrite we should probably try to retain more data, in particular line offsets, to make performances more acceptable.
pablojimenezmateo commentedon May 22, 2020
I have seen those inner loops indeed, but I am afraid I lack the expertise to improve the whole TextEdit code, there are many functions that are still unclear to me.
Still, I will try to implement the wrapping as it is something I need. Do you know if the TextEdit rewrite is still a low priority? I guess not a lot of people would benefit from it, but in my case it is crucial.
Also, thank you for answering, and for doing so so quickly!
ocornut commentedon May 24, 2020
InputText rewrite is not a low priority but there are so many things competing for high priority at the moment it is a little tricky for me to try to even order them. There are many desirable InputText changes that would benefit from that larger refactor. I guess someone should sit down for 2 weeks and rewrite the whole thing from stratch with a check-list derived from entries in TODO.txt and inputtext tagged issues. That's not very helpful for you however.
I still believe the easiest path for you is to use one of the existing full-featured text editor.
pablojimenezmateo commentedon May 25, 2020
I understand, unfortunately I do not have the time nor the expertise for a full rewrite addressing all the open issues.
I wish I could contribute more, I just made a small donation in the hopes that this awesome project continues and some brave developer rewrites the TextEdit code :)
Thank you again for your time and your replies! I will try to use one of the existing text-editors in the meantime, as you suggested :)
jenergy commentedon May 9, 2021
Imgui_1.79_multiline_wrap.zip
Here is my implementation. It's a patch from 1.79.
Basically, input/output buffer remains clean (= without fake \n), but display string (and bytes) are word wrapped where available, otherwise they are letter wrapped (= word is splitted). This means that some fake '\n' are added in display variables, but not in buffer.
Everytime buffer IS GOING TO BE updated:
Easy to say, not so easy to implement..
Notes:
timprepscius commentedon Aug 16, 2022
I took the time to isolate jenergy's patch:
https://github.com/timprepscius/imgui/tree/multiline_wrap_jenergy
I tried to merge the jenergy with current-master, but it doesn't seem to work (compiles, but weirdness):
https://github.com/timprepscius/imgui/tree/tjp_main_multiline_editing
I then wrote a patch that seems to work.
https://github.com/ocornut/imgui/compare/master...timprepscius:imgui:tjp_multiline?expand=1
Obviously very new, will be working on bit by bit if bugs arise.
Take if you want.
jenergy commentedon Aug 16, 2022
abvadabra commentedon Feb 17, 2023
Just to add to the discussion, here is my relatively old patch for wordwrapping support in multiline textfield, based on 1.83. It is relatively stable (there are some minor quirks but nothing serious) and we've been using it in our editors for two years now.
It reuses
ImDrawList::AddTextwhich supports word wrapping, with some additional logic to handle navigation in wrapped text.It consists of two commits, but can be easily combined into one:
abvadabra@d6be798
abvadabra@4fb8f2f
video.3.mp4
dgm3333 commentedon Jun 20, 2023
I'd totally vote for this being included in the core code - as lack of workwrap in multiline edit is sad (although I also sympathise with ocornut's comment that everyone has "just one more" thing so I get why it isn't there...
Anyway just to add my contribution - purely because it doesn't require modifying core ImGui but takes advantage of the multiline edit.
This is a very quick NON PRODUCTION hack which adds soft returns.
To use it:
At initialisation run the addSoftReturnsToText on the string.
If multilineWidth changes remove all softreturns then rerun addSoftReturnsToText to put them back in new locations.
When the editing is finished then remove all "\r\n" to get the core string back with edits.
imgui_autosizingMultilineInput isn't required - I use it to ensure sufficient height for the text without -1 making it eat up all the space (and some others bits which aren't required here)
added a fix to imgui that makes the lines in inputtextmultiline wrap …
pixtur commentedon Jan 16, 2024
It would be quite handy to have this a as a standard component.
Since I'm on imgui.net the full text editor components are sadly not easy to include.
64 remaining items
InputText: Word-Wrap: added custom implementation for Home/End that i…
InputText: Word-Wrap: attempt to track cursor while resizing frame/pa…
InputText: fixed gross buffer underflow introduced by a82f66a. (ocorn…
stb_textedit: extracted stb_textedit_move_line_start()/stb_textedit_m…
InputText: Added a line index. Refactored cursor and selection render…
InputText: Word-Wrap: avoid word-wrap specific path InputTextLineInde…
InputText: Word-Wrap: moving ImGuiInputTextFlags_WordWrap to public A…
InputText: Word-Wrap: amend 7f1e2bb to avoid triggering static analyz…
InputText: Word-Wrap: hide vertical scrollbar but takes its width int…
InputTextMultiline: fixed a crash when using ImGuiInputTextFlags_Word…
Text: rewrite word-wrapping logic. (ocornut#8990, ocornut#3237, ocorn…
Text: word-wrapping use a small lookup table. (ocornut#8990, ocornut#…
InputTextMultiline: fixed an issue calculating lines count when inact…
stb_textedit: preserve cursor side at Dear ImGui word-wrap boundaries
stb_textedit: preserve cursor side at Dear ImGui word-wrap boundaries