The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
ZXWidgetsForm.h
Go to the documentation of this file.
1
2/* **************************************************************************/
19#ifndef ZXGUIFORM
20#define ZXGUIFORM
21
22#include <functional>
23#include <string>
24#include <vector>
25#include <utility>
28
29
30
31namespace zxeco
32{
33
34namespace gui
35{
36
37
38/*******************************************************************************
39*
40* Template abstract base class: WidgetsForm
41*
42*******************************************************************************/
43
45
48template <class Underlying>
50{
51 public:
52
54
56 using Callback = std::function< bool(void) >;
57
58
60
61 WidgetsForm(const std::string & formname, Underlying & under):
62 formname_{formname},
63 underlying_{under},
64 populated_{false}
65 {}
66
67 WidgetsForm(const WidgetsForm &) = delete;
68 WidgetsForm(WidgetsForm &&) = delete;
69 WidgetsForm & operator=(const WidgetsForm &) = delete;
70 WidgetsForm & operator=(WidgetsForm &&) = delete;
71
72 virtual ~WidgetsForm(void) = default;
73
74
76
85
87 virtual void fill(void) = 0;
88
90 virtual void dump(void) = 0;
91
93
95
96
97 protected:
98
99 const std::string formname_;
101 Underlying & underlying_;
108 void checkIsPopulated(
109 const std::string & place = "") const;
111 const std::string & place = "") const;
112};
113
114
115/*******************************************************************************
116*
117* Class: WidgetsFormBroker
118*
119*******************************************************************************/
120
122
126{
127 public:
128
130 using Topic = int;
131
133 using Handler = std::pair<Topic,size_t>;
134
136
143 using Callback = std::function< bool(Topic, void *,
144 Widget *, const Widget::EventData *) >;
145
146
148
151 Handler subscribe(Topic topic, const Callback & cbck);
152
154
159 bool produce(Topic topic,
160 void * parms = nullptr,
161 Widget * w = nullptr, const Widget::EventData * ed = nullptr);
162
164 std::string to_string(void) const;
165
166
167 private:
168
169 using TopicLink = std::pair<Topic,size_t>;
170 using Callbacks = std::vector<Callback>;
171
172 static bool comp(const TopicLink & t1, const TopicLink & t2) noexcept
173 { return(t1.first < t2.first); }
174
175 std::vector<TopicLink> topics_;
176 std::vector<Callbacks> subscribed_;
177
178 size_t findTopic(Topic topic) const;
179};
180
181
182
183/*============================================================================
184
185 TEMPLATE IMPLEMENTATIONS
186
187==============================================================================*/
188
189
190/*******************************************************************************
191*
192* Template abstract base class: WidgetsForm
193*
194*******************************************************************************/
195
196template <class Underlying>
197void WidgetsForm<Underlying>::checkIsPopulated(const std::string & place) const
198{
199 if (!populated_) RUNTIMEEXCEP("WidgetsForm '" + formname_ +
200 "' not populated. " +
201 (place.empty() ? "" : place));
202}
203
204template <class Underlying>
205void WidgetsForm<Underlying>::checkIsUnpopulated(const std::string & place) const
206{
207 if (populated_) RUNTIMEEXCEP("Cannot populate twice WidgetsForm '" +
208 formname_ + "'. " +
209 (place.empty() ? "" : place));
210}
211
212template <class Underlying>
214{
215 checkIsPopulated();
216 return(*cont_);
217}
218
219
220
221} // end namespace gui
222} // end namespace zxeco
223
224#endif
225 // ZXForm
227
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
Definition: CppAddons.h:93
A container of other widgets.
All widgets must inherit from this.
Definition: ZXWidgetsBase.h:56
Data passed to a widget when an event occurs.
virtual void populateGUI(zxeco::gui::Window::DepthFirstInserter &wins)=0
Must create form widgets in WINS, fill CONT_ and set POPULATED_ to true.
bool produce(Topic topic, void *parms=nullptr, Widget *w=nullptr, const Widget::EventData *ed=nullptr)
Produce the given topic.
virtual void dump(void)=0
Must dump the current form field values into the underlying system.
void checkIsUnpopulated(const std::string &place="") const
Throw if populated.
std::function< bool(void) > Callback
A callback for some interacting element of the form.
Definition: ZXWidgetsForm.h:56
zxeco::gui::ContainerWidget & mainContainer(void)
Return a reference to the main container of the form.
Handler subscribe(Topic topic, const Callback &cbck)
Subscribe the caller to the given topic.
std::pair< Topic, size_t > Handler
A handler for a subscription.
std::string to_string(void) const
Return a string with info about the current state of the broker.
WidgetsForm(const std::string &formname, Underlying &under)
Base constructor. Derived constructors must not populate the form.
Definition: ZXWidgetsForm.h:61
std::function< bool(Topic, void *, Widget *, const Widget::EventData *) > Callback
A callback to be called when a topic produces a new value.
bool populated_
Whether the form has already been populated.
virtual void fill(void)=0
Must fill the fields with their current values from the underlying sys.
zxeco::gui::ContainerWidget * cont_
Main container of the form.
Class from which any form must derive.
Definition: ZXWidgetsForm.h:50
The subscription/production intercommunicator of a number of forms.
void checkIsPopulated(const std::string &place="") const
Throw if unpopulated.
To insert widgets in a window in a depth-first fashion.
Definition: ZXWindows.h:61
The main namespace of the library, that spans across all the zx modules.
Definition: ZXChars.h:31