-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorld.cpp
169 lines (146 loc) · 4.21 KB
/
HelloWorld.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// wxWidgets "Hello world" Program
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <optional>
class MyApp : public wxApp {
public:
virtual bool OnInit();
};
class MyFrame : public wxFrame {
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnHello(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum { ID_Hello = 1 };
// clang-format off
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Hello, MyFrame::OnHello)
EVT_MENU(wxID_EXIT, MyFrame::OnExit)
EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
wxEND_EVENT_TABLE()
wxIMPLEMENT_APP(MyApp);
// clang-format on
bool MyApp::OnInit()
{
MyFrame* frame = new MyFrame("Hello World", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
namespace DeclarativeUI {
template <typename T>
concept CreateAndAddable = requires(T widget, wxWindow* window, wxSizer* sizer) {
widget.createAndAdd(window, sizer, wxSizerFlags {});
};
template <typename W>
struct Widget {
explicit Widget(std::string str, std::optional<wxSizerFlags> flags = {})
: str(std::move(str))
, flags(flags)
{
}
auto createAndAdd(wxWindow* parent, wxSizer* sizer, wxSizerFlags parentFlags)
{
sizer->Add(new W(parent, wxID_ANY, str), flags ? *flags : parentFlags);
}
std::string str;
std::optional<wxSizerFlags> flags;
};
template <CreateAndAddable... W>
struct Sizer {
Sizer(wxOrientation orientation, wxSizerFlags flags, W... widgets)
: orientation(orientation)
, flags(flags)
, widgets(std::make_tuple(widgets...))
{
}
Sizer(wxOrientation orientation, W... widgets)
: orientation(orientation)
, widgets(std::make_tuple(widgets...))
{
}
auto createAndAdd(wxWindow* parent, wxSizer* parentSizer, wxSizerFlags parentFlags)
{
auto* sizer = new wxBoxSizer(orientation);
std::apply([this, parent, sizer, parentFlags](auto&&... tupleArg) {
(tupleArg.createAndAdd(parent, sizer, flags.value_or(parentFlags)), ...);
},
widgets);
parentSizer->Add(sizer, parentFlags);
return sizer;
}
auto attachTo(wxWindow* parent)
{
auto* sizer = new wxBoxSizer(orientation);
std::apply([parent, sizer, flags = flags ? *flags : wxSizerFlags()](auto&&... tupleArg) {
(tupleArg.createAndAdd(parent, sizer, flags), ...);
},
widgets);
parent->SetSizerAndFit(sizer);
return sizer;
}
wxOrientation orientation;
std::optional<wxSizerFlags> flags;
std::tuple<W...> widgets;
};
template <CreateAndAddable... W>
struct HSizer : Sizer<W...> {
HSizer(W... widgets)
: Sizer<W...>(wxHORIZONTAL, widgets...)
{
}
HSizer(wxSizerFlags flags, W... widgets)
: Sizer<W...>(wxHORIZONTAL, flags, widgets...)
{
}
};
template <CreateAndAddable... W>
struct VSizer : Sizer<W...> {
VSizer(W... widgets)
: Sizer<W...>(wxVERTICAL, widgets...)
{
}
VSizer(wxSizerFlags flags, W... widgets)
: Sizer<W...>(wxVERTICAL, flags, widgets...)
{
}
};
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
using namespace DeclarativeUI;
// Create and layout the controls.
VSizer {
wxSizerFlags().Expand().Border(),
HSizer {
Widget<wxTextCtrl> { "Dog",
wxSizerFlags(1).Expand().Border() },
Widget<wxButton> { "Right" },
},
HSizer {
Widget<wxButton> { "Left" },
Widget<wxStaticText> { "Cat" },
},
}
.attachTo(this);
}
void MyFrame::OnExit([[maybe_unused]] wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout([[maybe_unused]] wxCommandEvent& event)
{
wxMessageBox("This is a wxWidgets' Hello world sample", "About Hello World",
wxOK | wxICON_INFORMATION);
}
void MyFrame::OnHello([[maybe_unused]] wxCommandEvent& event)
{
wxLogMessage("Hello world from wxWidgets!");
}