1.10
button.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_BUTTON_H
7#define EGT_BUTTON_H
8
14#include <egt/buttongroup.h>
15#include <egt/detail/meta.h>
16#include <egt/geometry.h>
17#include <egt/image.h>
18#include <egt/imageholder.h>
19#include <egt/textwidget.h>
20#include <egt/theme.h>
21#include <memory>
22#include <string>
23
24namespace egt
25{
26inline namespace v1
27{
28
29class Frame;
30class Painter;
31
64class EGT_API Button : public TextWidget
65{
66public:
67
71 static void default_text_align(const AlignFlags& align);
72
77 explicit Button(const std::string& text = {},
78 const AlignFlags& text_align = default_text_align()) noexcept;
79
85 Button(const std::string& text, const Rect& rect,
86 const AlignFlags& text_align = default_text_align()) noexcept;
87
93 explicit Button(Frame& parent, const std::string& text = {},
94 const AlignFlags& text_align = default_text_align()) noexcept;
95
102 Button(Frame& parent,
103 const std::string& text,
104 const Rect& rect,
105 const AlignFlags& text_align = default_text_align()) noexcept;
109 explicit Button(Serializer::Properties& props) noexcept
110 : Button(props, false)
111 {
112 }
113
114protected:
115
116 explicit Button(Serializer::Properties& props, bool is_derived) noexcept;
117
118public:
119
120 Button(const Button&) = delete;
121 Button& operator=(const Button&) = delete;
122 Button(Button&&) noexcept = default;
123 Button& operator=(Button&&) noexcept = default;
124
125 void handle(Event& event) override;
126
127 using TextWidget::text;
128
129 void text(const std::string& text) override;
130
131 void draw(Painter& painter, const Rect& rect) override;
132
143 RegisterHandle on_click(const EventCallback& handler)
144 {
145 return on_event(handler, {EventId::pointer_click});
146 }
147
149 static void default_draw(const Button& widget, Painter& painter, const Rect& rect);
150
151 using Widget::checked;
152
153 void checked(bool value) override;
154
158 static void default_size(const Size& size);
159
160 using TextWidget::min_size_hint;
161
162 EGT_NODISCARD Size min_size_hint() const override;
163
164 ~Button() noexcept override;
165
166protected:
167
168 void set_parent(Widget* parent) override;
169
170private:
171
172 static Size default_button_size_value;
173 static Signal<>::RegisterHandle default_button_size_handle;
174 static void register_handler();
175 static void unregister_handler();
176
177 static AlignFlags default_text_align_value;
178
183 ButtonGroup* m_group{nullptr};
184
185 friend ButtonGroup;
186};
187
208
213class EGT_API CheckButton : public Button
214{
215 using Button::Button;
216
217public:
218
219 void handle(Event& event) override
220 {
221 Button::handle(event);
222
223 switch (event.id())
224 {
225 case EventId::pointer_click:
226 checked(!checked());
227 default:
228 break;
229 }
230 }
231};
232
233class EGT_API Switch : public Button
234{
235public:
236
242 explicit Switch(const std::string& text = {},
243 const Rect& rect = {}) noexcept;
244
248 explicit Switch(Serializer::Properties& props) noexcept
249 : Switch(props, false)
250 {
251 }
252
253protected:
254
255 explicit Switch(Serializer::Properties& props, bool is_derived) noexcept;
256
257 void serialize(Serializer& serializer) const override;
258
259public:
260
261 void handle(Event& event) override;
262
263 void draw(Painter& painter, const Rect& rect) override;
264
268 static void default_draw(const Switch& widget, Painter& painter, const Rect& rect);
269
270 using Button::min_size_hint;
271
272 EGT_NODISCARD Size min_size_hint() const override;
273
274 using Button::text;
283 void text(const std::string& text) override;
284
290 void show_label(bool value)
291 {
292 if (detail::change_if_diff<>(m_show_label, value))
293 damage();
294 }
295
299 EGT_NODISCARD bool show_label() const { return m_show_label; }
300
306 void switch_align(const AlignFlags& align)
307 {
308 if (detail::change_if_diff<>(m_switch_align, align))
309 {
310 damage();
311 layout();
312 }
313 }
314
318 EGT_NODISCARD AlignFlags switch_align() const { return m_switch_align; }
319
329 EGT_NODISCARD Image* switch_image(bool checked) const;
330
340 void switch_image(const Image& image, bool checked);
341
350 void reset_switch_image(bool checked);
351
352protected:
353
354 virtual void draw_switch(Painter& painter, const Rect& handle) const;
355
356private:
357
358 void deserialize(Serializer::Properties& props);
359
360 bool m_show_label{true};
362 AlignFlags m_switch_align{AlignFlag::left};
363
367 mutable std::unique_ptr<Image> m_normal_switch;
368 mutable std::unique_ptr<Image> m_checked_switch;
369};
370
376namespace experimental
377{
378
379}
380
381}
382}
383
384#endif
Alignment flags.
Definition widgetflags.h:379
Button group.
Definition buttongroup.h:39
Basic button control.
Definition button.h:65
static Size default_size()
Default button size.
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Button(Serializer::Properties &props, bool is_derived) noexcept
static void default_draw(const Button &widget, Painter &painter, const Rect &rect)
Default draw method for the widget.
Button(const Button &)=delete
static AlignFlags default_text_align()
Change text align.
Button & operator=(const Button &)=delete
Button(Frame &parent, const std::string &text, const Rect &rect, const AlignFlags &text_align=default_text_align()) noexcept
Button(Button &&) noexcept=default
static void default_size(const Size &size)
Change default button size.
~Button() noexcept override
static void default_text_align(const AlignFlags &align)
Change default text align.
void checked(bool value) override
Set the checked state of the widget.
Button(const std::string &text={}, const AlignFlags &text_align=default_text_align()) noexcept
Button(const std::string &text, const Rect &rect, const AlignFlags &text_align=default_text_align()) noexcept
Same as a normal Button, except it manipulates its checked state like a RadioBox or CheckBox.
Definition button.h:214
void handle(Event &event) override
Handle an event.
Definition button.h:219
A Frame is a Widget that has children widgets.
Definition frame.h:45
Definition imageholder.h:35
Raster image resource used for drawing or displaying.
Definition image.h:39
std::function< void(Event &event)> EventCallback
Event handler callback function.
Definition object.h:55
uint64_t RegisterHandle
Handle type.
Definition object.h:61
Drawing interface for 2D graphics.
Definition painter.h:45
Abstract base serializer class.
Definition serialize.h:34
std::list< std::tuple< std::string, std::string, Serializer::Attributes > > Properties
Definition serialize.h:47
Signal class used for defining a signal and dispatching events.
Definition signal.h:30
Definition button.h:234
EGT_NODISCARD Image * switch_image(bool checked) const
Get the image, if any, used to draw the widget's switch, based on the value of its 'checked' flag.
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
Switch(Serializer::Properties &props, bool is_derived) noexcept
void switch_image(const Image &image, bool checked)
Add an image to draw the widget's switch, based on the value of its 'checked' flag.
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
EGT_NODISCARD bool show_label() const
Get the show label state.
Definition button.h:299
Switch(Serializer::Properties &props) noexcept
Definition button.h:248
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
static void default_draw(const Switch &widget, Painter &painter, const Rect &rect)
Default draw method for the Switch.
void switch_align(const AlignFlags &align)
Set the alignement of the switch relative to the text.
Definition button.h:306
virtual void draw_switch(Painter &painter, const Rect &handle) const
Switch(const std::string &text={}, const Rect &rect={}) noexcept
void show_label(bool value)
Enable/disable showing the label text.
Definition button.h:290
void reset_switch_image(bool checked)
Remove the image, if any, used to draw the widget's switch, based on the value of its 'checked' flag.
void handle(Event &event) override
Handle an event.
EGT_NODISCARD AlignFlags switch_align() const
Get the switch alignment.
Definition button.h:318
void text(const std::string &text) override
Set the text.
A widget with text and text related properties.
Definition textwidget.h:33
Base Widget class.
Definition widget.h:53
ImageHolder< Button, Palette::ColorId::button_bg, Palette::ColorId::border, Palette::ColorId::button_text > ImageButton
Button that also contains an Image.
Definition button.h:207
T & align(T &widget, const AlignFlags &a)
Helper to set alignment of a widget.
Definition widgetflags.h:624
EGT framework namespace.
Definition animation.h:24
A single event that has information about the event and state for the event.
Definition event.h:255
EGT_NODISCARD const EventId & id() const noexcept
Get the id of the event.
Definition event.h:284