Skip to content

Commit

Permalink
Issue #156: We should be able to take ranges of std::string for choic…
Browse files Browse the repository at this point in the history
…es, list boxes, etc
  • Loading branch information
rmpowell77 committed Nov 30, 2023
1 parent 900f77e commit ce0bf68
Show file tree
Hide file tree
Showing 31 changed files with 654 additions and 196 deletions.
1 change: 1 addition & 0 deletions LATEST_RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Bugs addressed in this release:
Other changes:

* [#144](../../issues/144) RadioButtons are too easy to lay out incorrectly because of withStyle
* [#156](../../issues/156) We should be able to take ranges of std::string for choices, list boxes, etc
* [#157](../../issues/157) Text needs withWrap

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
Text { "GCD = " },
result = Text { "1" },
},
RadioBox { "&Log Levels:", { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
RadioBox { "&Log Levels:", RadioBox::withChoices {}, { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
.setStyle(wxRA_SPECIFY_ROWS)
.withMajorDim(1)
.withSelection(1),

HSizer {
"Details",
CheckBox { "Show" },
Choice { { "Less", "More" } },
Choice { "Less", "More" },
TextCtrl { wxSizerFlags(1).Expand().Border(), "Fill in the blank" }
.withStyle(wxALIGN_LEFT),
},
Expand Down
4 changes: 2 additions & 2 deletions docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Essentially, you supply a object that converts to `wxSizer*` or `wxWindow*`, or
HSizer {
"Details",
CheckBox { "Show" },
Choice { { "Less", "More" } },
Choice { "Less", "More" },
TextCtrl { wxSizerFlags(1).Expand().Border(), "Fill in the blank" }
.withStyle(wxALIGN_LEFT),
},
Expand Down Expand Up @@ -334,6 +334,6 @@ An example of how to use could be as follows:
#### 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.
`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. In addition, because `RadioBox` can take in a string as a "caption", a key-value is necessary to prevent `char`-arrays from being interpreted as `initializer_list<std::string>`.
`Button` and `BitmapButton` support the `setDefault` function which allows you to set them as the default button.
2 changes: 1 addition & 1 deletion docs/src/docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,6 @@ An example of how to use could be as follows:
#### 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.
`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. In addition, because `RadioBox` can take in a string as a "caption", a key-value is necessary to prevent `char`-arrays from being interpreted as `initializer_list<std::string>`.
`Button` and `BitmapButton` support the `setDefault` function which allows you to set them as the default button.
6 changes: 3 additions & 3 deletions examples/HelloWorld/ExtendedExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ExtendedExample::ExtendedExample(wxWindow* parent)
BitmapButton { wxBitmap {} },
},
HSizer {
BitmapComboBox { { std::tuple { "", wxBitmap {} } } },
BitmapComboBox { std::tuple { "", wxBitmap {} } },
},
HSizer {
BitmapToggleButton { wxBitmap {} },
Expand Down Expand Up @@ -81,10 +81,10 @@ ExtendedExample::ExtendedExample(wxWindow* parent)
Line {},
},
HSizer {
ListBox { {} },
ListBox {},
},
HSizer {
RadioBox { std::vector<wxString> { "hello" } },
RadioBox { RadioBox::withChoices {}, "hello" },
},
HSizer {
Slider {},
Expand Down
4 changes: 2 additions & 2 deletions examples/HelloWorld/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
result = Text { "1" },
},
// endsnippet withwxUI
RadioBox { "&Log Levels:", { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
RadioBox { "&Log Levels:", RadioBox::withChoices {}, { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
.setStyle(wxRA_SPECIFY_ROWS)
.withMajorDim(1)
.withSelection(1),
Expand All @@ -320,7 +320,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
HSizer {
"Details",
CheckBox { "Show" },
Choice { { "Less", "More" } },
Choice { "Less", "More" },
TextCtrl { wxSizerFlags(1).Expand().Border(), "Fill in the blank" }
.withStyle(wxALIGN_LEFT),
},
Expand Down
14 changes: 7 additions & 7 deletions include/wxUI/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ namespace wxUI {
struct Bitmap : public details::WidgetDetails<Bitmap, wxStaticBitmap> {
using super = details::WidgetDetails<Bitmap, wxStaticBitmap>;

explicit Bitmap(wxBitmap const& bitmap)
: Bitmap(wxID_ANY, bitmap)
{
}

Bitmap(wxWindowID identity, wxBitmap const& bitmap)
: super(identity)
, bitmap(bitmap)
{
}

explicit Bitmap(wxBitmap const& bitmap)
: Bitmap(wxID_ANY, bitmap)
explicit Bitmap(wxSizerFlags const& flags, wxBitmap const& bitmap)
: Bitmap(flags, wxID_ANY, bitmap)
{
}

Expand All @@ -51,11 +56,6 @@ struct Bitmap : public details::WidgetDetails<Bitmap, wxStaticBitmap> {
{
}

explicit Bitmap(wxSizerFlags const& flags, wxBitmap const& bitmap)
: Bitmap(flags, wxID_ANY, bitmap)
{
}

struct Proxy : super::WidgetProxy {
PROXY_BOILERPLATE();
};
Expand Down
14 changes: 7 additions & 7 deletions include/wxUI/BitmapButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,19 @@ namespace wxUI {
struct BitmapButton : public details::WidgetDetails<BitmapButton, wxBitmapButton> {
using super = details::WidgetDetails<BitmapButton, wxBitmapButton>;

explicit BitmapButton(wxBitmap const& bitmap)
: BitmapButton(wxID_ANY, bitmap)
{
}

BitmapButton(wxWindowID identity, wxBitmap const& bitmap)
: super(identity)
, bitmap(bitmap)
{
}

explicit BitmapButton(wxBitmap const& bitmap)
: BitmapButton(wxID_ANY, bitmap)
BitmapButton(wxSizerFlags const& flags, wxBitmap const& bitmap)
: BitmapButton(flags, wxID_ANY, bitmap)
{
}

Expand All @@ -51,11 +56,6 @@ struct BitmapButton : public details::WidgetDetails<BitmapButton, wxBitmapButton
{
}

BitmapButton(wxSizerFlags const& flags, wxBitmap const& bitmap)
: BitmapButton(flags, wxID_ANY, bitmap)
{
}

auto setDefault() -> BitmapButton&
{
isDefault = true;
Expand Down
37 changes: 31 additions & 6 deletions include/wxUI/BitmapComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ SOFTWARE.

#include "GetterSetter.h"
#include "Widget.h"
#include <ranges>
#include <wx/bmpcbox.h>

#include "HelperMacros.h"
Expand All @@ -35,7 +36,12 @@ 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)
explicit BitmapComboBox(std::initializer_list<std::tuple<std::string, wxBitmap>> bitmapChoices)
: BitmapComboBox(wxID_ANY, bitmapChoices)
{
}

BitmapComboBox(wxWindowID identity, std::initializer_list<std::tuple<std::string, wxBitmap>> bitmapChoices)
: super(identity)
, choices([&bitmapChoices] {
std::vector<wxString> result;
Expand All @@ -56,12 +62,12 @@ struct BitmapComboBox : public details::WidgetDetails<BitmapComboBox, wxBitmapCo
{
}

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

BitmapComboBox(wxSizerFlags const& flags, wxWindowID identity, std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
BitmapComboBox(wxSizerFlags const& flags, wxWindowID identity, std::initializer_list<std::tuple<std::string, wxBitmap>> bitmapChoices)
: super(flags, identity)
, choices([&bitmapChoices] {
std::vector<wxString> result;
Expand All @@ -82,8 +88,27 @@ struct BitmapComboBox : public details::WidgetDetails<BitmapComboBox, wxBitmapCo
{
}

BitmapComboBox(wxSizerFlags const& flags, std::vector<std::tuple<wxString, wxBitmap>> const& bitmapChoices)
: BitmapComboBox(flags, wxID_ANY, bitmapChoices)
explicit BitmapComboBox(details::Ranges::input_range_of<std::tuple<wxString, wxBitmap>> auto&& choices)
: BitmapComboBox(wxID_ANY, std::forward<decltype(choices)>(choices))
{
}

BitmapComboBox(wxWindowID identity, details::Ranges::input_range_of<std::tuple<wxString, wxBitmap>> auto&& choices)
: super(identity)
, choices(details::Ranges::ToVector<wxString>(choices | std::views::transform([](auto&& item) { return std::get<0>(item); })))
, bitmaps(details::Ranges::ToVector<wxBitmap>(choices | std::views::transform([](auto&& item) { return std::get<1>(item); })))
{
}

BitmapComboBox(wxSizerFlags const& flags, details::Ranges::input_range_of<std::tuple<wxString, wxBitmap>> auto&& choices)
: BitmapComboBox(flags, wxID_ANY, std::forward<decltype(choices)>(choices))
{
}

BitmapComboBox(wxSizerFlags const& flags, wxWindowID identity, details::Ranges::input_range_of<std::tuple<wxString, wxBitmap>> auto&& choices)
: super(flags, identity)
, choices(details::Ranges::ToVector<wxString>(choices | std::views::transform([](auto&& item) { return std::get<0>(item); })))
, bitmaps(details::Ranges::ToVector<wxBitmap>(choices | std::views::transform([](auto&& item) { return std::get<1>(item); })))
{
}

Expand Down
14 changes: 7 additions & 7 deletions include/wxUI/BitmapToggleButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ namespace wxUI {
struct BitmapToggleButton : public details::WidgetDetails<BitmapToggleButton, wxBitmapToggleButton> {
using super = details::WidgetDetails<BitmapToggleButton, wxBitmapToggleButton>;

explicit BitmapToggleButton(wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: BitmapToggleButton(wxID_ANY, bitmap, std::move(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, wxBitmap const& bitmap, std::optional<wxBitmap> bitmapPressed = {})
: BitmapToggleButton(flags, wxID_ANY, bitmap, std::move(bitmapPressed))
{
}

Expand All @@ -54,11 +59,6 @@ struct BitmapToggleButton : public details::WidgetDetails<BitmapToggleButton, wx
{
}

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

using super::bind;
template <typename Function>
auto bind(Function func)
Expand Down
16 changes: 8 additions & 8 deletions include/wxUI/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ struct Button : public details::WidgetDetails<Button, wxButton> {
using super = details::WidgetDetails<Button, wxButton>;
using details::WidgetDetails<Button, wxButton>::underlying_t;

explicit Button(wxWindowID identity, std::string text = "")
: super(identity)
, text(std::move(text))
{
}

explicit Button(std::string text = "")
: Button(wxID_ANY, std::move(text))
{
}

explicit Button(wxSizerFlags const& flags, wxWindowID identity, std::string text = "")
: super(flags, identity)
explicit Button(wxWindowID identity, std::string text = "")
: super(identity)
, text(std::move(text))
{
}
Expand All @@ -57,6 +51,12 @@ struct Button : public details::WidgetDetails<Button, wxButton> {
{
}

Button(wxSizerFlags const& flags, wxWindowID identity, std::string text = "")
: super(flags, identity)
, text(std::move(text))
{
}

auto setDefault() -> Button&
{
isDefault = true;
Expand Down
16 changes: 8 additions & 8 deletions include/wxUI/CheckBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,13 @@ namespace wxUI {
struct CheckBox : public details::WidgetDetails<CheckBox, wxCheckBox> {
using super = details::WidgetDetails<CheckBox, wxCheckBox>;

explicit CheckBox(wxWindowID identity, std::string text = "")
: super(identity)
, text(std::move(text))
{
}

explicit CheckBox(std::string text = "")
: CheckBox(wxID_ANY, std::move(text))
{
}

explicit CheckBox(wxSizerFlags const& flags, wxWindowID identity, std::string text = "")
: super(flags, identity)
explicit CheckBox(wxWindowID identity, std::string text = "")
: super(identity)
, text(std::move(text))
{
}
Expand All @@ -57,6 +51,12 @@ struct CheckBox : public details::WidgetDetails<CheckBox, wxCheckBox> {
{
}

CheckBox(wxSizerFlags const& flags, wxWindowID identity, std::string text = "")
: super(flags, identity)
, text(std::move(text))
{
}

auto createImpl(wxWindow* parent) -> wxWindow* override
{
auto* widget = setProxy(new underlying_t(parent, getIdentity(), text, getPos(), getSize(), getStyle()));
Expand Down
38 changes: 30 additions & 8 deletions include/wxUI/Choice.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,47 @@ namespace wxUI {
struct Choice : public details::WidgetDetails<Choice, wxChoice> {
using super = details::WidgetDetails<Choice, wxChoice>;

explicit Choice(wxWindowID identity, std::vector<wxString> choices = {})
Choice(std::initializer_list<std::string> choices = {})
: Choice(wxID_ANY, choices)
{
}

explicit Choice(wxWindowID identity, std::initializer_list<std::string> choices = {})
: super(identity)
, choices(std::move(choices))
, choices(details::Ranges::convertTo(choices))
{
}

explicit Choice(std::vector<wxString> choices = {})
: Choice(wxID_ANY, std::move(choices))
explicit Choice(wxSizerFlags const& flags, std::initializer_list<std::string> choices = {})
: Choice(flags, wxID_ANY, choices)
{
}

explicit Choice(wxSizerFlags const& flags, wxWindowID identity, std::vector<wxString> choices = {})
Choice(wxSizerFlags const& flags, wxWindowID identity, std::initializer_list<std::string> choices = {})
: super(flags, identity)
, choices(std::move(choices))
, choices(details::Ranges::convertTo(choices))
{
}

explicit Choice(details::Ranges::input_range_of<wxString> auto&& choices)
: Choice(wxID_ANY, std::forward<decltype(choices)>(choices))
{
}

Choice(wxWindowID identity, details::Ranges::input_range_of<wxString> auto&& choices)
: super(identity)
, choices(details::Ranges::ToVector<wxString>(std::forward<decltype(choices)>(choices)))
{
}

explicit Choice(wxSizerFlags const& flags, std::vector<wxString> choices = {})
: Choice(flags, wxID_ANY, std::move(choices))
explicit Choice(wxSizerFlags const& flags, details::Ranges::input_range_of<wxString> auto&& choices)
: Choice(flags, wxID_ANY, std::forward<decltype(choices)>(choices))
{
}

explicit Choice(wxSizerFlags const& flags, wxWindowID identity, details::Ranges::input_range_of<wxString> auto&& choices)
: super(flags, identity)
, choices(details::Ranges::ToVector<wxString>(choices))
{
}

Expand Down
Loading

0 comments on commit ce0bf68

Please sign in to comment.