cnjinhao / nana

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

spinbox edit box does not track allowable values #597

Open jimorc opened 3 years ago

jimorc commented 3 years ago

That's a pretty badly titled issue, but the explanation below should clear things up:

We start with the following sample program:

#include <iostream>
#include <nana/gui.hpp>
#include <nana/gui/widgets/spinbox.hpp>

using namespace nana;
using namespace std;

int main()
{
    form fm;
    fm.div(string("vertical <<spin weight=150> weight=25>"));

    spinbox sb(fm);
    sb.range(1, 99, 1);
    sb.events().key_char( [&]() {
    auto val = sb.to_int();
        auto strVal = sb.value();
    cout << val << ": " << strVal << endl;
    });
    fm["spin"] << sb;

    fm.collocate();

    fm.show();
    exec();
    return 0;
}

Note that the range for the spinbox is set to 1-99. When first displayed, the spinbox contains "1". If I edit the spinbox by pressing the "5" key, then "1: 1" is printed. Since the key_char event executes before the character is actually added to the spinbox edit box, everything is OK. Now the spinbox edit box contains "15" as it should. Now press the "5" key again. "15: 15" is output (still OK), but then the spinbox contains "155". Press the "5" key again. "15: 15" is again printed and the spinbox contains "1555".

So, to_int and value return the correct values, but the spinbox does not reflect the actual value ("15", not "1555").

This program was built using nana develop-1.8 branch, and gcc 9.3.0 on Ubuntu 20.04. I have not tried it on the main branch, or any other OS.

cnjinhao commented 3 years ago

Hi, @jimorc It's an expected behavior. An exceed value is allowed when edit the textbox by pressing keys, but the to_int&value always return the last correct value. When the textbox lose focus, it will correct the textbox and show the last correct value.

Consider a situation that range is 10-99. When the textbox is "15", how to input "16" by pressing key? Press backspace to delete "5", then textbox shows "1", if spinbox traces the allowable values, it rejects the backspace because "1" is out of the range. So we need the spinbox to accept an exceed value when it get editted.