1.10
notebook.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_NOTEBOOK_H
7#define EGT_NOTEBOOK_H
8
14#include <egt/detail/meta.h>
15#include <egt/frame.h>
16#include <egt/signal.h>
17#include <memory>
18#include <string>
19#include <vector>
20
21namespace egt
22{
23inline namespace v1
24{
25
26class Notebook;
30class EGT_API NotebookTab : public Frame
31{
32public:
33
35 {
36 name("NotebookTab" + std::to_string(m_widgetid));
37
38 // tabs are not transparent by default
39 fill_flags(Theme::FillFlag::solid);
40 }
41
42 explicit NotebookTab(Serializer::Properties& props) noexcept
43 : NotebookTab(props, false)
44 {
45 }
46
47 using Frame::add;
48 void add(const std::shared_ptr<Widget>& widget) override
49 {
50 if (!widget)
51 return;
52
53 if (widget.get() == this)
54 throw std::runtime_error("cannot add a widget to itself");
55
56 assert(!widget->parent() && "widget already has parent!");
57
58 auto cell = std::dynamic_pointer_cast<NotebookTab>(widget);
59 if (cell)
60 throw std::invalid_argument("cannot have nested NotebookTab");
61
62 Frame::add(widget);
63 }
64
65protected:
66
67 explicit NotebookTab(Serializer::Properties& props, bool is_derived) noexcept
68 : Frame(props, true)
69 {
70 if (!is_derived)
71 deserialize_leaf(props);
72 }
73
74public:
75
79 virtual bool leave()
80 {
81 return true;
82 }
83
87 virtual void enter()
88 {
89 }
90
94 void select();
95};
96
102class EGT_API Notebook : public Frame
103{
104public:
105
119 explicit Notebook(const Rect& rect = {}) noexcept;
120
125 explicit Notebook(Frame& parent, const Rect& rect = {}) noexcept;
126
130 explicit Notebook(Serializer::Properties& props) noexcept
131 : Notebook(props, false)
132 {
133 }
134
135protected:
136
137 explicit Notebook(Serializer::Properties& props, bool is_derived) noexcept;
138
139public:
140
141 using Frame::add;
142
143 void add(const std::shared_ptr<Widget>& widget) override;
144
145 void remove(Widget* widget) override;
146
150 void selected(size_t index);
151
155 void selected(Widget* widget);
156
160 EGT_NODISCARD ssize_t selected() const { return m_selected; }
161
167 EGT_NODISCARD NotebookTab* get(size_t index) const;
168
169 void serialize(Serializer& serializer) const override;
170
175
176protected:
177
179 using CellArray = std::vector<std::weak_ptr<NotebookTab>>;
180
183
185 ssize_t m_selected{-1};
186};
187
188}
189}
190
191#endif
A Frame is a Widget that has children widgets.
Definition frame.h:45
A single layer of a Notebook.
Definition notebook.h:31
NotebookTab(Serializer::Properties &props, bool is_derived) noexcept
Definition notebook.h:67
virtual void enter()
Called when this tab is entered and before show() is invoked.
Definition notebook.h:87
void add(const std::shared_ptr< Widget > &widget) override
Add a child widget.
Definition notebook.h:48
virtual bool leave()
Definition notebook.h:79
NotebookTab()
Definition notebook.h:34
void select()
Select this tab.
NotebookTab(Serializer::Properties &props) noexcept
Definition notebook.h:42
Allows a collection of NotebookTab widgets to be shown one at a time.
Definition notebook.h:103
Notebook(Serializer::Properties &props, bool is_derived) noexcept
Notebook(const Rect &rect={}) noexcept
EGT_NODISCARD NotebookTab * get(size_t index) const
Get a widget at the specified index.
Signal on_selected_changed
Event signal.
Definition notebook.h:113
EGT_NODISCARD ssize_t selected() const
Get the currently selected index.
Definition notebook.h:160
CellArray m_cells
Array of notebook tabs.
Definition notebook.h:182
Notebook(Serializer::Properties &props) noexcept
Definition notebook.h:130
void add(const std::shared_ptr< Widget > &widget) override
Add a child widget.
void post_deserialize(Serializer::Properties &props) override
Resume deserializing of the widget after its children have been deserialized.
Notebook(Frame &parent, const Rect &rect={}) noexcept
void selected(size_t index)
Set the selected widget by index.
void serialize(Serializer &serializer) const override
Serialize the widget to the specified serializer.
void remove(Widget *widget) override
Remove a child widget.
void selected(Widget *widget)
Set the selected widget by widget.
std::vector< std::weak_ptr< NotebookTab > > CellArray
Type of array of notebook tabs.
Definition notebook.h:179
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
EGT framework namespace.
Definition animation.h:24