Getting checkbox and radio button state

There are two ways to get the current state of a checkbox or a radio button.

size_t n=button->get_state();

get_state() returns the current state of the checkbox or the radio button, directly. A turned off checkbox or a radio button returns 0. A turned on checkbox or a radio button returns 1, and a checkbox with an explicitly set intermediate state returns 2.

Note

Using get_state() immediately after an explicit set_state() may return the button's previous state. This is because the checkbox's or the radio button's state gets updated by LibCXX Widget Toolkit's internal execution thread, and that hasn't happened just yet. set_state() sends an internal message to the execution thread, which might be busy with other things. Once all other urgen matters get resolved, the execution thread will process the message and update the button's state.

on_activate installs a callback that gets executed by the internal thread whenever it updates the checkbox's or the radio button's state:

button->on_activate([]
                    (ONLY IN_THREAD,
                     size_t value,
                     const x::w::callback_trigger_t &trigger,
                     const x::w::busy &mcguffin)
                    {
                        if (trigger.index() == x::w::callback_trigger_initial)
                        {
                               .. ,,,
                        }
                    });

The lambda gets executed whenever the button changes its value. The lambda also gets executed immediately after it gets installed, and receives the checkbox's current state as the value. Its trigger parameter indicates the reason for lambda's execution. x::w::callback_trigger_t is a std::variant and the initial invocation of the variant specifies x::w::callback_trigger_initial.

Note

Because the lambda gets executed from the internal execution thread the lambda may get invoked before or after on_activate() returns.

Only one callback can be installed at a time. Installing a second callback replaces the first one, and the second callback also gets executed with the trigger indicating its initial invocation. Since callback execution and installation is asynchronous, it's possible for the first callback to get executed one last time after on_activate() gets called to install its replacement callback. This happens if the internal execution thread already started processing a button click. In all cases the first callback never gets called after the initial execution of the second callback; so this is a confirmation that the callback is replaced.

These callbacks get invoked to report new button values set directly, by pointer and button clicks; and manually with set_value(). Setting a new radio button results in callbacks for both the previously set and the newly set radio button getting invoked, in turn, reporting the change to the value of the previous radio button and the newly set one.