Skip to content

Commit

Permalink
Merge pull request #60 from rmpowell77/dev/bitmaptogglebutton
Browse files Browse the repository at this point in the history
Issue #59: need BitmapToggleButton
  • Loading branch information
rmpowell77 authored Jan 3, 2023
2 parents 46e3ae2 + a30c12a commit 6e84bcc
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_library(
wxUI
INTERFACE
include/wxUI/wxUI.h
include/wxUI/BitmapToggleButton.h
include/wxUI/BitmapButton.h
include/wxUI/Button.h
include/wxUI/CheckBox.h
Expand Down
1 change: 1 addition & 0 deletions docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ The list of Methods supported by all controllers:
The "Controllers" currently supported by `wxUI`:

* `BitmapButton` for `wxBitmapButton`
* `BitmapToggleButton` for `wxBitmapToggleButton`
* `Button` for `wxButton`
* `CheckBox` for `wxCheckBox`
* `Choice` for `wxChoice`
Expand Down
1 change: 1 addition & 0 deletions docs/src/docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ The list of Methods supported by all controllers:
The "Controllers" currently supported by `wxUI`:

* `BitmapButton` for `wxBitmapButton`
* `BitmapToggleButton` for `wxBitmapToggleButton`
* `Button` for `wxButton`
* `CheckBox` for `wxCheckBox`
* `Choice` for `wxChoice`
Expand Down
3 changes: 3 additions & 0 deletions examples/HelloWorld/ExtendedExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ ExtendedExample::ExtendedExample(wxWindow* parent)
HStack {
BitmapButton { wxBitmap {} },
},
HStack {
BitmapToggleButton { wxBitmap {} },
},
HStack {
Button {},
},
Expand Down
84 changes: 84 additions & 0 deletions include/wxUI/BitmapToggleButton.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
MIT License
Copyright (c) 2022 Richard Powell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once

#include "Widget.h"
#include <wx/tglbtn.h>

namespace wxUI {

struct BitmapToggleButton : public details::WidgetDetails<BitmapToggleButton, wxBitmapToggleButton> {
using super = details::WidgetDetails<BitmapToggleButton, wxBitmapToggleButton>;

wxBitmap bitmap;
std::optional<wxBitmap> bitmapPressed;

BitmapToggleButton(wxWindowID identity, wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: super(identity)
, bitmap(bitmap)
, bitmapPressed(std::move(bitmapPressed))
{
}

explicit BitmapToggleButton(wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: BitmapToggleButton(wxID_ANY, bitmap, std::move(bitmapPressed))
{
}

BitmapToggleButton(wxSizerFlags const& flags, wxWindowID identity, wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: super(flags, identity)
, bitmap(bitmap)
, bitmapPressed(std::move(bitmapPressed))
{
}

BitmapToggleButton(wxSizerFlags const& flags, wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: BitmapToggleButton(flags, wxID_ANY, bitmap, std::move(bitmapPressed))
{
}

auto create(wxWindow* parent) -> wxWindow* override
{
auto* widget = new underlying_t(parent, identity, bitmap, wxDefaultPosition, wxDefaultSize);
if (bitmapPressed) {
widget->SetBitmapPressed(*bitmapPressed);
}
return widget;
}

template <typename Function>
auto bind(Function func)
{
return details::BindWidgetToEvent { *this, wxEVT_TOGGLEBUTTON, func };
}

virtual ~BitmapToggleButton() = default;
BitmapToggleButton(BitmapToggleButton const&) = default;
BitmapToggleButton(BitmapToggleButton&&) = default;
auto operator=(BitmapToggleButton const&) -> BitmapToggleButton& = default;
auto operator=(BitmapToggleButton&&) -> BitmapToggleButton& = default;
};

static_assert(details::Widget<BitmapToggleButton>);
}
1 change: 1 addition & 0 deletions include/wxUI/wxUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
// SOFTWARE.

#include <wxUI/BitmapButton.h>
#include <wxUI/BitmapToggleButton.h>
#include <wxUI/Button.h>
#include <wxUI/CheckBox.h>
#include <wxUI/Choice.h>
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cmake_minimum_required(VERSION 3.23)

add_executable(wxUI_Tests
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_BitmapButtonTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_BitmapToggleButtonTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_ButtonTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_CheckBoxTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_ChoiceTests.cpp
Expand Down
101 changes: 101 additions & 0 deletions tests/wxUI_BitmapToggleButtonTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
MIT License
Copyright (c) 2022 Richard Powell
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <catch2/catch_test_macros.hpp>
#include <wxUI/BitmapToggleButton.h>

#include <wx/wx.h>

// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity)
using TypeUnderTest = wxUI::BitmapToggleButton;
TEST_CASE("BitmapToggleButton")
{
SECTION("bitmap")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetLabel().empty());
}

SECTION("bitmap.toggle")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxBitmap {}, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetLabel().empty());
}

SECTION("id.bitmap")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { 10000, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(10000 == window->GetId());
CHECK(window->GetLabel().empty());
}

SECTION("id.bitmap.toggle")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { 10000, wxBitmap {}, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(10000 == window->GetId());
CHECK(window->GetLabel().empty());
}

SECTION("size.bitmap")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxSizerFlags(1), wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetLabel().empty());
}

SECTION("size.bitmap.toggle")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxSizerFlags(1), wxBitmap {}, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetLabel().empty());
}

SECTION("size.id.bitmap")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxSizerFlags(1), 10000, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(10000 == window->GetId());
CHECK(window->GetLabel().empty());
}

SECTION("size.id.bitmap.toggle")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxSizerFlags(1), 10000, wxBitmap {}, wxBitmap {} };
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(10000 == window->GetId());
CHECK(window->GetLabel().empty());
}
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity)

0 comments on commit 6e84bcc

Please sign in to comment.