Skip to content

Commit

Permalink
Merge pull request #78 from rmpowell77/dev/combobox
Browse files Browse the repository at this point in the history
Issue #76: add wxBitmapComboBox
  • Loading branch information
rmpowell77 authored Feb 4, 2023
2 parents d412399 + 5ff463d commit 8ad5f91
Show file tree
Hide file tree
Showing 10 changed files with 411 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ add_library(
INTERFACE
include/wxUI/wxUI.h
include/wxUI/BitmapToggleButton.h
include/wxUI/BitmapComboBox.h
include/wxUI/BitmapButton.h
include/wxUI/Button.h
include/wxUI/CheckBox.h
include/wxUI/Choice.h
include/wxUI/ComboBox.h
include/wxUI/Custom.h
include/wxUI/Layout.h
include/wxUI/Line.h
Expand Down
1 change: 1 addition & 0 deletions LATEST_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Other changes:
* [#70](../../issues/70) Widget::style() is odd, it should be withStyle()
* [#71](../../issues/71) remove wxUI::details::Custom
* [#72](../../issues/72) consider making WidgetDetails member values private
* [#76](../../issues/76) add wxBitmapComboBox

2 changes: 2 additions & 0 deletions docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,12 @@ The list of Methods supported by all controllers:
The "Controllers" currently supported by `wxUI`:

* `BitmapButton` for `wxBitmapButton`
* `BitmapComboBox` for `wxBitmapComboBox`
* `BitmapToggleButton` for `wxBitmapToggleButton`
* `Button` for `wxButton`
* `CheckBox` for `wxCheckBox`
* `Choice` for `wxChoice`
* `ComboBox` for `ComboBox`
* `ListBox` for `wxListBox`
* `Line` for `wxStaticLine`
* `RadioBox` for `wxRadioBox`
Expand Down
2 changes: 2 additions & 0 deletions docs/src/docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,12 @@ The list of Methods supported by all controllers:
The "Controllers" currently supported by `wxUI`:

* `BitmapButton` for `wxBitmapButton`
* `BitmapComboBox` for `wxBitmapComboBox`
* `BitmapToggleButton` for `wxBitmapToggleButton`
* `Button` for `wxButton`
* `CheckBox` for `wxCheckBox`
* `Choice` for `wxChoice`
* `ComboBox` for `ComboBox`
* `ListBox` for `wxListBox`
* `Line` for `wxStaticLine`
* `RadioBox` for `wxRadioBox`
Expand Down
122 changes: 122 additions & 0 deletions include/wxUI/BitmapComboBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
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/bmpcbox.h>

namespace wxUI {

struct BitmapComboBox : public details::WidgetDetails<BitmapComboBox, wxBitmapComboBox> {
using super = details::WidgetDetails<BitmapComboBox, wxBitmapComboBox>;

BitmapComboBox(wxWindowID identity, std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
: super(identity)
, choices([&bitmapChoices] {
std::vector<wxString> result;
result.reserve(bitmapChoices.size());
for (auto&& [first, _] : bitmapChoices) {
result.push_back(first);
}
return result;
}())
, bitmaps([&bitmapChoices] {
std::vector<wxBitmap> result;
result.reserve(bitmapChoices.size());
for (auto&& [_, second] : bitmapChoices) {
result.push_back(second);
}
return result;
}())
{
}

explicit BitmapComboBox(std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
: BitmapComboBox(wxID_ANY, bitmapChoices)
{
}

BitmapComboBox(wxSizerFlags const& flags, wxWindowID identity, std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
: super(flags, identity)
, choices([&bitmapChoices] {
std::vector<wxString> result;
result.reserve(bitmapChoices.size());
for (auto&& [first, _] : bitmapChoices) {
result.push_back(first);
}
return result;
}())
, bitmaps([&bitmapChoices] {
std::vector<wxBitmap> result;
result.reserve(bitmapChoices.size());
for (auto&& [_, second] : bitmapChoices) {
result.push_back(second);
}
return result;
}())
{
}

BitmapComboBox(wxSizerFlags const& flags, std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
: BitmapComboBox(flags, wxID_ANY, bitmapChoices)
{
}

auto create(wxWindow* parent) -> wxWindow* override
{
auto&& first = (choices.size() > 0) ? wxString(choices.at(0)) : wxString(wxEmptyString);
auto* widget = new underlying_t(parent, getIdentity(), first, getPos(), getSize(), static_cast<int>(choices.size()), choices.data(), getStyle());
for (auto i = 0lu; i < bitmaps.size(); ++i) {
widget->SetItemBitmap(i, bitmaps[i]);
}
widget->SetSelection(selection);
return widget;
}

auto withSelection(int which) -> BitmapComboBox&
{
selection = which;
return *this;
}

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

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

private:
std::vector<wxString> choices;
std::vector<wxBitmap> bitmaps;
int selection = 0;
};

static_assert(details::Widget<BitmapComboBox>);
}
88 changes: 88 additions & 0 deletions include/wxUI/ComboBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
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/combobox.h>

namespace wxUI {

struct ComboBox : public details::WidgetDetails<ComboBox, wxComboBox> {
using super = details::WidgetDetails<ComboBox, wxComboBox>;

ComboBox(wxWindowID identity, std::vector<wxString> const& choices)
: super(identity)
, choices(choices)
{
}

explicit ComboBox(std::vector<wxString> const& choices)
: ComboBox(wxID_ANY, choices)
{
}

ComboBox(wxSizerFlags const& flags, wxWindowID identity, std::vector<wxString> const& choices)
: super(flags, identity)
, choices(choices)
{
}

ComboBox(wxSizerFlags const& flags, std::vector<wxString> const& choices)
: ComboBox(flags, wxID_ANY, choices)
{
}

auto create(wxWindow* parent) -> wxWindow* override
{
auto&& first = (choices.size() > 0) ? wxString(choices.at(0)) : wxString(wxEmptyString);
auto* widget = new underlying_t(parent, getIdentity(), first, getPos(), getSize(), static_cast<int>(choices.size()), choices.data(), getStyle());
widget->SetSelection(selection);
return widget;
}

auto withSelection(int which) -> ComboBox&
{
selection = which;
return *this;
}

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

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

private:
std::vector<wxString> choices;
int selection = 0;
};

static_assert(details::Widget<ComboBox>);
}
2 changes: 2 additions & 0 deletions include/wxUI/wxUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
// SOFTWARE.

#include <wxUI/BitmapButton.h>
#include <wxUI/BitmapComboBox.h>
#include <wxUI/BitmapToggleButton.h>
#include <wxUI/Button.h>
#include <wxUI/CheckBox.h>
#include <wxUI/Choice.h>
#include <wxUI/ComboBox.h>
#include <wxUI/Custom.h>
#include <wxUI/Layout.h>
#include <wxUI/Line.h>
Expand Down
2 changes: 2 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ cmake_minimum_required(VERSION 3.23)

add_executable(wxUI_Tests
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_BitmapButtonTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_BitmapComboBox.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
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_ComboBox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_LayoutTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_LineTests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wxUI_ListBoxTests.cpp
Expand Down
95 changes: 95 additions & 0 deletions tests/wxUI_BitmapComboBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
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/BitmapComboBox.h>

#include <wx/wx.h>

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

SECTION("pos")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { { { wxString {}, wxBitmap {} } } }.withPosition({ 1, 2 });
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetPosition() == wxPoint { 1, 2 });
}

SECTION("size")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { { { wxString {}, wxBitmap {} } } }.withSize({ 1, 2 });
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(window->GetSize() == wxSize { 1, 2 });
}

SECTION("setSelection")
{
wxFrame frame { nullptr, wxID_ANY, "" };
auto uut = TypeUnderTest { wxSizerFlags(1), 10000, { { "Hello", wxBitmap {} }, { "Goodbye", wxBitmap {} } } }.withSize({ 1, 2 }).withSelection(1);
auto* window = dynamic_cast<TypeUnderTest::underlying_t*>(uut.create(&frame));
CHECK(10000 == window->GetId());
CHECK(2 == window->GetCount());
CHECK(1 == window->GetSelection());
CHECK("Hello" == window->GetString(0));
CHECK("Goodbye" == window->GetString(1));
}
}
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers, readability-magic-numbers, readability-function-cognitive-complexity)
Loading

0 comments on commit 8ad5f91

Please sign in to comment.