cnjinhao / nana

a modern C++ GUI library
https://nana.acemind.cn
Boost Software License 1.0
2.3k stars 331 forks source link

textbox not updating scroll position when not focused #602

Closed ashbob999 closed 3 years ago

ashbob999 commented 3 years ago

If I have a nana::textbox with many lines so that the scroll bar appears, then I manually change the caret position using nana::textbox::caret_pos(const upoint& pos) while the textbox is focused the caret position is changed correctly and the view is scolled so that the caret is visible. But if I call nana::textbox::caret_pos(const upoint& pos) from another source (i.e. a button) the caret changes position but the textbox does scroll to make the caret visible.

Code to test:

#include <nana/gui.hpp>
#include <nana/gui/widgets/textbox.hpp>
#include <nana/gui/widgets/button.hpp>

using namespace nana;

int main()
{
    form fm;
    fm.div("<tb><btn>");

    textbox tb{ fm };

    tb.events().key_press([&tb](const arg_keyboard& arg)
    {
        if (arg.key == 'A')
        {
            tb.caret_pos({ 0, 0 });
        }
    });

    button btn{ fm, "button" };

    btn.events().click([&tb] {
    tb.caret_pos({ 0, 0 });
    });

    fm["tb"] << tb;
    fm["btn"] << btn;

    fm.collocate();

    fm.show();

    exec();
}

Pressing a when the text box has many line (so scroll bar is shown) takes you back to the first line. But pressing the button updates the carets position but does not take you to it's position.

After looking at the source code it looks like this is the problem: nana\source\gui\widgets\skeletonstext_editior.cpp Line 1709-1710

Not sure what the reason behind this is, but a temp fix can be achieved by changing the button lambda to:

btn.events().click([&tb, &btn] {
    api::focus_window(tb);
    tb.caret_pos({ 0, 0 });
    api::focus_window(btn);
});

So really my question is what is the reason behind not updating the scroll when not focused? and is/can there be a fix where I do not have to manually adjust the widgets focus.

cnjinhao commented 3 years ago

Fixed!

tb.caret_pos({ 0, 0 }, true);
ashbob999 commented 3 years ago

Thanks.