TGUI 1.13
Loading...
Searching...
No Matches
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
33namespace 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
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
Base class for render targets.
Definition BackendRenderTarget.hpp:46
Class to store the position or size of a widget.
Definition Layout.hpp:320
Renderer for the MenuBar widget.
Definition MenuBarRenderer.hpp:38
bool setMenuItemEnabled(const String &menu, const String &menuItem, bool enabled)
Enable or disable a menu item.
bool removeMenuItems(const String &menu)
Removes all menu items from a menu.
bool setMenuEnabled(const String &menu, bool enabled)
Enable or disable an entire menu.
void setEnabled(bool enabled) override
Enables or disables the widget.
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
bool removeSubMenuItems(const std::vector< String > &hierarchy)
Removes a all menu items below a (sub) menu.
Widget::Ptr clone() const override
Makes a copy of the widget if you don't know its exact type.
bool changeMenuItem(const std::vector< String > &hierarchy, const String &text)
Changes the text of an existing 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.
float getMinimumSubMenuWidth() const
Returns the minimum width of the submenus.
void rendererChanged(const String &property) override
Function called when one of the properties of the renderer is changed.
bool addMenuItem(const String &menu, const String &text)
Adds a new menu item to an existing menu.
bool addMenuItem(const std::vector< String > &hierarchy, bool createParents=true)
Adds a new menu item (or sub menu item).
bool leftMousePressed(Vector2f pos) override
Called by the parent when the left mouse button goes down on top of the widget.
bool getInvertedMenuDirection() const
Returns whether the menus open above or below the menu bar.
void openMenu(std::size_t menuIndex)
Opens a menu.
std::shared_ptr< const MenuBar > ConstPtr
Shared constant widget pointer.
Definition MenuBar.hpp:42
bool isMouseOnWidget(Vector2f pos) const override
Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget...
void draw(BackendRenderTarget &target, RenderStates states) const override
Draw the widget to a render target.
void load(const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
Loads the widget from a tree of nodes.
std::vector< GetMenusElement > getMenus() const
Returns the menus and their menu items, including submenus.
void addMenu(const String &text)
Adds a new menu.
MenuBarRenderer * getRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void removeAllMenus()
Removes all menus.
static constexpr char StaticWidgetType[]
Type name of the widget.
Definition MenuBar.hpp:44
void updateTextSize() override
Called when the text size is changed (either by setTextSize or via the renderer).
bool removeMenuItem(const String &menu, const String &menuItem)
Removes a menu item.
void closeMenu() override
Closes the open menu when one of the menus is open.
static MenuBar::Ptr copy(const MenuBar::ConstPtr &menuBar)
Makes a copy of another menu bar.
bool getMenuEnabled(const String &menu) const
Check if an entire menu is enabled or disabled.
void setMinimumSubMenuWidth(float minimumWidth)
Changes the minimum width of the submenus.
std::shared_ptr< MenuBar > Ptr
Shared widget pointer.
Definition MenuBar.hpp:41
static MenuBar::Ptr create()
Creates a new menu bar widget.
bool addMenuItem(const String &text)
Adds a new menu item to the last added menu.
bool setMenuItemEnabled(const std::vector< String > &hierarchy, bool enabled)
Enable or disable a menu item.
bool removeMenuItem(const std::vector< String > &hierarchy, bool removeParentsWhenEmpty=true)
Removes a menu item (or sub menu item).
bool getMenuItemEnabled(const std::vector< String > &hierarchy) const
Check if a menu item is enabled or disabled.
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
void setInvertedMenuDirection(bool invertDirection)
Changes whether the menus open above or below the menu bar.
MenuBarRenderer * getSharedRenderer() override
Returns the renderer, which gives access to functions that determine how the widget is displayed.
void setSize(const Layout2d &size) override
Changes the size of the menu bar.
bool getMenuItemEnabled(const String &menu, const String &menuItem) const
Check if a menu item is enabled or disabled.
bool removeMenu(const String &menu)
Removes a menu.
SignalItemHierarchy onMenuItemClick
Definition MenuWidgetBase.hpp:312
Definition Sprite.hpp:45
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
States used for drawing.
Definition RenderStates.hpp:38