1.10
checkbox.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_CHECKBOX_H
7#define EGT_CHECKBOX_H
8
14#include <egt/button.h>
15#include <egt/detail/meta.h>
16#include <string>
17
18namespace egt
19{
20inline namespace v1
21{
22
23class Painter;
24class Frame;
25
38class EGT_API CheckBox : public Switch
39{
40public:
41
46 explicit CheckBox(const std::string& text = {},
47 const Rect& rect = {}) noexcept;
48
54 explicit CheckBox(Frame& parent,
55 const std::string& text = {},
56 const Rect& rect = {}) noexcept;
57
58 using Switch::Switch;
59
60 void draw(Painter& painter, const Rect& rect) override;
61
67 void checkbox_align(const AlignFlags& align)
68 {
69 switch_align(align);
70 }
71
75 EGT_NODISCARD AlignFlags checkbox_align() const { return switch_align(); }
76
77protected:
78
79 void draw_switch(Painter& painter, const Rect& handle) const override;
80};
81
90class EGT_API ToggleBox : public CheckBox
91{
92public:
93
97 explicit ToggleBox(const Rect& rect = {}) noexcept;
98
103 explicit ToggleBox(Frame& parent, const Rect& rect = {}) noexcept;
104
108 explicit ToggleBox(Serializer::Properties& props) noexcept
109 : ToggleBox(props, false)
110 {
111 }
112
113protected:
114
115 explicit ToggleBox(Serializer::Properties& props, bool is_derived) noexcept;
116
117public:
118
119 void draw(Painter& painter, const Rect& rect) override;
120
124 static void default_draw(ToggleBox& widget, Painter& painter, const Rect& rect);
125
126 using CheckBox::min_size_hint;
127
128 EGT_NODISCARD Size min_size_hint() const override;
129
136 void toggle_text(const std::string& off_text,
137 const std::string& on_text)
138 {
139 bool off = detail::change_if_diff<>(m_off_text, off_text);
140 bool on = detail::change_if_diff<>(m_on_text, on_text);
141
142 if (on || off)
143 damage();
144 }
145
149 EGT_NODISCARD const std::string& off_text() const { return m_off_text; }
150
154 EGT_NODISCARD const std::string& on_text() const { return m_on_text; }
155
159 EGT_NODISCARD bool enable_disable() const { return m_enable_disable; }
160
169 void enable_disable(bool value)
170 {
171 if (detail::change_if_diff<>(m_enable_disable, value))
172 damage();
173 }
174
175 void serialize(Serializer& serializer) const override;
176
177private:
178
179 void deserialize(Serializer::Properties& props);
180
181protected:
182
183 // this is particularly bad design that we don't even use text() here
184 using TextWidget::text;
185
189 std::string m_off_text;
190
194 std::string m_on_text;
195
199 bool m_enable_disable{true};
200};
201
202}
203}
204
205#endif
Alignment flags.
Definition widgetflags.h:379
Boolean checkbox.
Definition checkbox.h:39
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
void draw_switch(Painter &painter, const Rect &handle) const override
EGT_NODISCARD AlignFlags checkbox_align() const
Get the image alignment.
Definition checkbox.h:75
CheckBox(const std::string &text={}, const Rect &rect={}) noexcept
void checkbox_align(const AlignFlags &align)
Set the alignment of the checkbox relative to the text.
Definition checkbox.h:67
CheckBox(Frame &parent, const std::string &text={}, const Rect &rect={}) noexcept
A Frame is a Widget that has children widgets.
Definition frame.h:45
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
Definition button.h:234
CheckBox with a boolean slider style interface.
Definition checkbox.h:91
std::string m_off_text
Optional "off" text.
Definition checkbox.h:189
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
ToggleBox(const Rect &rect={}) noexcept
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
static void default_draw(ToggleBox &widget, Painter &painter, const Rect &rect)
Default draw method for the CheckBox.
std::string m_on_text
Optional "on" text.
Definition checkbox.h:194
void enable_disable(bool value)
Set the enable/disable mode.
Definition checkbox.h:169
EGT_NODISCARD bool enable_disable() const
Get the enable/disable mode.
Definition checkbox.h:159
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
EGT_NODISCARD const std::string & on_text() const
Get the "on" text.
Definition checkbox.h:154
void toggle_text(const std::string &off_text, const std::string &on_text)
Set the optional "on" and "off" text.
Definition checkbox.h:136
ToggleBox(Frame &parent, const Rect &rect={}) noexcept
ToggleBox(Serializer::Properties &props) noexcept
Definition checkbox.h:108
ToggleBox(Serializer::Properties &props, bool is_derived) noexcept
EGT_NODISCARD const std::string & off_text() const
Get the "off" text.
Definition checkbox.h:149
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