Skip to content

Commit

Permalink
Merge pull request #53 from rmpowell77/dev/button-setdefault
Browse files Browse the repository at this point in the history
Issue #25: Need a way to set the the default Button
  • Loading branch information
rmpowell77 authored Jan 2, 2023
2 parents 83821fa + 8591324 commit 160d172
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 2 additions & 0 deletions docs/src/docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
13 changes: 12 additions & 1 deletion include/wxUI/BitmapButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct BitmapButton : public details::WidgetDetails<BitmapButton, wxBitmapButton
using super = details::WidgetDetails<BitmapButton, wxBitmapButton>;

wxBitmap bitmap;
bool isDefault = false;

BitmapButton(wxWindowID identity, wxBitmap const& bitmap)
: super(identity)
Expand All @@ -57,7 +58,17 @@ struct BitmapButton : public details::WidgetDetails<BitmapButton, wxBitmapButton

auto create(wxWindow* parent) -> 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 <typename Function>
Expand Down
13 changes: 12 additions & 1 deletion include/wxUI/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct Button : public details::WidgetDetails<Button, wxButton> {
using super = details::WidgetDetails<Button, wxButton>;

std::string text;
bool isDefault = false;

explicit Button(wxWindowID identity, std::string text = "")
: super(identity)
Expand All @@ -57,7 +58,17 @@ struct Button : public details::WidgetDetails<Button, wxButton> {

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 <typename Function>
Expand Down
2 changes: 1 addition & 1 deletion include/wxUI/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ struct WidgetDetails {
return static_cast<ConcreteWidget&>(*this);
}

auto getHandle([[maybe_unused]] Underlying** handle) -> ConcreteWidget&
auto getHandle(Underlying** handle) -> ConcreteWidget&
{
windowHandle = handle;
return static_cast<ConcreteWidget&>(*this);
Expand Down
14 changes: 14 additions & 0 deletions tests/wxUI_BitmapButtonTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
13 changes: 13 additions & 0 deletions tests/wxUI_ButtonTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 160d172

Please sign in to comment.