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
MenuBar.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_MENU_BAR_HPP
26
#define TGUI_MENU_BAR_HPP
27
28
#include <TGUI/Renderers/MenuBarRenderer.hpp>
29
#include <TGUI/Widgets/MenuWidgetBase.hpp>
30
32
33
namespace
tgui
34
{
38
class
TGUI_API MenuBar :
public
MenuWidgetBase
39
{
40
public
:
41
using
Ptr
= std::shared_ptr<MenuBar>;
42
using
ConstPtr
= std::shared_ptr<const MenuBar>;
43
44
static
constexpr
char
StaticWidgetType
[] =
"MenuBar"
;
45
53
explicit
MenuBar(
const
char
* typeName =
StaticWidgetType
,
bool
initRenderer =
true
);
54
60
[[nodiscard]]
static
MenuBar::Ptr
create
();
61
69
[[nodiscard]]
static
MenuBar::Ptr
copy
(
const
MenuBar::ConstPtr
& menuBar);
70
75
[[nodiscard]]
MenuBarRenderer
*
getSharedRenderer
()
override
;
76
[[nodiscard]]
const
MenuBarRenderer
*
getSharedRenderer
()
const override
;
77
83
[[nodiscard]]
MenuBarRenderer
*
getRenderer
()
override
;
84
98
template
<
typename
Func,
typename
... Args>
99
unsigned
int
connectMenuItem
(
const
String
& menu,
const
String
& menuItem, Func&& handler,
const
Args&... args)
100
{
101
return
connectMenuItem
({menu, menuItem}, std::forward<Func>(handler), args...);
102
}
103
117
template
<
typename
Func,
typename
... Args>
118
unsigned
int
connectMenuItem
(
const
std::vector<String>& hierarchy, Func&& handler,
const
Args&... args)
119
{
120
return
onMenuItemClick
.connect(
121
[=](
const
std::vector<String>& clickedMenuItem)
122
{
123
if
(clickedMenuItem == hierarchy)
124
std::invoke(handler, args...);
125
});
126
}
127
135
void
setSize
(
const
Layout2d
& size)
override
;
136
using
Widget::setSize
;
137
145
void
setEnabled
(
bool
enabled)
override
;
146
152
void
addMenu
(
const
String
& text);
153
171
bool
addMenuItem
(
const
String
& text);
172
191
bool
addMenuItem
(
const
String
& menu,
const
String
& text);
192
209
bool
addMenuItem
(
const
std::vector<String>& hierarchy,
bool
createParents =
true
);
210
227
bool
changeMenuItem
(
const
std::vector<String>& hierarchy,
const
String
& text);
228
232
void
removeAllMenus
();
233
243
bool
removeMenu
(
const
String
& menu);
244
253
bool
removeMenuItem
(
const
String
& menu,
const
String
& menuItem);
254
268
bool
removeMenuItem
(
const
std::vector<String>& hierarchy,
bool
removeParentsWhenEmpty =
true
);
269
277
bool
removeMenuItems
(
const
String
& menu);
278
290
bool
removeSubMenuItems
(
const
std::vector<String>& hierarchy);
291
298
bool
setMenuEnabled
(
const
String
& menu,
bool
enabled);
299
305
[[nodiscard]]
bool
getMenuEnabled
(
const
String
& menu)
const
;
306
314
bool
setMenuItemEnabled
(
const
String
& menu,
const
String
& menuItem,
bool
enabled);
315
322
bool
setMenuItemEnabled
(
const
std::vector<String>& hierarchy,
bool
enabled);
323
330
[[nodiscard]]
bool
getMenuItemEnabled
(
const
String
& menu,
const
String
& menuItem)
const
;
331
337
[[nodiscard]]
bool
getMenuItemEnabled
(
const
std::vector<String>& hierarchy)
const
;
338
346
void
setMinimumSubMenuWidth
(
float
minimumWidth);
347
355
[[nodiscard]]
float
getMinimumSubMenuWidth
()
const
;
356
362
void
setInvertedMenuDirection
(
bool
invertDirection);
363
369
[[nodiscard]]
bool
getInvertedMenuDirection
()
const
;
370
375
[[nodiscard]] std::vector<GetMenusElement>
getMenus
()
const
;
376
380
void
closeMenu
()
override
;
381
387
[[nodiscard]]
bool
isMouseOnWidget
(Vector2f pos)
const override
;
388
392
bool
leftMousePressed
(Vector2f pos)
override
;
393
397
void
leftMouseReleased(Vector2f pos)
override
;
398
402
void
mouseMoved(Vector2f pos)
override
;
403
410
void
draw
(
BackendRenderTarget
& target,
RenderStates
states)
const override
;
411
413
414
protected
:
420
void
rendererChanged
(
const
String
& property)
override
;
421
425
[[nodiscard]] std::unique_ptr<DataIO::Node>
save
(SavingRenderersMap& renderers)
const override
;
426
430
void
load
(
const
std::unique_ptr<DataIO::Node>& node,
const
LoadingRenderersMap& renderers)
override
;
431
435
void
updateTextSize
()
override
;
436
438
// Makes a copy of the widget
440
[[nodiscard]]
Widget::Ptr
clone
()
const override
;
441
446
void
openMenu
(std::size_t menuIndex);
447
452
void
drawMenusOnBar(
BackendRenderTarget
& target,
RenderStates
states)
const
;
453
458
[[nodiscard]] Vector2f calculateMenuOffset(std::size_t visibleMenuIdx)
const
;
459
464
void
emitMenuItemClick(
const
std::vector<String>& hierarchy)
override
;
465
470
void
deselectDeepestItem()
override
;
471
478
[[nodiscard]]
bool
isMouseOnOpenMenu(Vector2f pos)
const override
;
479
484
[[nodiscard]]
float
getDefaultMenuItemHeight()
const override
;
485
490
void
leftMouseReleasedOnMenu()
override
;
491
497
void
mouseMovedOnMenu(Vector2f pos)
override
;
498
505
void
drawOpenMenu(
BackendRenderTarget
& target,
RenderStates
states)
const override
;
506
508
509
protected
:
510
std::vector<Menu> m_menus;
511
int
m_visibleMenu = -1;
512
Sprite
m_spriteBackground;
513
};
514
}
// namespace tgui
515
516
#endif
// TGUI_MENU_BAR_HPP
tgui::BackendRenderTarget
Base class for render targets.
Definition
BackendRenderTarget.hpp:46
tgui::Layout2d
Class to store the position or size of a widget.
Definition
Layout.hpp:320
tgui::MenuBarRenderer
Renderer for the MenuBar widget.
Definition
MenuBarRenderer.hpp:38
tgui::MenuBar::setMenuItemEnabled
bool setMenuItemEnabled(const String &menu, const String &menuItem, bool enabled)
Enable or disable a menu item.
tgui::MenuBar::removeMenuItems
bool removeMenuItems(const String &menu)
Removes all menu items from a menu.
tgui::MenuBar::setMenuEnabled
bool setMenuEnabled(const String &menu, bool enabled)
Enable or disable an entire menu.
tgui::MenuBar::setEnabled
void setEnabled(bool enabled) override
Enables or disables the widget.
tgui::MenuBar::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
MenuBar.hpp:118
tgui::MenuBar::removeSubMenuItems
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
tgui::MenuBar::clone
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
tgui::MenuBar::changeMenuItem
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing menu item.
tgui::MenuBar::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::MenuBar::getMinimumSubMenuWidth
float getMinimumSubMenuWidth() const
Returns the minimum width of the submenus.
tgui::MenuBar::rendererChanged
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
tgui::MenuBar::addMenuItem
bool addMenuItem(const String &menu, const String &text)
Adds a new menu item to an existing menu.
tgui::MenuBar::addMenuItem
bool addMenuItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new menu item (or sub menu item).
tgui::MenuBar::leftMousePressed
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
tgui::MenuBar::getInvertedMenuDirection
bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
tgui::MenuBar::openMenu
void openMenu(std::size_t menuIndex)
Opens a menu.
tgui::MenuBar::ConstPtr
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition
MenuBar.hpp:42
tgui::MenuBar::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::MenuBar::draw
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
tgui::MenuBar::load
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
tgui::MenuBar::getMenus
std::vector< GetMenusElement > getMenus() const
Returns the menus and their menu items, including submenus.
tgui::MenuBar::addMenu
void addMenu(const String &text)
Adds a new menu.
tgui::MenuBar::getRenderer
MenuBarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
tgui::MenuBar::removeAllMenus
void removeAllMenus()
Removes all menus.
tgui::MenuBar::StaticWidgetType
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition
MenuBar.hpp:44
tgui::MenuBar::updateTextSize
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
tgui::MenuBar::removeMenuItem
bool removeMenuItem(const String &menu, const String &menuItem)
Removes a menu item.
tgui::MenuBar::closeMenu
void closeMenu() override
Closes the open menu when one of the menus is open.
tgui::MenuBar::copy
static MenuBar::Ptr copy(const MenuBar::ConstPtr &menuBar)
Makes a copy of another menu bar.
tgui::MenuBar::getMenuEnabled
bool getMenuEnabled(const String &menu) const
Check if an entire menu is enabled or disabled.
tgui::MenuBar::setMinimumSubMenuWidth
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
tgui::MenuBar::Ptr
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition
MenuBar.hpp:41
tgui::MenuBar::create
static MenuBar::Ptr create()
Creates a new menu bar widget.
tgui::MenuBar::addMenuItem
bool addMenuItem(const String &text)
Adds a new menu item to the last added menu.
tgui::MenuBar::setMenuItemEnabled
bool setMenuItemEnabled(const std::vector< String > &hierarchy, bool enabled)
Enable or disable a menu item.
tgui::MenuBar::removeMenuItem
bool removeMenuItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a menu item (or sub menu item).
tgui::MenuBar::getMenuItemEnabled
bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
tgui::MenuBar::connectMenuItem
unsigned int connectMenuItem(const String &menu, 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
MenuBar.hpp:99
tgui::MenuBar::setInvertedMenuDirection
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
tgui::MenuBar::getSharedRenderer
MenuBarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
tgui::MenuBar::setSize
void setSize(const Layout2d &size) override
Changes the size of the menu bar.
tgui::MenuBar::getMenuItemEnabled
bool getMenuItemEnabled(const String &menu, const String &menuItem) const
Check if a menu item is enabled or disabled.
tgui::MenuBar::removeMenu
bool removeMenu(const String &menu)
Removes a menu.
tgui::MenuWidgetBase::onMenuItemClick
SignalItemHierarchy onMenuItemClick
Definition
MenuWidgetBase.hpp:312
tgui::Sprite
Definition
Sprite.hpp:45
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::RenderStates
States used for drawing.
Definition
RenderStates.hpp:38
Generated on
for TGUI by
1.18.0