Don't disable wxStaticBox window label when disabling the box

This behaviour might be not completely intuitive, but it makes it much
simpler to handle the box state using a checkbox as the label control
(which is by far the most common case of using box window labels).

Notice that while we could add a separate EnableWithoutLabel() method to
wxStaticBox to make it possible to set the state of the box directly
relatively easily, it wouldn't help with using wxEVT_UPDATE_UI for
managing the box state indirectly as it relies on calling Enable() only.
And this solution does allow wxEVT_UPDATE_UI handlers for the box itself
to work (provided the handler takes care to check for the event object
being the box itself, as otherwise it would still disable the child
checkbox when its wxEVT_UPDATE_UI bubbles up to the box).
This commit is contained in:
Vadim Zeitlin
2018-01-07 01:14:17 +01:00
parent d332ccfd6f
commit 1d037dd4c9
3 changed files with 66 additions and 0 deletions

View File

@@ -158,4 +158,30 @@ public:
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxStaticBoxNameStr);
/**
Enables or disables the box without affecting its label window, if any.
wxStaticBox overrides wxWindow::Enable() in order to avoid disabling
the control used as a label, if this box is using one. This is done in
order to allow using a wxCheckBox, for example, label and enable or
disable the box according to the state of the checkbox: if disabling
the box also disabled the checkbox in this situation, it would make it
impossible for the user to re-enable the box after disabling it, so the
checkbox stays enabled even if @c box->Enable(false) is called.
However with the actual behaviour, implemented in this overridden
method, the following code (shown using C++11 only for convenience,
this behaviour is not C++11-specific):
@code
auto check = new wxCheckBox(parent, wxID_ANY, "Use the box");
auto box = new wxStaticBox(parent, wxID_ANY, check);
check->Bind(wxEVT_CHECKBOX,
[box](wxCommandEvent& event) {
box->Enable(event.IsChecked());
});
@endcode
does work as expected.
*/
virtual bool Enable(bool enable = true);
};