From a1632c6116d3d5f0cd1eccb8a0b49e61acc5398c Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 24 Oct 2025 16:42:21 +0200 Subject: [PATCH] InputText, Demo: amend comments to direct more users to the std::string version. https://www.youtube.com/watch?v=pLwvNdpTpjs wasted a solid hour before finding this. Crazy that people are using AI instead of actually _reading_ comments, demo and headers. (The information appeared multiple times on their screen) --- imgui.h | 2 +- imgui_demo.cpp | 11 +++++++---- imgui_widgets.cpp | 5 ++--- misc/cpp/imgui_stdlib.cpp | 12 ++++++++++++ misc/cpp/imgui_stdlib.h | 12 ++++++++++++ 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/imgui.h b/imgui.h index b58f09430..e7920e64f 100644 --- a/imgui.h +++ b/imgui.h @@ -705,7 +705,7 @@ namespace ImGui IMGUI_API bool VSliderScalar(const char* label, const ImVec2& size, ImGuiDataType data_type, void* p_data, const void* p_min, const void* p_max, const char* format = NULL, ImGuiSliderFlags flags = 0); // Widgets: Input with Keyboard - // - If you want to use InputText() with std::string or any custom dynamic string type, see misc/cpp/imgui_stdlib.h and comments in imgui_demo.cpp. + // - If you want to use InputText() with std::string or any custom dynamic string type, use the wrapper in misc/cpp/imgui_stdlib.h/.cpp! // - Most of the ImGuiInputTextFlags flags are only useful for InputText() and not for InputFloatX, InputIntX, InputDouble etc. IMGUI_API bool InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); IMGUI_API bool InputTextMultiline(const char* label, char* buf, size_t buf_size, const ImVec2& size = ImVec2(0, 0), ImGuiInputTextFlags flags = 0, ImGuiInputTextCallback callback = NULL, void* user_data = NULL); diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 8f0e393a1..3c3900b43 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -887,8 +887,9 @@ static void DemoWindowWidgetsBasic() ImGui::SeparatorText("Inputs"); { - // To wire InputText() with std::string or any other custom string type, - // see the "Text Input > Resize Callback" section of this demo, and the misc/cpp/imgui_stdlib.h file. + // If you want to use InputText() with std::string or any custom dynamic string type: + // - For std::string: use the wrapper in misc/cpp/imgui_stdlib.h/.cpp + // - Otherwise, see the 'Dear ImGui Demo->Widgets->Text Input->Resize Callback' for using ImGuiInputTextFlags_CallbackResize. IMGUI_DEMO_MARKER("Widgets/Basic/InputText"); static char str0[128] = "Hello, world!"; ImGui::InputText("input text", str0, IM_ARRAYSIZE(str0)); @@ -3696,8 +3697,10 @@ static void DemoWindowWidgetsTextInput() IMGUI_DEMO_MARKER("Widgets/Text Input/Multi-line Text Input"); if (ImGui::TreeNode("Multi-line Text Input")) { - // Note: we are using a fixed-sized buffer for simplicity here. See ImGuiInputTextFlags_CallbackResize - // and the code in misc/cpp/imgui_stdlib.h for how to setup InputText() for dynamically resizing strings. + // WE ARE USING A FIXED-SIZE BUFFER FOR SIMPLICITY HERE. + // If you want to use InputText() with std::string or any custom dynamic string type: + // - For std::string: use the wrapper in misc/cpp/imgui_stdlib.h/.cpp + // - Otherwise, see the 'Dear ImGui Demo->Widgets->Text Input->Resize Callback' for using ImGuiInputTextFlags_CallbackResize. static char text[1024 * 16] = "/*\n" " The Pentium F00F bug, shorthand for F0 0F C7 C8,\n" diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index e2a8e9580..13d4b3be4 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -3920,6 +3920,7 @@ namespace ImStb #include "imstb_textedit.h" } +// If you want to use InputText() with std::string or any custom dynamic string type, use the wrapper in misc/cpp/imgui_stdlib.h/.cpp! bool ImGui::InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* user_data) { IM_ASSERT(!(flags & ImGuiInputTextFlags_Multiline)); // call InputTextMultiline() @@ -4620,9 +4621,7 @@ static ImVec2 InputTextLineIndexGetPosOffset(ImGuiContext& g, ImGuiInputTextStat // This is so we can easily call InputText() on static arrays using ARRAYSIZE() and to match // Note that in std::string world, capacity() would omit 1 byte used by the zero-terminator. // - When active, hold on a privately held copy of the text (and apply back to 'buf'). So changing 'buf' while the InputText is active has no effect. -// - If you want to use ImGui::InputText() with std::string, see misc/cpp/imgui_stdlib.h -// (FIXME: Rather confusing and messy function, among the worse part of our codebase, expecting to rewrite a V2 at some point.. Partly because we are -// doing UTF8 > U16 > UTF8 conversions on the go to easily interface with stb_textedit. Ideally should stay in UTF-8 all the time. See https://github.com/nothings/stb/issues/188) +// - If you want to use InputText() with std::string or any custom dynamic string type, use the wrapper in misc/cpp/imgui_stdlib.h/.cpp! bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_size, const ImVec2& size_arg, ImGuiInputTextFlags flags, ImGuiInputTextCallback callback, void* callback_user_data) { ImGuiWindow* window = GetCurrentWindow(); diff --git a/misc/cpp/imgui_stdlib.cpp b/misc/cpp/imgui_stdlib.cpp index c04d487cc..7c463ca6b 100644 --- a/misc/cpp/imgui_stdlib.cpp +++ b/misc/cpp/imgui_stdlib.cpp @@ -1,9 +1,21 @@ // dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) + // This is also an example of how you may wrap your own similar types. +// TL;DR; this is using the ImGuiInputTextFlags_CallbackResize facility, +// which also demonstrated in 'Dear ImGui Demo->Widgets->Text Input->Resize Callback'. // Changelog: // - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string +// Usage: +// { +// #include "misc/cpp/imgui_stdlib.h" +// #include "misc/cpp/imgui_stdlib.cpp" // <-- If you want to include implementation without messing with your project/build. +// [...] +// std::string my_string; +// ImGui::InputText("my string", &my_string); +// } + // See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki: // https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness diff --git a/misc/cpp/imgui_stdlib.h b/misc/cpp/imgui_stdlib.h index 697fc34ad..cf4b528bd 100644 --- a/misc/cpp/imgui_stdlib.h +++ b/misc/cpp/imgui_stdlib.h @@ -1,9 +1,21 @@ // dear imgui: wrappers for C++ standard library (STL) types (std::string, etc.) + // This is also an example of how you may wrap your own similar types. +// TL;DR; this is using the ImGuiInputTextFlags_CallbackResize facility, +// which also demonstrated in 'Dear ImGui Demo->Widgets->Text Input->Resize Callback'. // Changelog: // - v0.10: Initial version. Added InputText() / InputTextMultiline() calls with std::string +// Usage: +// { +// #include "misc/cpp/imgui_stdlib.h" +// #include "misc/cpp/imgui_stdlib.cpp" // <-- If you want to include implementation without messing with your project/build. +// [...] +// std::string my_string; +// ImGui::InputText("my string", &my_string); +// } + // See more C++ related extension (fmt, RAII, syntaxis sugar) on Wiki: // https://github.com/ocornut/imgui/wiki/Useful-Extensions#cness