1.10
combo.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_COMBO_H
7#define EGT_COMBO_H
8
14#include <egt/detail/meta.h>
15#include <egt/list.h>
16#include <egt/popup.h>
17#include <egt/signal.h>
18#include <egt/string.h>
19#include <egt/widget.h>
20#include <memory>
21#include <string>
22#include <vector>
23
24namespace egt
25{
26inline namespace v1
27{
28class ComboBox;
29class Serializer;
30
31namespace detail
32{
33class ComboBoxPopup;
34}
35
44class EGT_API ComboBox : public Widget
45{
46public:
47
59 using ItemArray = std::vector<std::shared_ptr<StringItem>>;
60
64 explicit ComboBox(const ItemArray& items = ItemArray()) noexcept;
65
69 explicit ComboBox(const Rect& rect) noexcept;
70
75 ComboBox(const ItemArray& items, const Rect& rect) noexcept;
76
82 explicit ComboBox(Frame& parent,
83 const ItemArray& items = {},
84 const Rect& rect = {}) noexcept;
85
89 explicit ComboBox(Serializer::Properties& props) noexcept
90 : ComboBox(props, false)
91 {
92 }
93
94protected:
95
96 explicit ComboBox(Serializer::Properties& props, bool is_derived) noexcept;
97
98public:
99
100 void handle(Event& event) override;
101
102 void resize(const Size& s) override;
103
104 void move(const Point& point) override;
105
106 void draw(Painter& painter, const Rect& rect) override;
107
110
112 static void default_size(const Size& size);
113
114 using Widget::min_size_hint;
115
116 EGT_NODISCARD Size min_size_hint() const override;
117
121 static void default_draw(ComboBox& widget, Painter& painter, const Rect& rect);
122
126 void selected(size_t index);
127
131 EGT_NODISCARD ssize_t selected() const { return m_list.selected(); }
132
136 void add_item(const std::shared_ptr<StringItem>& item);
137
148 {
149 // Nasty, but it gets the job done. If a widget is passed in as a
150 // reference, we don't own it, so create a "pointless" shared_ptr that
151 // will not delete it.
152 auto i = std::shared_ptr<StringItem>(&item, [](StringItem*) {});
153 add_item(i);
154 }
155
160
164 EGT_NODISCARD std::shared_ptr<StringItem> item_at(size_t index) const
165 {
166 return m_list.item_at(index);
167 }
168
172 EGT_NODISCARD size_t item_count() const
173 {
174 return m_list.item_count();
175 }
176
180 void show_popup() const;
181
185 void hide_popup() const;
186
190 void clear();
191
192 void serialize(Serializer& serializer) const override;
193
194protected:
195
196 void set_parent(Widget* parent) override;
197
200
202 std::shared_ptr<detail::ComboBoxPopup> m_popup;
203
204 friend class detail::ComboBoxPopup;
205
206private:
207
208 static Size default_combobox_size_value;
209 static Signal<>::RegisterHandle default_combobox_size_handle;
210 static void register_handler();
211 static void unregister_handler();
212
213 void initialize(bool init_inherited_properties = true);
214
215 void deserialize(Serializer::Properties& props);
216};
217
218}
219}
220
221#endif
Combo box widget.
Definition combo.h:45
static Size default_size()
Default combobox size.
EGT_NODISCARD Size min_size_hint() const override
Get a minimum size hint for the Widget.
ComboBox(Serializer::Properties &props, bool is_derived) noexcept
Signal on_selected_changed
Event signal.
Definition combo.h:55
EGT_NODISCARD size_t item_count() const
Return the number of items in the list.
Definition combo.h:172
void show_popup() const
Show the ComboBox popup.
EGT_NODISCARD ssize_t selected() const
Get the index of the selected item.
Definition combo.h:131
ComboBox(Serializer::Properties &props) noexcept
Definition combo.h:89
void draw(Painter &painter, const Rect &rect) override
Draw the widget.
ComboBox(const ItemArray &items=ItemArray()) noexcept
std::vector< std::shared_ptr< StringItem > > ItemArray
Item array type.
Definition combo.h:59
void selected(size_t index)
Set the index of the selected item.
void resize(const Size &s) override
Resize the widget.
static void default_size(const Size &size)
Change default combobox size.
static void default_draw(ComboBox &widget, Painter &painter, const Rect &rect)
Default draw method for the ComboBox.
ListBox m_list
Internal list.
Definition combo.h:199
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
void hide_popup() const
Hide the ComboBox popup.
void remove_item(StringItem *item)
Remove an item from a ComboBox.
void handle(Event &event) override
Handle an event.
void clear()
Remove all items from the list.
void add_item(const std::shared_ptr< StringItem > &item)
Append a new item to the ComboBox.
void set_parent(Widget *parent) override
Set this widget's parent.
void move(const Point &point) override
Move the Widget to a new position.
void add_item(StringItem &item)
Append a new item to the ComboBox.
Definition combo.h:147
std::shared_ptr< detail::ComboBoxPopup > m_popup
Popup associated with the ComboBox.
Definition combo.h:202
EGT_NODISCARD std::shared_ptr< StringItem > item_at(size_t index) const
Get an item at the specified index.
Definition combo.h:164
A Frame is a Widget that has children widgets.
Definition frame.h:45
ListBox that manages a selectable list of widgets.
Definition list.h:42
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
uint32_t RegisterHandle
Handle type.
Definition signal.h:46
Base Widget class.
Definition widget.h:53
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
List string helper.
Definition string.h:34