From d8ef864b02c44f46aa6e71401527fe1f17c4564d Mon Sep 17 00:00:00 2001 From: ocornut Date: Wed, 6 Sep 2023 11:28:14 +0200 Subject: [PATCH] InputTextMultiline: Fixed a crash pressing Down on last empty line of a multiline buffer. (#6783, #6000) Amend 57a5b73a4c --- docs/CHANGELOG.txt | 4 +++- imgui_widgets.cpp | 2 +- imstb_textedit.h | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 9bbdf182..fb503eb0 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -44,7 +44,9 @@ Breaking changes: Other changes: -- BeginListBox(): fixed not consuming SetNextWindowXXX data when returning false. +- InputTextMultiline: Fixed a crash pressing Down on last empty line of a multiline buffer. + (regression from 1.89.2, only happened in some states). (#6783, #6000) +- BeginListBox(): Fixed not consuming SetNextWindowXXX data when returning false. ----------------------------------------------------------------------- diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 5a59ddb3..d63a629f 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3693,7 +3693,7 @@ namespace ImStb { static int STB_TEXTEDIT_STRINGLEN(const ImGuiInputTextState* obj) { return obj->CurLenW; } -static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { return obj->TextW[idx]; } +static ImWchar STB_TEXTEDIT_GETCHAR(const ImGuiInputTextState* obj, int idx) { IM_ASSERT(idx <= obj->CurLenW); return obj->TextW[idx]; } static float STB_TEXTEDIT_GETWIDTH(ImGuiInputTextState* obj, int line_start_idx, int char_idx) { ImWchar c = obj->TextW[line_start_idx + char_idx]; if (c == '\n') return STB_TEXTEDIT_GETWIDTH_NEWLINE; ImGuiContext& g = *obj->Ctx; return g.Font->GetCharAdvance(c) * (g.FontSize / g.Font->FontSize); } static int STB_TEXTEDIT_KEYTOTEXT(int key) { return key >= 0x200000 ? 0 : key; } static ImWchar STB_TEXTEDIT_NEWLINE = '\n'; diff --git a/imstb_textedit.h b/imstb_textedit.h index a8a82311..062d13d6 100644 --- a/imstb_textedit.h +++ b/imstb_textedit.h @@ -2,7 +2,7 @@ // This is a slightly modified version of stb_textedit.h 1.14. // Those changes would need to be pushed into nothings/stb: // - Fix in stb_textedit_discard_redo (see https://github.com/nothings/stb/issues/321) -// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000) +// - Fix in stb_textedit_find_charpos to handle last line (see https://github.com/ocornut/imgui/issues/6000 + #6783) // Grep for [DEAR IMGUI] to find the changes. // stb_textedit.h - v1.14 - public domain - Sean Barrett @@ -549,7 +549,10 @@ static void stb_textedit_find_charpos(StbFindState *find, STB_TEXTEDIT_STRING *s i += r.num_chars; find->y += r.baseline_y_delta; if (i == z) // [DEAR IMGUI] + { + r.num_chars = 0; // [DEAR IMGUI] break; // [DEAR IMGUI] + } } find->first_char = first = i;