Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline text with ReadOnly can render incorrectly when text changes #8242

Closed
bgribble opened this issue Dec 18, 2024 · 5 comments
Closed

Comments

@bgribble
Copy link

Version/Branch of Dear ImGui:

Version 1.91.5, via imgui_bundle

Back-ends:

SDL2

Compiler, OS:

Linux + python and c++

Full config/build information:

No response

Details:

I discovered this issue while working with ImGui Bundle in python. The full report with reproduction code in Python and C++, and a screencast of the behavior, is in pthom/imgui_bundle#295

My application uses an InputTextMultiline with the ReadOnly flag to implement an interactive REPL in the application. I made it ReadOnly so I can handle input myself, to implement app-specific keybindings for history navigation and editing.

I observed that under some circumstances the InputTextMultiline would display spaces or nonprinting characters (appearing as ???) rather than the text that was passed to the widget. Clicking in a different Imgui window in the app would cause the text to appear as expected.

I discussed with Pascal Thomet and he believes that this might be a bug in Imgui, since the following simple C++ code demonstrates it.

Screenshots/Video:

v.mp4

Minimal, Complete and Verifiable Example code:

#include <imgui.h>
#include "misc/cpp/imgui_stdlib.h"
#include <hello_imgui/hello_imgui.h>

std::string static_text = R"(
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex
ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint
occaecat cupidatat non proident, sunt in culpa qui officia deserunt
mollit anim id est laborum.
)";


char buffer[2048];

int frame_count = 0;

void gui()
{
    ImGui::Text("Should reveal text over time.");
    ImGui::Text("Click in the multiline text widget to see invisible and erroneous characters");
    ImGui::Text("Then click outside the widget to see the correct text restored");
    ImGui::Text("Code taken from demo_widgets.py");

    auto callback = [](ImGuiInputTextCallbackData* cb_data) -> int
    {
        int bufpos = frame_count / 2;
        cb_data->SelectionStart = bufpos;
        cb_data->SelectionEnd = bufpos;
        cb_data->CursorPos = bufpos;
        return 0;
    };

    memset(buffer, 0, sizeof(buffer));
    strncpy(buffer, static_text.c_str(), frame_count / 2);

    ImGui::InputTextMultiline(
        "readonly_multiline",
        buffer,
        sizeof(buffer),
        ImVec2(600, 200),
        ImGuiInputTextFlags_ReadOnly | ImGuiInputTextFlags_CallbackAlways,
        callback
    );
    frame_count++;
    if (frame_count > 2 * static_text.size())
        frame_count = 0;
}


int main()
{
    HelloImGui::Run(gui);
}
ocornut added a commit that referenced this issue Dec 18, 2024
ocornut added a commit that referenced this issue Dec 18, 2024
… buffer in text processing code. (#8242)

Followup to e900571
Removed SetClipboardText() trick used in abd07f6 (#7925)
@ocornut
Copy link
Owner

ocornut commented Dec 18, 2024

Thank you for your report and the repro, @bgribble and @pthom.

I have pushed a first fix: e900571 which uses a bit of a "live dangerously" approach, and I then extended and improved that work and removed the "live dangerously" bit by introducing an internal TextSrc pointer in 32f1140 and ensuring all read only operations used it.

Even though all tests are passing, statistically speaking I won't be surprised if this introduced a small issue somewhere. We will find out.

pthom added a commit to pthom/imgui_bundle that referenced this issue Dec 18, 2024
@pthom
Copy link
Contributor

pthom commented Dec 18, 2024

Merci beaucoup Omar for your correction in a record time!

I like to live dangerously sometimes also :-)
So, since you merged the correction to the docking branch, I applied the changes to Dear ImGui Bundle in this commit, and the issue is also solved in Python!

@bgribble : the Python wheels will be available shortly here: https://github.com/pthom/imgui_bundle/actions/runs/12398172321

@ocornut
Copy link
Owner

ocornut commented Dec 22, 2024

My apologies, 32f1140 pushed on wednesday badly broke clipboard cut/copy.
Pushed fix 6982ce4

@pthom
Copy link
Contributor

pthom commented Dec 22, 2024

Thanks for letting me know!

pthom added a commit to pthom/imgui_bundle that referenced this issue Dec 22, 2024
@pthom
Copy link
Contributor

pthom commented Dec 22, 2024

I updated the python side too. Thanks !

matthew-mccall pushed a commit to matthew-mccall/imgui that referenced this issue Jan 1, 2025
matthew-mccall pushed a commit to matthew-mccall/imgui that referenced this issue Jan 1, 2025
… buffer in text processing code. (ocornut#8242)

Followup to e900571
Removed SetClipboardText() trick used in abd07f6 (ocornut#7925)
matthew-mccall pushed a commit to matthew-mccall/imgui that referenced this issue Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants