TGUI
=
=
Download
Tutorials
1.x
0.9
0.8
Documentation
1.x
0.9
0.8
Examples
1.x
0.9
0.8
Community
TGUI
1.x-dev
Toggle main menu visibility
Loading...
Searching...
No Matches
include
TGUI
Widgets
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
33
namespace
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
122
void
openMenuAtMouseCursor
();
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
256
void
removeAllMenuItems
();
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
tgui::BackendRenderTarget
Base class for render targets.
Definition
BackendRenderTarget.hpp:46
tgui::ContextMenuRenderer
Renderer for the ContextMenu widget.
Definition
ContextMenuRenderer.hpp:38
tgui::ContextMenu::changeMenuItem
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing menu item.
tgui::ContextMenu::addMenuItem
bool addMenuItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new sub menu item.
tgui::ContextMenu::save
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.
tgui::ContextMenu::ConstPtr
std::shared_ptr< const ContextMenu > ConstPtr
Shared constant widget pointer.
Definition
ContextMenu.hpp:44
tgui::ContextMenu::draw
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
tgui::ContextMenu::removeMenuItem
bool removeMenuItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a sub menu item.
tgui::ContextMenu::getMinimumMenuWidth
float getMinimumMenuWidth() const
Returns the minimum width of the menus.
tgui::ContextMenu::getMenuItemEnabled
bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
tgui::ContextMenu::Ptr
std::shared_ptr< ContextMenu > Ptr
Shared widget pointer.
Definition
ContextMenu.hpp:43
tgui::ContextMenu::setMinimumMenuWidth
void setMinimumMenuWidth(float minimumWidth)
Changes the minimum width of the menus.
tgui::ContextMenu::setSize
void setSize(const Layout2d &size) override
This function is overriden to do nothing.
tgui::ContextMenu::copy
static ContextMenu::Ptr copy(const ContextMenu::ConstPtr &menuBar)
Makes a copy of another menu bar.
tgui::ContextMenu::getMenuItemEnabled
bool getMenuItemEnabled(const String &menuItem) const
Check if a menu item is enabled or disabled.
tgui::ContextMenu::rendererChanged
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
tgui::ContextMenu::getSharedRenderer
ContextMenuRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
tgui::ContextMenu::updateTextSize
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
tgui::ContextMenu::removeSubMenuItems
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
tgui::ContextMenu::openMenu
void openMenu(Vector2f position)
Opens the context menu at a certain position.
tgui::ContextMenu::getRenderer
ContextMenuRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
tgui::ContextMenu::connectMenuItem
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
tgui::ContextMenu::closeMenu
void closeMenu() override
Closes the context menu if it was open.
tgui::ContextMenu::create
static ContextMenu::Ptr create()
Creates a new menu bar widget.
tgui::ContextMenu::StaticWidgetType
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition
ContextMenu.hpp:46
tgui::ContextMenu::removeAllMenuItems
void removeAllMenuItems()
Removes all menu items.
tgui::ContextMenu::openMenuAtMouseCursor
void openMenuAtMouseCursor()
Opens the context menu at the last mouse cursor position.
tgui::ContextMenu::setMenuItemEnabled
bool setMenuItemEnabled(const String &menuItem, bool enabled)
Enable or disable a menu item.
tgui::ContextMenu::getItemHeight
float getItemHeight() const
Returns the height of the items in the context menu.
tgui::ContextMenu::openMenu
void openMenu()
Opens the context menu at the position of this widget.
tgui::ContextMenu::setEnabled
void setEnabled(bool enabled) override
Enables or disables the widget.
tgui::ContextMenu::getMenuItems
std::vector< GetMenusElement > getMenuItems() const
Returns the menu items, including submenus.
tgui::ContextMenu::isMouseOnWidget
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
tgui::ContextMenu::setMenuItemEnabled
bool setMenuItemEnabled(const std::vector< String > &hierarchy, bool enabled)
Enable or disable a menu item.
tgui::ContextMenu::connectMenuItem
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
tgui::ContextMenu::setItemHeight
void setItemHeight(float itemHeight)
Changes the height of the items in the context menu.
tgui::ContextMenu::isMenuOpen
bool isMenuOpen() const
Returns whether the context menu is open.
tgui::ContextMenu::clone
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
tgui::ContextMenu::removeMenuItem
bool removeMenuItem(const String &menuItem)
Removes a menu item.
tgui::ContextMenu::addMenuItem
void addMenuItem(const String &text)
Adds a new menu item to the menu.
tgui::ContextMenu::load
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
tgui::Layout2d
Class to store the position or size of a widget.
Definition
Layout.hpp:320
tgui::MenuWidgetBase::onMenuItemClick
SignalItemHierarchy onMenuItemClick
Definition
MenuWidgetBase.hpp:312
tgui::String
Wrapper class to store strings.
Definition
String.hpp:94
tgui::Widget::Ptr
std::shared_ptr< Widget > Ptr
Shared widget pointer.
Definition
Widget.hpp:85
tgui::Widget::setSize
virtual void setSize(const Layout2d &size)
Changes the size of the widget.
tgui
Namespace that contains all TGUI functions and classes.
Definition
AbsoluteOrRelativeValue.hpp:37
tgui::MenuWidgetBase::Menu
Definition
MenuWidgetBase.hpp:98
tgui::RenderStates
States used for drawing.
Definition
RenderStates.hpp:38
Generated on
for TGUI by
1.18.0