1.11
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 <list>
22#include <string>
23
24namespace egt
25{
26inline namespace v1
27{
28
33class EGT_API ListBoxBase : public Widget
34{
35public:
36
45
53 using ItemArray = std::vector<std::shared_ptr<StringItem>>;
54
55protected:
56
60 explicit ListBoxBase(const ItemArray& items = ItemArray()) noexcept;
61
65 explicit ListBoxBase(const Rect& rect) noexcept;
66
71 ListBoxBase(const ItemArray& items, const Rect& rect) noexcept;
72
78 explicit ListBoxBase(Frame& parent, const ItemArray& items = {}, const Rect& rect = {}) noexcept;
79
80 explicit ListBoxBase(Serializer::Properties& props) noexcept;
81
82public:
83
84 virtual ~ListBoxBase() = default;
85
86 void resize(const Size& s) override
87 {
88 if (s != size())
89 {
90 Widget::resize(s);
91 auto carea = content_area();
92 if (!carea.empty())
93 {
94 m_view.box(to_subordinate(carea));
95 m_sizer.resize(carea.size());
96 }
97 }
98 }
99
103 virtual void selected(size_t index) = 0;
104
108 EGT_NODISCARD size_t item_count() const { return m_sizer.count_children(); }
109
113 void add_item(const std::shared_ptr<StringItem>& item);
114
118 void add_item_at(const std::shared_ptr<StringItem>& item, size_t pos);
119
130 {
131 // Nasty, but it gets the job done. If a widget is passed in as a
132 // reference, we don't own it, so create a "pointless" shared_ptr that
133 // will not delete it.
134 auto i = std::shared_ptr<StringItem>(&item, [](StringItem*) {});
135 add_item(i);
136 }
137
150 void add_item_at(StringItem& item, size_t pos)
151 {
152 // Nasty, but it gets the job done. If a widget is passed in as a
153 // reference, we don't own it, so create a "pointless" shared_ptr that
154 // will not delete it.
155 auto i = std::shared_ptr<StringItem>(&item, [](StringItem*) {});
156 add_item_at(i, pos);
157 }
158
162 EGT_NODISCARD std::shared_ptr<StringItem> item_at(size_t index) const;
163
168
180 void remove_item_at(size_t pos);
181
185 void clear();
186
191
196
202
208
214 void scroll_offset(int offset);
215
219 void orient(Orientation orient);
220
224 EGT_NODISCARD Orientation orient() const
225 {
226 return m_orient;
227 }
228
229 void serialize(Serializer& serializer) const override;
230
231protected:
232
235
238
240 Orientation m_orient{Orientation::vertical};
241
242private:
243
244 void add_item_private(const std::shared_ptr<StringItem>& item, ssize_t pos = -1);
245
246 void deserialize(Serializer::Properties& props);
247};
248
262class EGT_API ListBox : public ListBoxBase
263{
264public:
265
280 explicit ListBox(const ItemArray& items = ItemArray()) noexcept
281 : ListBoxBase(items)
282 {
283 }
284
288 explicit ListBox(const Rect& rect) noexcept
289 : ListBoxBase(rect)
290 {
291 }
292
297 ListBox(const ItemArray& items, const Rect& rect) noexcept
298 : ListBoxBase(items, rect)
299 {
300 }
301
307 explicit ListBox(Frame& parent, const ItemArray& items = {}, const Rect& rect = {}) noexcept
308 : ListBoxBase(parent, items, rect)
309 {
310 }
311
315 explicit ListBox(Serializer::Properties& props) noexcept
316 : ListBox(props, false)
317 {
318 }
319
320protected:
321
322 explicit ListBox(Serializer::Properties& props, bool is_derived) noexcept;
323
324public:
325
326 void handle(Event& event) override;
327
331 void selected(size_t index) override;
332
338 EGT_NODISCARD ssize_t selected() const;
339};
340
362class EGT_API ListBoxMulti : public ListBoxBase
363{
364public:
365
375
387 explicit ListBoxMulti(const ItemArray& items = ItemArray()) noexcept;
388
392 explicit ListBoxMulti(const Rect& rect) noexcept;
393
398 ListBoxMulti(const ItemArray& items, const Rect& rect) noexcept;
399
405 explicit ListBoxMulti(Frame& parent, const ItemArray& items = {}, const Rect& rect = {}) noexcept;
406
410 explicit ListBoxMulti(Serializer::Properties& props) noexcept
411 : ListBoxMulti(props, false)
412 {
413 }
414
415protected:
416
417 explicit ListBoxMulti(Serializer::Properties& props, bool is_derived) noexcept;
418
419public:
420
421 void handle(Event& event) override;
422
426 void selected(size_t index) override;
427
431 void selected(const std::list<size_t>& indexes);
432
437 void selected(size_t start_index, size_t end_index);
438
442 void deselected(size_t index);
443
447 void deselected(const std::list<size_t>& indexes);
448
453 void deselected(size_t start_index, size_t end_index);
454
458 void select(size_t index);
459
463 void select(const std::list<size_t>& indexes);
464
469 void select(size_t start_index, size_t end_index);
470
475
479 void deselect(size_t index);
480
484 void deselect(const std::list<size_t>& indexes);
485
490 void deselect(size_t start_index, size_t end_index);
491
496
500 void toggle(size_t index);
501
505 void toggle(const std::list<size_t>& indexes);
506
510 void toggle(size_t start_index, size_t end_index);
511
516
522 EGT_NODISCARD std::list<size_t> selected() const;
523
529 EGT_NODISCARD std::list<size_t> deselected() const;
530
531protected:
532
533 bool selected(const std::list<size_t>& indexes,
534 bool selected,
535 std::list<size_t>& selected_update,
536 std::list<size_t>& deselected_update);
537 bool select(const std::list<size_t>& indexes,
538 bool selected,
539 std::list<size_t>& selected_update,
540 std::list<size_t>& deselected_update);
541};
542
543}
544}
545
546#endif
Positions and sizes widgets by Orientation.
Definition sizer.h:49
A Frame is a Widget that has children widgets.
Definition frame.h:45
ListBoxBase is an abstract class that can be used as a base class to implement list boxes.
Definition list.h:34
void add_item_at(const std::shared_ptr< StringItem > &item, size_t pos)
Insert a new item to the list at the specified position.
BoxSizer m_sizer
Internal sizer used to layout items.
Definition list.h:237
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:234
Signal on_items_changed
Invoked when items are added or removed.
Definition list.h:49
Signal on_selected_changed
Event signal.
Definition list.h:44
EGT_NODISCARD size_t item_count() const
Return the number of items in the list.
Definition list.h:108
void scroll_beginning()
Scroll all the way to the beginning of the list, either horizontally or vertically.
virtual ~ListBoxBase()=default
void scroll_bottom()
Scroll all the way to the bottom of the list.
ListBoxBase(Serializer::Properties &props) noexcept
EGT_NODISCARD Orientation orient() const
Get the orientation of the list: either vertical or horizontal.
Definition list.h:224
std::vector< std::shared_ptr< StringItem > > ItemArray
Item array type.
Definition list.h:53
void resize(const Size &s) override
Resize the widget.
Definition list.h:86
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
void scroll_offset(int offset)
Scroll an offset value either horizontally or vertically.
void add_item_at(StringItem &item, size_t pos)
Insert a new item to the list at the specified position.
Definition list.h:150
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.
virtual void selected(size_t index)=0
Select an item by index.
void remove_item_at(size_t pos)
Remove the item at the specified position.
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:129
ListBoxBase(const ItemArray &items=ItemArray()) noexcept
EGT_NODISCARD std::shared_ptr< StringItem > item_at(size_t index) const
Get the currently selected index item from list.
ListBoxMulti a listbox that manages a selectable list of items and allows multi selection.
Definition list.h:363
void select(const std::list< size_t > &indexes)
Select a list of items by indexes, others items are left unchanged.
void toggle(size_t index)
Toggle an item by index.
void select(size_t index)
Select an item by index, others items are left unchanged.
bool select(const std::list< size_t > &indexes, bool selected, std::list< size_t > &selected_update, std::list< size_t > &deselected_update)
void deselected(size_t index)
Set the deselected item, other items are selected.
void deselect(size_t start_index, size_t end_index)
Deselect a range [start_index, end_index) of items, others items are left unchanged.
void deselect_all()
Deselect all the items.
void selected(size_t start_index, size_t end_index)
Set a range [start_index, end_index) of selected items, other items are deselected.
Signal< std::list< size_t > > on_deselected
Invoked when items are deselected with the list of the indexes of the items deselected.
Definition list.h:380
bool selected(const std::list< size_t > &indexes, bool selected, std::list< size_t > &selected_update, std::list< size_t > &deselected_update)
void deselect(size_t index)
Deselect an item by index, others items are left unchanged.
void select(size_t start_index, size_t end_index)
Select a range [start_index, end_index) of items, others items are left unchanged.
void deselected(const std::list< size_t > &indexes)
Set a list of deselected items, other items are selected.
EGT_NODISCARD std::list< size_t > selected() const
Get the currently selected indexes.
void selected(size_t index) override
Set the selected item, other items are deselected.
Signal< std::list< size_t > > on_selected
Event signal.
Definition list.h:374
void toggle_all()
Toggle all the items.
void toggle(size_t start_index, size_t end_index)
Toggle a range [start_index, end_index) of items.
ListBoxMulti(Serializer::Properties &props, bool is_derived) noexcept
ListBoxMulti(Serializer::Properties &props) noexcept
Definition list.h:410
void selected(const std::list< size_t > &indexes)
Set a list of selected items, other items are deselected.
void toggle(const std::list< size_t > &indexes)
Toggle a list of items by indexes.
ListBoxMulti(const ItemArray &items=ItemArray()) noexcept
void handle(Event &event) override
Handle an event.
EGT_NODISCARD std::list< size_t > deselected() const
Get the currently deselected indexes.
void deselected(size_t start_index, size_t end_index)
Set a range [start_index, end_index) of deselected items, other items are selected.
void select_all()
Select all the items.
void deselect(const std::list< size_t > &indexes)
Deselect a list of items by indexes, others items are left unchanged.
ListBox that manages a selectable list of widgets.
Definition list.h:263
Signal< size_t > on_selected
Event signal.
Definition list.h:273
EGT_NODISCARD ssize_t selected() const
Get the currently selected index.
ListBox(Serializer::Properties &props, bool is_derived) noexcept
void selected(size_t index) override
Select an item by index.
ListBox(const Rect &rect) noexcept
Definition list.h:288
ListBox(Serializer::Properties &props) noexcept
Definition list.h:315
ListBox(const ItemArray &items=ItemArray()) noexcept
Definition list.h:280
void handle(Event &event) override
Handle an event.
ListBox(Frame &parent, const ItemArray &items={}, const Rect &rect={}) noexcept
Definition list.h:307
ListBox(const ItemArray &items, const Rect &rect) noexcept
Definition list.h:297
A scrollable view.
Definition view.h:35
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:679
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