Skip to content

Commit

Permalink
Merge pull request #143 from rmpowell77/dev/generic_proxy
Browse files Browse the repository at this point in the history
Issue #142: Generic needs a proxy
  • Loading branch information
rmpowell77 authored Nov 20, 2023
2 parents aa4bda1 + 54baf02 commit 5d15712
Show file tree
Hide file tree
Showing 48 changed files with 826 additions and 552 deletions.
2 changes: 2 additions & 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/BindInfo.h
include/wxUI/Bitmap.h
include/wxUI/BitmapToggleButton.h
include/wxUI/BitmapComboBox.h
Expand All @@ -42,6 +43,7 @@ add_library(
include/wxUI/Choice.h
include/wxUI/ComboBox.h
include/wxUI/Custom.h
include/wxUI/Generic.h
include/wxUI/GetterSetter.h
include/wxUI/HelperMacros.h
include/wxUI/Hyperlink.h
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
},
RadioBox { "&Log Levels:", { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
.withStyle(wxRA_SPECIFY_ROWS)
.withoutStyle(wxRA_SPECIFY_COLS)
.withMajorDim(1)
.withSelection(1),

Expand All @@ -71,7 +72,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
.bind([](wxCommandEvent&) { wxLogMessage("Pressed Right"); }),
},

Generic { CreateStdDialogButtonSizer(wxOK) },
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
}
Expand Down
31 changes: 16 additions & 15 deletions docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ The basics of `wxUI` layout is the *Layout*. You use a specific type of *Layout

In the above example we have constructed a vertical layout sizer that will use a `wxSizer` with the `wxSizerFlags` set to expand with a default border. Then the first item in the sizer is a second layer sizer with horizontal layout. The `wxSizerFlags` are propogated to each layer so the horizontal layout in this example would also be set to expand with a default border. The second sizer would be created as a "named" box horizonal sizer.

A *Layout* takes a collection of objects, which can be either additional *Layout* (to create a tree of *Layouts*) or *Controllers*. Here is the general form of constructions for *Sizers*:
A *Layout* takes a collection of "Items", which can be either additional *Layout* (to create a tree of *Layouts*), *Controllers*, anything that is convertable `wxSizer*`. Here is the general form of constructions for *Sizers*:

```
Sizer { Items... }
Expand All @@ -163,7 +163,7 @@ wxUI::VSizer { "Current Frame" }.attachTo(this);

#### Generic

One special type of *Layout* is `Generic`. There are cases where you may have an existing layout as a `wxSizer*` (such as a common dialog) or `wxWindow*` (such as a custom window) that you wish to use with `wxUI`. Or you may have a existing window that requires a parent `wxWindow*` for construction. This is a case to use `Generic`:
One special type of *Layout* is `Generic`. There are cases where you have to construct a `wxWindow*` with a parent. This is a case to use `Generic`:

```cpp
VSizer {
Expand All @@ -172,7 +172,8 @@ One special type of *Layout* is `Generic`. There are cases where you may have a
[](wxWindow* window) {
return new wxButton(window, wxID_ANY, "Generic");
} },
Generic { CreateStdDialogButtonSizer(wxOK) },
// ...
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
```
Expand All @@ -191,17 +192,17 @@ Essentially, you supply a object that converts to `wxSizer*` or `wxWindow*`, or
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
HSplitter {
TextCtrl { "This is Right Top.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
TextCtrl { "This is Right Bottom.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
},
}
.withSize(wxSize(500, 400)),
Generic { CreateStdDialogButtonSizer(wxOK) },
rightUpper = TextCtrl { "This is Right Top.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
Button { "Incr" }
.bind([this]() {
auto original = std::string { *rightUpper } + "\nThis is Right Top.\n";
*rightUpper = original;
}),
} },
// ...
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
```
Expand Down Expand Up @@ -327,7 +328,7 @@ An example of how to use could be as follows:
},
},
},
Generic { CreateStdDialogButtonSizer(wxOK) },
CreateStdDialogButtonSizer(wxOK),
```
#### Misc notes.
Expand Down
4 changes: 2 additions & 2 deletions docs/src/docs/ProgrammersGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ The basics of `wxUI` layout is the *Layout*. You use a specific type of *Layout

In the above example we have constructed a vertical layout sizer that will use a `wxSizer` with the `wxSizerFlags` set to expand with a default border. Then the first item in the sizer is a second layer sizer with horizontal layout. The `wxSizerFlags` are propogated to each layer so the horizontal layout in this example would also be set to expand with a default border. The second sizer would be created as a "named" box horizonal sizer.

A *Layout* takes a collection of objects, which can be either additional *Layout* (to create a tree of *Layouts*) or *Controllers*. Here is the general form of constructions for *Sizers*:
A *Layout* takes a collection of "Items", which can be either additional *Layout* (to create a tree of *Layouts*), *Controllers*, anything that is convertable `wxSizer*`. Here is the general form of constructions for *Sizers*:

```
Sizer { Items... }
Expand All @@ -87,7 +87,7 @@ wxUI::VSizer { "Current Frame" }.attachTo(this);

