26#ifndef TGUI_SIGNAL_HPP
27#define TGUI_SIGNAL_HPP
31#include <TGUI/Global.hpp>
32#include <TGUI/Vector2f.hpp>
33#include <TGUI/Animation.hpp>
34#include <TGUI/Color.hpp>
35#include <SFML/System/String.hpp>
52 class SignalWidgetBase;
54 namespace internal_signal
56 extern TGUI_API std::deque<const void*> parameters;
65 static constexpr const char*
const SizeChanged =
"SizeChanged";
66 static constexpr const char*
const Focused =
"Focused";
67 static constexpr const char*
const Unfocused =
"Unfocused";
69 static constexpr const char*
const MouseLeft =
"MouseLeft";
77 static constexpr const char*
const Clicked =
"Clicked";
85 static constexpr const char*
const Pressed =
"Pressed";
91 static constexpr const char*
const Closed =
"Closed";
92 static constexpr const char*
const Minimized =
"Minimized";
93 static constexpr const char*
const Maximized =
"Maximized";
99 static constexpr const char*
const ColorChange =
"ColorChanged";
100 static constexpr const char*
const OkPressed =
"OkPressed";
158 static constexpr const char*
const Clicked =
"Clicked";
172 static constexpr const char*
const Full =
"Full";
177 static constexpr const char*
const Checked =
"Checked";
178 static constexpr const char*
const Unchecked =
"Unchecked";
179 static constexpr const char*
const Changed =
"Changed";
217 static constexpr const char*
const Expanded =
"Expanded";
218 static constexpr const char*
const Collapsed =
"Collapsed";
234 using Delegate = std::function<void()>;
235 using DelegateEx = std::function<void(std::shared_ptr<Widget>,
const std::string&)>;
247 Signal(std::string&& name, std::size_t extraParameters = 0) :
248 m_name{std::move(name)}
250 if (1 + extraParameters > internal_signal::parameters.size())
251 internal_signal::parameters.resize(1 + extraParameters);
286 unsigned int connect(
const Delegate& handler);
296 unsigned int connect(
const DelegateEx& handler);
324 if (m_handlers.empty() || !m_enabled)
327 internal_signal::parameters[0] =
static_cast<const void*
>(&widget);
330 auto handlers = m_handlers;
331 for (
auto& handler : handlers)
382 virtual unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const;
392 static std::shared_ptr<Widget> getWidget();
398 bool m_enabled =
true;
400 std::map<
unsigned int, std::function<void()>> m_handlers;
403 #define TGUI_SIGNAL_VALUE_DECLARATION(TypeName, Type) \
407 class TGUI_API Signal##TypeName : public Signal \
410 using Delegate##TypeName = std::function<void(Type)>; \
411 using Delegate##TypeName##Ex = std::function<void(std::shared_ptr<Widget>, const std::string&, Type)>; \
412 using Signal::connect; \
417 Signal##TypeName(std::string&& name) : \
418 Signal{std::move(name), 1} \
429 unsigned int connect(const Delegate##TypeName& handler); \
438 unsigned int connect(const Delegate##TypeName##Ex& handler); \
444 bool emit(const Widget* widget, Type param) \
446 if (m_handlers.empty()) \
449 internal_signal::parameters[1] = static_cast<const void*>(¶m); \
450 return Signal::emit(widget); \
459 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters) const override; \
462 TGUI_SIGNAL_VALUE_DECLARATION(Int,
int)
463 TGUI_SIGNAL_VALUE_DECLARATION(UInt,
unsigned int)
464 TGUI_SIGNAL_VALUE_DECLARATION(Bool,
bool)
465 TGUI_SIGNAL_VALUE_DECLARATION(Float,
float)
466 TGUI_SIGNAL_VALUE_DECLARATION(
String,
const sf::String&)
478 using DelegateRange = std::function<void(
float,
float)>;
479 using DelegateRangeEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
float,
float)>;
486 Signal{std::move(name), 2}
498 unsigned int connect(
const DelegateRange& handler);
508 unsigned int connect(
const DelegateRangeEx& handler);
515 bool emit(
const Widget* widget,
float value1,
float value2);
525 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const override;
535 class TGUI_API SignalChildWindow :
public Signal
539 using DelegateChildWindow = std::function<void(std::shared_ptr<ChildWindow>)>;
540 using DelegateChildWindowEx = std::function<void(std::shared_ptr<Widget>,
const std::string&, std::shared_ptr<ChildWindow>)>;
548 Signal{std::move(name), 1}
560 unsigned int connect(
const DelegateChildWindow& handler);
570 unsigned int connect(
const DelegateChildWindowEx& handler);
587 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const override;
597 class TGUI_API SignalItem :
public Signal
601 using DelegateItem = std::function<void(
const sf::String&)>;
602 using DelegateItemAndId = std::function<void(
const sf::String&,
const sf::String&)>;
603 using DelegateItemIndex = std::function<void(
int)>;
604 using DelegateItemEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
const sf::String&)>;
605 using DelegateItemAndIdEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
const sf::String&,
const sf::String&)>;
606 using DelegateItemIndexEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
int)>;
614 Signal{std::move(name), 3}
626 unsigned int connect(
const DelegateItem& handler);
636 unsigned int connect(
const DelegateItemEx& handler);
646 unsigned int connect(
const DelegateItemAndId& handler);
656 unsigned int connect(
const DelegateItemAndIdEx& handler);
666 unsigned int connect(
const DelegateItemIndex& handler);
676 unsigned int connect(
const DelegateItemIndexEx& handler);
683 bool emit(
const Widget* widget,
const sf::String& item,
const sf::String&
id,
int index)
685 if (m_handlers.empty())
688 internal_signal::parameters[1] =
static_cast<const void*
>(&item);
689 internal_signal::parameters[2] =
static_cast<const void*
>(&id);
690 internal_signal::parameters[3] =
static_cast<const void*
>(&index);
702 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const override;
725 Signal{std::move(name), 2}
735 if (m_handlers.empty())
738 internal_signal::parameters[1] =
static_cast<const void*
>(&type);
739 internal_signal::parameters[2] =
static_cast<const void*
>(&visible);
751 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const override;
765 using DelegateMenuItem = std::function<void(
const sf::String&)>;
766 using DelegateMenuItemFull = std::function<void(
const std::vector<sf::String>&)>;
767 using DelegateMenuItemEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
const sf::String&)>;
768 using DelegateMenuItemFullEx = std::function<void(std::shared_ptr<Widget>,
const std::string&,
const std::vector<sf::String>&)>;
776 Signal{std::move(name), 2}
788 unsigned int connect(
const DelegateMenuItem& handler);
798 unsigned int connect(
const DelegateMenuItemEx& handler);
808 unsigned int connect(
const DelegateMenuItemFull& handler);
818 unsigned int connect(
const DelegateMenuItemFullEx& handler);
825 bool emit(
const Widget* widget,
const sf::String& item,
const std::vector<sf::String>& fullItem)
827 if (m_handlers.empty())
830 internal_signal::parameters[1] =
static_cast<const void*
>(&item);
831 internal_signal::parameters[2] =
static_cast<const void*
>(&fullItem);
843 unsigned int validateTypes(std::initializer_list<std::type_index> unboundParameters)
const override;
849#ifndef TGUI_REMOVE_DEPRECATED_CODE
863#if defined(__cpp_if_constexpr) && (__cpp_if_constexpr >= 201606L)
873 template <
typename Func,
typename... BoundArgs>
874 unsigned int connect(std::string signalName, Func&& handler,
const BoundArgs&... args);
886 template <
typename Func,
typename... Args,
typename std::enable_if<std::is_convertible<Func, std::function<void(
const Args&...)>>::value>::type* =
nullptr>
887 unsigned int connect(std::string signalName, Func&& handler,
const Args&... args);
900 template <
typename Func,
typename... BoundArgs,
typename std::enable_if<!std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value
901 && std::is_convertible<Func, std::function<void(
const BoundArgs&..., std::shared_ptr<Widget>,
const std::string&)>>::value>::type* =
nullptr>
902 unsigned int connect(std::string signalName, Func&& handler, BoundArgs&&... args);
914 template <
typename Func,
typename... BoundArgs,
typename std::enable_if<!std::is_convertible<Func, std::function<void(
const BoundArgs&...)>>::value
915 && !std::is_convertible<Func, std::function<void(
const BoundArgs&..., std::shared_ptr<Widget>,
const std::string&)>>::value>::type* =
nullptr>
916 unsigned int connect(std::string signalName, Func&& handler, BoundArgs&&... args);
928 template <
typename Func,
typename... BoundArgs>
929 unsigned int connect(std::initializer_list<std::string> signalNames, Func&& handler, BoundArgs&&... args);
939 bool disconnect(
unsigned int id);
947 void disconnectAll(std::string signalName);
953 void disconnectAll();
959 virtual Signal& getSignal(std::string signalName) = 0;
965 std::map<unsigned int, std::string> m_connectedSignals;
Child window widget.
Definition: ChildWindow.hpp:44
Wrapper for colors.
Definition: Color.hpp:49
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:695
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:513
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:739
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:575
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:452
Signal to which the user can subscribe to get callbacks from.
Definition: Signal.hpp:231
bool emit(const Widget *widget)
Call all connected signal handlers.
Definition: Signal.hpp:322
std::string getName() const
Returns the name given to the signal.
Definition: Signal.hpp:343
Signal(const Signal &other)
Copy constructor which will not copy the signal handlers.
bool isEnabled() const
Returns whether this signal calls the connected functions when triggered.
Definition: Signal.hpp:371
void disconnectAll()
Disconnect all signal handler from this signal.
void setEnabled(bool enabled)
Changes whether this signal calls the connected functions when triggered.
Definition: Signal.hpp:357
bool disconnect(unsigned int id)
Disconnect a signal handler from this signal.
Signal & operator=(const Signal &other)
Copy assignment operator which will not copy the signal handlers.
unsigned int connect(const Delegate &handler)
Connects a signal handler that will be called when this signal is emitted.
Signal & operator=(Signal &&other) noexcept=default
Default move assignment operator.
Signal(std::string &&name, std::size_t extraParameters=0)
Constructor.
Definition: Signal.hpp:247
unsigned int connect(const DelegateEx &handler)
Connects a signal handler that will be called when this signal is emitted.
Signal(Signal &&other) noexcept=default
Default move constructor.
Wrapper class to store strings.
Definition: String.hpp:119
Definition: Vector2f.hpp:39
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
ShowAnimationType
Type of animation to show/hide widget.
Definition: Animation.hpp:44
Definition: Signal.hpp:89
static constexpr const char *const EscapeKeyPressed
The escape key was pressed while the child window was focused. Optional parameter: pointer to the win...
Definition: Signal.hpp:94
static constexpr const char *const Maximized
The window was maximized. Optional parameter: pointer to the window.
Definition: Signal.hpp:93
static constexpr const char *const Minimized
The window was minimized. Optional parameter: pointer to the window.
Definition: Signal.hpp:92
static constexpr const char *const MousePressed
The mouse went down on the widget.
Definition: Signal.hpp:90
static constexpr const char *const Closed
The window was closed. Optional parameter: pointer to the window.
Definition: Signal.hpp:91
Definition: Signal.hpp:98
static constexpr const char *const OkPressed
The ok key was pressed. Optional parameter: color chosen.
Definition: Signal.hpp:100
static constexpr const char *const ColorChange
The color was changed. Optional parameter: color chosen.
Definition: Signal.hpp:99
Definition: Signal.hpp:104
static constexpr const char *const ItemSelected
An item was selected in the combo box. Optional parameter: selected item or its index.
Definition: Signal.hpp:105
Definition: Signal.hpp:109
static constexpr const char *const ReturnKeyPressed
The return key was pressed. Optional parameter: text in the edit box.
Definition: Signal.hpp:111
static constexpr const char *const TextChanged
The text was changed. Optional parameter: new text.
Definition: Signal.hpp:110
Definition: Signal.hpp:115
static constexpr const char *const ValueChanged
The value of the know changed. Optional parameter: new value.
Definition: Signal.hpp:116
Definition: Signal.hpp:120
static constexpr const char *const DoubleClicked
The label was double clicked. Optional parameter: text of the label.
Definition: Signal.hpp:121
Definition: Signal.hpp:125
static constexpr const char *const ItemSelected
An item was selected in the list box. Optional parameter: selected item or its index.
Definition: Signal.hpp:126
static constexpr const char *const MousePressed
The mouse went down on an item. Optional parameter: selected item or its index.
Definition: Signal.hpp:127
static constexpr const char *const MouseReleased
The mouse was released on one of the items. Optional parameter: selected item or its index.
Definition: Signal.hpp:128
static constexpr const char *const DoubleClicked
An item was double clicked. Optional parameter: selected item or its index.
Definition: Signal.hpp:129
Definition: Signal.hpp:133
static constexpr const char *const RightClicked
Right mouse clicked. Optional parameter: index of item below mouse (-1 when not on top of item)
Definition: Signal.hpp:136
static constexpr const char *const DoubleClicked
An item was double clicked. Optional parameter: selected item index.
Definition: Signal.hpp:135
static constexpr const char *const HeaderClicked
The header was clicked. Optional parameter: column index.
Definition: Signal.hpp:137
static constexpr const char *const ItemSelected
An item was selected in the list view. Optional parameter: selected item index (-1 when deselecting)
Definition: Signal.hpp:134
Definition: Signal.hpp:150
static constexpr const char *const ButtonPressed
One of the buttons was pressed. Optional parameter: text of the pressed button.
Definition: Signal.hpp:151
Definition: Signal.hpp:155
static constexpr const char *const MouseReleased
The mouse was released on top of the panel. Optional parameter: mouse position relative to panel.
Definition: Signal.hpp:157
static constexpr const char *const MousePressed
The mouse went down on the panel. Optional parameter: mouse position relative to panel.
Definition: Signal.hpp:156
static constexpr const char *const RightMousePressed
The right mouse button went down on the panel. Optional parameter: mouse position relative to panel.
Definition: Signal.hpp:159
static constexpr const char *const RightMouseReleased
The right mouse button was released on top of the panel. Optional parameter: mouse position relative ...
Definition: Signal.hpp:160
static constexpr const char *const Clicked
The panel was clicked. Optional parameter: mouse position relative to panel.
Definition: Signal.hpp:158
static constexpr const char *const RightClicked
The panel was right clicked. Optional parameter: mouse position relative to panel.
Definition: Signal.hpp:161
Definition: Signal.hpp:165
static constexpr const char *const DoubleClicked
The picture was double clicked. Optional parameter: mouse position relative to picture.
Definition: Signal.hpp:166
Definition: Signal.hpp:170
static constexpr const char *const Full
Value of the progress bar changed and he progress bar became full.
Definition: Signal.hpp:172
static constexpr const char *const ValueChanged
Value of the progress bar changed. Optional parameter: new value.
Definition: Signal.hpp:171
Definition: Signal.hpp:183
static constexpr const char *const RangeChanged
Value of the slider changed. Optional parameter: new value.
Definition: Signal.hpp:184
Definition: Signal.hpp:193
static constexpr const char *const ValueChanged
Value of the slider changed. Optional parameter: new value.
Definition: Signal.hpp:194
Definition: Signal.hpp:203
static constexpr const char *const TabSelected
A tab that was selected. Optional parameter: selected item.
Definition: Signal.hpp:204
Definition: Signal.hpp:208
static constexpr const char *const SelectionChanged
Selected text changed.
Definition: Signal.hpp:210
static constexpr const char *const TextChanged
The text was changed. Optional parameter: new text.
Definition: Signal.hpp:209
Definition: Signal.hpp:214
static constexpr const char *const RightClicked
A node was right clicked. Optional parameter: node below mouse.
Definition: Signal.hpp:219
static constexpr const char *const ItemSelected
An node was selected in the tree view. Optional parameter: selected node.
Definition: Signal.hpp:215
static constexpr const char *const DoubleClicked
A leaf node was double clicked. Optional parameter: selected node.
Definition: Signal.hpp:216
static constexpr const char *const Expanded
A branch node was expanded in the tree view. Optional parameter: expanded node.
Definition: Signal.hpp:217
static constexpr const char *const Collapsed
A branch node was collapsed in the tree view. Optional parameter: collapsed node.
Definition: Signal.hpp:218