TGUI 1.13
Loading...
Searching...
No Matches
ContextMenu.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_CONTEXT_MENU_HPP
26#define TGUI_CONTEXT_MENU_HPP
27
28#include <TGUI/Renderers/ContextMenuRenderer.hpp>
29#include <TGUI/Widgets/MenuWidgetBase.hpp>
30
32
33namespace tgui
34{
40 class TGUI_API ContextMenu : public MenuWidgetBase
41 {
42 public:
43 using Ptr = std::shared_ptr<ContextMenu>;
44 using ConstPtr = std::shared_ptr<const ContextMenu>;
45
46 static constexpr char StaticWidgetType[] = "ContextMenu";
47
55 explicit ContextMenu(const char* typeName = StaticWidgetType, bool initRenderer = true);
56
62 [[nodiscard]] static ContextMenu::Ptr create();
63
71 [[nodiscard]] static ContextMenu::Ptr copy(const ContextMenu::ConstPtr& menuBar);
72
77 [[nodiscard]] ContextMenuRenderer* getSharedRenderer() override;
78 [[nodiscard]] const ContextMenuRenderer* getSharedRenderer() const override;
79
85 [[nodiscard]] ContextMenuRenderer* getRenderer() override;
86
93 [[nodiscard]] bool isMenuOpen() const;
94
102 void openMenu();
103
113 void openMenu(Vector2f position);
114
123
127 void closeMenu() override;
128
134 void setItemHeight(float itemHeight);
135
141 [[nodiscard]] float getItemHeight() const;
142
155 template <typename Func, typename... Args>
156 unsigned int connectMenuItem(const String& menuItem, Func&& handler, const Args&... args)
157 {
158 return connectMenuItem(std::vector<String>{menuItem}, std::forward<Func>(handler), args...);
159 }
160
173 template <typename Func, typename... Args>
174 unsigned int connectMenuItem(const std::vector<String>& hierarchy, Func&& handler, const Args&... args)
175 {
176 return onMenuItemClick.connect(
177 [=](const std::vector<String>& clickedMenuItem)
178 {
179 if (clickedMenuItem == hierarchy)
180 std::invoke(handler, args...);
181 });
182 }
183
191 void setSize(const Layout2d& size) override;
192 using Widget::setSize;
193
201 void setEnabled(bool enabled) override;
202
216 void addMenuItem(const String& text);
217
234 bool addMenuItem(const std::vector<String>& hierarchy, bool createParents = true);
235
251 bool changeMenuItem(const std::vector<String>& hierarchy, const String& text);
252
257
265 bool removeMenuItem(const String& menuItem);
266
279 bool removeMenuItem(const std::vector<String>& hierarchy, bool removeParentsWhenEmpty = true);
280
292 bool removeSubMenuItems(const std::vector<String>& hierarchy);
293
300 bool setMenuItemEnabled(const String& menuItem, bool enabled);
301
308 bool setMenuItemEnabled(const std::vector<String>& hierarchy, bool enabled);
309
315 [[nodiscard]] bool getMenuItemEnabled(const String& menuItem) const;
316
322 [[nodiscard]] bool getMenuItemEnabled(const std::vector<String>& hierarchy) const;
323
331 void setMinimumMenuWidth(float minimumWidth);
332
340 [[nodiscard]] float getMinimumMenuWidth() const;
341
346 [[nodiscard]] std::vector<GetMenusElement> getMenuItems() const;
347
353 [[nodiscard]] bool isMouseOnWidget(Vector2f pos) const override;
354
361 void draw(BackendRenderTarget& target, RenderStates states) const override;
362
363 protected:
369 void rendererChanged(const String& property) override;
370
374 [[nodiscard]] std::unique_ptr<DataIO::Node> save(SavingRenderersMap& renderers) const override;
375
379 void load(const std::unique_ptr<DataIO::Node>& node, const LoadingRenderersMap& renderers) override;
380
384 void updateTextSize() override;
385
387 // Makes a copy of the widget
389 [[nodiscard]] Widget::Ptr clone() const override;
390
395 void emitMenuItemClick(const std::vector<String>& hierarchy) override;
396
401 [[nodiscard]] float getDefaultMenuItemHeight() const override;
402
407 void leftMouseReleasedOnMenu() override;
408
415 [[nodiscard]] bool isMouseOnOpenMenu(Vector2f pos) const override;
416
422 void mouseMovedOnMenu(Vector2f pos) override;
423
428 void deselectDeepestItem() override;
429
436 void drawOpenMenu(BackendRenderTarget& target, RenderStates states) const override;
437
439
440 protected:
441 Menu m_menu;
442 bool m_menuOpen = false;
443 float m_itemHeight = 0;
444 };
445} // namespace tgui
446
447#endif // TGUI_CONTEXT_MENU_HPP
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Renderer for the ContextMenu widget.
Definition ContextMenuRenderer.hpp:38
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing menu item.
bool addMenuItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new sub menu item.
std::unique_ptr< DataIO::Node > save(SavingRenderersMap &renderers) const override
Saves the widget as a tree node in order to save it to a file.
std::shared_ptr< const ContextMenu > ConstPtr
Shared constant widget pointer.
Definition ContextMenu.hpp:44
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
bool removeMenuItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a sub menu item.
float getMinimumMenuWidth() const
Returns the minimum width of the menus.
bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
std::shared_ptr< ContextMenu > Ptr
Shared widget pointer.
Definition ContextMenu.hpp:43
void setMinimumMenuWidth(float minimumWidth)
Changes the minimum width of the menus.
void setSize(const Layout2d &size) override
This function is overriden to do nothing.
static ContextMenu::Ptr copy(const ContextMenu::ConstPtr &menuBar)
Makes a copy of another menu bar.
bool getMenuItemEnabled(const String &menuItem) const
Check if a menu item is enabled or disabled.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
ContextMenuRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
void openMenu(Vector2f position)
Opens the context menu at a certain position.
ContextMenuRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
unsigned int connectMenuItem(const std::vector< String > &hierarchy, Func &&handler, const Args &... args)
Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific ...
Definition ContextMenu.hpp:174
void closeMenu() override
Closes the context menu if it was open.
static ContextMenu::Ptr create()
Creates a new menu bar widget.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition ContextMenu.hpp:46
void removeAllMenuItems()
Removes all menu items.
void openMenuAtMouseCursor()
Opens the context menu at the last mouse cursor position.
bool setMenuItemEnabled(const String &menuItem, bool enabled)
Enable or disable a menu item.
float getItemHeight() const
Returns the height of the items in the context menu.
void openMenu()
Opens the context menu at the position of this widget.
void setEnabled(bool enabled) override
Enables or disables the widget.
std::vector< GetMenusElement > getMenuItems() const
Returns the menu items, including submenus.
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
bool setMenuItemEnabled(const std::vector< String > &hierarchy, bool enabled)
Enable or disable a menu item.
unsigned int connectMenuItem(const String &menuItem, Func &&handler, const Args &... args)
Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific ...
Definition ContextMenu.hpp:156
void setItemHeight(float itemHeight)
Changes the height of the items in the context menu.
bool isMenuOpen() const
Returns whether the context menu is open.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool removeMenuItem(const String &menuItem)
Removes a menu item.
void addMenuItem(const String &text)
Adds a new menu item to the menu.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
Class to store the position or size of a widget.
Definition Layout.hpp:320
SignalItemHierarchy onMenuItemClick
Definition MenuWidgetBase.hpp:312
Wrapper class to store strings.
Definition String.hpp:94
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition Widget.hpp:85
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
Definition MenuWidgetBase.hpp:98
States used for drawing.
Definition RenderStates.hpp:38