#### Generic

One special type of *Layout* is `Generic`. There are cases where you may have an existing layout as a `wxSizer*` (such as a common dialog) or `wxWindow*` (such as a custom window) that you wish to use with `wxUI`. Or you may have a existing window that requires a parent `wxWindow*` for construction. This is a case to use `Generic`:
One special type of *Layout* is `Generic`. There are cases where you have to construct a `wxWindow*` with a parent. This is a case to use `Generic`:

```cpp
{{{ examples/HelloWorld/ExtendedExample.cpp GenericExample " // ..." }}}
Expand Down
55 changes: 42 additions & 13 deletions examples/HelloWorld/ExtendedExample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ ExtendedExample::ExtendedExample(wxWindow* parent)
},
},
},
Generic { CreateStdDialogButtonSizer(wxOK) },
CreateStdDialogButtonSizer(wxOK),
// endsnippet CustomExample
}
.attachTo(this);
Expand All @@ -143,7 +143,7 @@ MultibindExample::MultibindExample(wxWindow* parent)
timesTyped = Text { "0" },
timesEntered = Text { "0" },
},
Generic { CreateStdDialogButtonSizer(wxOK) },
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
}
Expand All @@ -152,6 +152,7 @@ SplitterExample::SplitterExample(wxWindow* parent)
: wxDialog(parent, wxID_ANY, "SplitterExample", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
using namespace wxUI;
GenericProxy<wxButton> proxy {};
// snippet SplitterExample
VSizer {
wxSizerFlags().Expand().Border(),
Expand All @@ -160,35 +161,63 @@ SplitterExample::SplitterExample(wxWindow* parent)
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
HSplitter {
TextCtrl { "This is Right Top.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
TextCtrl { "This is Right Bottom.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
rightUpper = TextCtrl { "This is Right Top.\n" }
.withStyle(wxTE_MULTILINE)
.withSize(wxSize(200, 100)),
Button { "Incr" }
.bind([this]() {
auto original = std::string { *rightUpper } + "\nThis is Right Top.\n";
*rightUpper = original;
}),
} },
// endsnippet SplitterExample
VSplitter {
TextCtrl {}.withStyle(wxTE_MULTILINE | wxHSCROLL | wxTE_PROCESS_TAB),
Generic {
[](wxWindow* parent) {
return new wxButton(parent, wxID_ANY, "Raw button");
} },
},
VSplitter {
proxy = [](wxWindow* parent) {
return new wxButton(parent, wxID_ANY, "Raw button");
},
}
.withSize(wxSize(500, 400)),

Generic { CreateStdDialogButtonSizer(wxOK) },
TextCtrl {}.withStyle(wxTE_MULTILINE | wxHSCROLL | wxTE_PROCESS_TAB),
},
// snippet SplitterExample
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
// endsnippet SplitterExample
*rightUpper = std::string { proxy->GetLabel() };
}

GenericExample::GenericExample(wxWindow* parent)
: wxDialog(parent, wxID_ANY, "SplitterExample", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
using namespace wxUI;
GenericProxy<wxButton> proxy1 {};
GenericProxy<wxButton> proxy2 {};
// snippet GenericExample
VSizer {
wxSizerFlags().Expand().Border(),
Generic {
[](wxWindow* window) {
return new wxButton(window, wxID_ANY, "Generic");
} },
Generic { CreateStdDialogButtonSizer(wxOK) },
// endsnippet GenericExample
proxy1 = Generic<wxButton> {
[](wxWindow* window) {
return new wxButton(window, wxID_ANY, "Raw 1");
} },
proxy2 = [](wxWindow* window) {
return new wxButton(window, wxID_ANY, "Raw 2");
},
// snippet GenericExample
CreateStdDialogButtonSizer(wxOK),
}
.attachTo(this);
// endsnippet GenericExample
assert(proxy1->GetLabel() == "Raw 1");
assert(proxy2->GetLabel() == "Raw 2");
}
1 change: 1 addition & 0 deletions examples/HelloWorld/ExtendedExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class SplitterExample : public wxDialog {
explicit SplitterExample(wxWindow* parent);

private:
wxUI::TextCtrl::Proxy rightUpper;
};

class GenericExample : public wxDialog {
Expand Down
3 changes: 2 additions & 1 deletion examples/HelloWorld/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
// endsnippet withwxUI
RadioBox { "&Log Levels:", { "&Information", "&Warning", "&Error", "&None", "&Custom" } }
.withStyle(wxRA_SPECIFY_ROWS)
.withoutStyle(wxRA_SPECIFY_COLS)
.withMajorDim(1)
.withSelection(1),

Expand All @@ -336,7 +337,7 @@ ExampleDialog::ExampleDialog(wxWindow* parent)
// endsnippet wxUIBind
},

Generic { CreateStdDialogButtonSizer(wxOK) },
CreateStdDialogButtonSizer(wxOK),
// snippet withwxUI
// snippet wxUILayoutBasic
}
Expand Down
124 changes: 124 additions & 0 deletions include/wxUI/BindInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
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 <optional>
#include <wx/sizer.h>

namespace wxUI::details {

// https://stackoverflow.com/questions/27866909/get-function-arity-from-template-parameter
template <typename T>
constexpr bool noarg_callable_impl(
typename std::enable_if<bool(sizeof((std::declval<T>()(), 0)))>::type*)
{
return true;
}

template <typename T>
constexpr bool noarg_callable_impl(...)
{
return false;
}

template <typename T>
constexpr bool is_noarg_callable()
{
return noarg_callable_impl<T>(nullptr);
}

// BindInfo uses type erase to allow any binding for any Event type.
struct BindInfo {

void bindTo([[maybe_unused]] wxWindow* widget) const
{
mInfo->bindTo(widget);
}

template <typename Event, typename Function>
BindInfo([[maybe_unused]] Event event, [[maybe_unused]] Function function)
: mInfo(std::make_unique<BindInfoDetails<Event, Function>>(event, function))
{
}

~BindInfo() = default;

BindInfo(BindInfo const& bindInfo)
: mInfo(bindInfo.mInfo->clone())
{
}

auto operator=(BindInfo const& bindInfo) -> BindInfo&
{
if (this == &bindInfo) {
return *this;
}
mInfo = bindInfo.mInfo->clone();
return *this;
}

BindInfo(BindInfo&& bindInfo) noexcept
: mInfo(std::move(bindInfo.mInfo))
{
}

auto operator=(BindInfo&& bindInfo) noexcept -> BindInfo&
{
mInfo = std::move(bindInfo.mInfo);
return *this;
}

private:
struct BindInfoDetailsBase {
virtual ~BindInfoDetailsBase() = default;
virtual void bindTo(wxWindow* widget) const = 0;
[[nodiscard]] virtual auto clone() const -> std::unique_ptr<BindInfoDetailsBase> = 0;
};

template <typename Event, typename Function>
struct BindInfoDetails : BindInfoDetailsBase {
BindInfoDetails(Event event, Function function)
: event(event)
, function(function)
{
}
Event event;
Function function;
void bindTo(wxWindow* widget) const override
{
widget->Bind(event, function);
}
[[nodiscard]] auto clone() const -> std::unique_ptr<BindInfoDetailsBase> override
{
return std::make_unique<BindInfoDetails>(event, function);
}
};

std::unique_ptr<BindInfoDetailsBase> mInfo;
};

static_assert(std::is_nothrow_move_constructible_v<BindInfo>);
static_assert(std::is_nothrow_move_assignable_v<BindInfo>);

}
10 changes: 5 additions & 5 deletions include/wxUI/Bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ struct Bitmap : public details::WidgetDetails<Bitmap, wxStaticBitmap> {
{
}

auto createImpl(wxWindow* parent) -> wxWindow* override
{
return setProxy(new underlying_t(parent, getIdentity(), bitmap, getPos(), getSize(), getStyle()));
}

struct Proxy : super::WidgetProxy {
PROXY_BOILERPLATE();
};
RULE_OF_SIX_BOILERPLATE(Bitmap);

private:
wxBitmap bitmap;

auto createImpl(wxWindow* parent) -> wxWindow* override
{
return setProxy(new underlying_t(parent, getIdentity(), bitmap, getPos(), getSize(), getStyle()));
}
};

WIDGET_STATIC_ASSERT_BOILERPLATE(Bitmap);
Expand Down
Loading

0 comments on commit 5d15712

Please sign in to comment.