1.10
list.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_LIST_H
7#define EGT_LIST_H
8
14#include <egt/detail/meta.h>
15#include <egt/label.h>
16#include <egt/signal.h>
17#include <egt/sizer.h>
18#include <egt/string.h>
19#include <egt/view.h>
20#include <egt/widget.h>
21#include <string>
22
23namespace egt
24{
25inline namespace v1
26{
27
41class EGT_API ListBox : public Widget
42{
43public:
44
53
58
66 using ItemArray = std::vector<std::shared_ptr<StringItem>>;
67
71 explicit ListBox(const ItemArray& items = ItemArray()) noexcept;
72
76 explicit ListBox(const Rect& rect) noexcept;
77
82 ListBox(const ItemArray& items, const Rect& rect) noexcept;
83
89 explicit ListBox(Frame& parent, const ItemArray& items = {}, const Rect& rect = {}) noexcept;
90
94 explicit ListBox(Serializer::Properties& props) noexcept
95 : ListBox(props, false)
96 {
97 }
98
99protected:
100
101 explicit ListBox(Serializer::Properties& props, bool is_derived) noexcept;
102
103public:
104
105 void handle(Event& event) override;
106
107 void resize(const Size& s) override
108 {
109 if (s != size())
110 {
111 Widget::resize(s);
112 auto carea = content_area();
113 if (!carea.empty())
114 {
115 m_view.box(to_subordinate(carea));
116 m_sizer.resize(carea.size());
117 }
118 }
119 }
120
124 void selected(size_t index);
125
131 EGT_NODISCARD ssize_t selected() const;
132
136 EGT_NODISCARD size_t item_count() const { return m_sizer.count_children(); }
137
141 void add_item(const std::shared_ptr<StringItem>& item);
142
153 {
154 // Nasty, but it gets the job done. If a widget is passed in as a
155 // reference, we don't own it, so create a "pointless" shared_ptr that
156 // will not delete it.
157 auto i = std::shared_ptr<StringItem>(&item, [](StringItem*) {});
158 add_item(i);
159 }
160
164 EGT_NODISCARD std::shared_ptr<StringItem> item_at(size_t index) const;
165
170
174 void clear();
175
180
185
191
197
201 void orient(Orientation orient);
202
206 EGT_NODISCARD Orientation orient() const
207 {
208 return m_orient;
209 }
210
211 void serialize(Serializer& serializer) const override;
212
213protected:
214
217
220
222 Orientation m_orient{Orientation::vertical};
223
224private:
225
226 void add_item_private(const std::shared_ptr<StringItem>& item);
227
228 void deserialize(Serializer::Properties& props);
229};
230
231}
232}
233
234#endif
Positions and sizes widgets by Orientation.
Definition sizer.h:49
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
BoxSizer m_sizer
Internal sizer used to layout items.
Definition list.h:219
void scroll_top()
Scroll all the way to the top of the list.
void orient(Orientation orient)
Set the orientation of the list: either vertical or horizontal.
ScrolledView m_view
View used to contain the possible large sizer.
Definition list.h:216
Signal on_items_changed
Invoked when items are added or removed.
Definition list.h:62
Signal on_selected_changed
Event signal.
Definition list.h:52
Signal< size_t > on_selected
Invoked when an item is selected with the index of the item selected.
Definition list.h:57
EGT_NODISCARD size_t item_count() const
Return the number of items in the list.
Definition list.h:136
EGT_NODISCARD ssize_t selected() const
Get the currently selected index.
void scroll_beginning()
Scroll all the way to the beginning of the list, either horizontally or vertically.
ListBox(Serializer::Properties &props, bool is_derived) noexcept
void scroll_bottom()
Scroll all the way to the bottom of the list.
EGT_NODISCARD Orientation orient() const
Get the orientation of the list: either vertical or horizontal.
Definition list.h:206
std::vector< std::shared_ptr< StringItem > > ItemArray
Item array type.
Definition list.h:66
void selected(size_t index)
Select an item by index.
void resize(const Size &s) override
Resize the widget.
Definition list.h:107
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
void scroll_end()
Scroll all the way to the end of the list, either horizontally or vertically.
void remove_item(StringItem *item)
Remove an item from the list.
ListBox(Serializer::Properties &props) noexcept
Definition list.h:94
ListBox(const ItemArray &items=ItemArray()) noexcept
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)
Add a new item to the end of the list.
void add_item(StringItem &item)
Add a new item to the end of the list.
Definition list.h:152
EGT_NODISCARD std::shared_ptr< StringItem > item_at(size_t index) const
Get the currently selected index item from list.
A scrollable view.
Definition view.h:36
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
Base Widget class.
Definition widget.h:53
Orientation
Generic orientation flags.
Definition widgetflags.h:652
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