diff --git a/docs/ProgrammersGuide.md b/docs/ProgrammersGuide.md index c87303c..aeb51d5 100644 --- a/docs/ProgrammersGuide.md +++ b/docs/ProgrammersGuide.md @@ -241,3 +241,5 @@ You would then create the controller to confomr #### Misc notes. `wxRadioBox` requires a list of strings to operate correctly, so `RadioBox` requires a `std::vector` of strings. Note, you *can* provide an empty `std::vector`, but a crash may occur if you do so. + +`Button` and `BitmapButton` support the `setDefault` function which allows you to set them as the default button. diff --git a/docs/src/docs/ProgrammersGuide.md b/docs/src/docs/ProgrammersGuide.md index 6c64554..c6d3a03 100644 --- a/docs/src/docs/ProgrammersGuide.md +++ b/docs/src/docs/ProgrammersGuide.md @@ -180,3 +180,5 @@ You would then create the controller to confomr #### Misc notes. `wxRadioBox` requires a list of strings to operate correctly, so `RadioBox` requires a `std::vector` of strings. Note, you *can* provide an empty `std::vector`, but a crash may occur if you do so. + +`Button` and `BitmapButton` support the `setDefault` function which allows you to set them as the default button. diff --git a/include/wxUI/BitmapButton.h b/include/wxUI/BitmapButton.h index ad73d98..92b5799 100644 --- a/include/wxUI/BitmapButton.h +++ b/include/wxUI/BitmapButton.h @@ -32,6 +32,7 @@ struct BitmapButton : public details::WidgetDetails; wxBitmap bitmap; + bool isDefault = false; BitmapButton(wxWindowID identity, wxBitmap const& bitmap) : super(identity) @@ -57,7 +58,17 @@ struct BitmapButton : public details::WidgetDetails wxWindow* override { - return new underlying_t(parent, identity, bitmap, wxDefaultPosition, wxDefaultSize); + auto* widget = new underlying_t(parent, identity, bitmap, wxDefaultPosition, wxDefaultSize); + if (isDefault) { + widget->SetDefault(); + } + return widget; + } + + auto setDefault() -> BitmapButton& + { + isDefault = true; + return *this; } template diff --git a/include/wxUI/Button.h b/include/wxUI/Button.h index 2d3262a..c44c1a5 100644 --- a/include/wxUI/Button.h +++ b/include/wxUI/Button.h @@ -32,6 +32,7 @@ struct Button : public details::WidgetDetails { using super = details::WidgetDetails; std::string text; + bool isDefault = false; explicit Button(wxWindowID identity, std::string text = "") : super(identity) @@ -57,7 +58,17 @@ struct Button : public details::WidgetDetails { auto create(wxWindow* parent) -> wxWindow* override { - return new underlying_t(parent, identity, text, wxDefaultPosition, wxDefaultSize); + auto* widget = new underlying_t(parent, identity, text, wxDefaultPosition, wxDefaultSize); + if (isDefault) { + widget->SetDefault(); + } + return widget; + } + + auto setDefault() -> Button& + { + isDefault = true; + return *this; } template diff --git a/include/wxUI/Widget.h b/include/wxUI/Widget.h index 096e187..260df91 100644 --- a/include/wxUI/Widget.h +++ b/include/wxUI/Widget.h @@ -129,7 +129,7 @@ struct WidgetDetails { return static_cast(*this); } - auto getHandle([[maybe_unused]] Underlying** handle) -> ConcreteWidget& + auto getHandle(Underlying** handle) -> ConcreteWidget& { windowHandle = handle; return static_cast(*this); diff --git a/tests/wxUI_BitmapButtonTests.cpp b/tests/wxUI_BitmapButtonTests.cpp index 7617eab..da6b464 100644 --- a/tests/wxUI_BitmapButtonTests.cpp +++ b/tests/wxUI_BitmapButtonTests.cpp @@ -63,5 +63,19 @@ TEST_CASE("BitmapButton") CHECK(10000 == window->GetId()); CHECK(window->GetLabel().empty()); } + + SECTION("setDefault.false") + { + wxFrame frame { nullptr, wxID_ANY, "" }; + auto uut = TypeUnderTest { wxSizerFlags(1), 10000, wxBitmap {} }; + CHECK(!uut.isDefault); + } + + SECTION("setDefault.true") + { + wxFrame frame { nullptr, wxID_ANY, "" }; + auto uut = TypeUnderTest { wxSizerFlags(1), 10000, wxBitmap {} }.setDefault(); + CHECK(uut.isDefault); + } } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity) diff --git a/tests/wxUI_ButtonTests.cpp b/tests/wxUI_ButtonTests.cpp index fb2e57c..9eaead2 100644 --- a/tests/wxUI_ButtonTests.cpp +++ b/tests/wxUI_ButtonTests.cpp @@ -105,5 +105,18 @@ TEST_CASE("Button") CHECK(10000 == window->GetId()); CHECK("Hello" == window->GetLabel()); } + SECTION("setDefault.false") + { + wxFrame frame { nullptr, wxID_ANY, "" }; + auto uut = TypeUnderTest { wxSizerFlags(1), 10000, "Hello" }; + CHECK(!uut.isDefault); + } + + SECTION("setDefault.true") + { + wxFrame frame { nullptr, wxID_ANY, "" }; + auto uut = TypeUnderTest { wxSizerFlags(1), 10000, "Hello" }.setDefault(); + CHECK(uut.isDefault); + } } // NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity)