TGUI  0.8.9

Menu bar widget. More...

#include <TGUI/Widgets/MenuBar.hpp>

Inheritance diagram for tgui::MenuBar:
tgui::Widget tgui::SignalWidgetBase

Classes

struct  GetAllMenusElement
 Used for return value of getAllMenus. More...
 
struct  GetMenuListElement
 Used for return value of getMenuList. More...
 
struct  Menu
 

Public Types

typedef std::shared_ptr< MenuBarPtr
 Shared widget pointer.
 
typedef std::shared_ptr< const MenuBarConstPtr
 Shared constant widget pointer.
 
- Public Types inherited from tgui::Widget
typedef std::shared_ptr< WidgetPtr
 Shared widget pointer.
 
typedef std::shared_ptr< const WidgetConstPtr
 Shared constant widget pointer.
 

Public Member Functions

MenuBarRenderergetSharedRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
MenuBarRenderergetRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
template<typename Func , typename... Args>
unsigned int connectMenuItem (const sf::String &menu, const sf::String &menuItem, Func &&handler, const Args &... args)
 Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific menu item was clicked. More...
 
template<typename Func , typename... Args>
unsigned int connectMenuItem (const std::vector< sf::String > &hierarchy, Func &&handler, const Args &... args)
 Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific menu item was clicked. More...
 
void setSize (const Layout2d &size) override
 Changes the size of the menu bar. More...
 
void setEnabled (bool enabled) override
 Enables or disables the widget. More...
 
void addMenu (const sf::String &text)
 Adds a new menu. More...
 
bool addMenuItem (const sf::String &text)
 Adds a new menu item to the last added menu. More...
 
bool addMenuItem (const sf::String &menu, const sf::String &text)
 Adds a new menu item to an existing menu. More...
 
bool addMenuItem (const std::vector< sf::String > &hierarchy, bool createParents=true)
 Adds a new menu item (or sub menu item) More...
 
void removeAllMenus ()
 Removes all menus.
 
bool removeMenu (const sf::String &menu)
 Removes a menu. More...
 
bool removeMenuItem (const sf::String &menu, const sf::String &menuItem)
 Removes a menu item. More...
 
bool removeMenuItem (const std::vector< sf::String > &hierarchy, bool removeParentsWhenEmpty=true)
 Removes a menu item (or sub menu item) More...
 
bool removeMenuItems (const sf::String &menu)
 Removes all menu items from a menu. More...
 
bool removeSubMenuItems (const std::vector< sf::String > &hierarchy)
 Removes a all menu items below a (sub) menu. More...
 
bool setMenuEnabled (const sf::String &menu, bool enabled)
 Enable or disable an entire menu. More...
 
bool getMenuEnabled (const sf::String &menu) const
 Check if an entire menu is enabled or disabled. More...
 
bool setMenuItemEnabled (const sf::String &menu, const sf::String &menuItem, bool enabled)
 Enable or disable a menu item. More...
 
bool setMenuItemEnabled (const std::vector< sf::String > &hierarchy, bool enabled)
 Enable or disable a menu item. More...
 
bool getMenuItemEnabled (const sf::String &menu, const sf::String &menuItem) const
 Check if a menu item is enabled or disabled. More...
 
bool getMenuItemEnabled (const std::vector< sf::String > &hierarchy) const
 Check if a menu item is enabled or disabled. More...
 
void setTextSize (unsigned int size) override
 Changes the character size of the text. More...
 
void setMinimumSubMenuWidth (float minimumWidth)
 Changes the minimum width of the submenus. More...
 
float getMinimumSubMenuWidth () const
 Returns the distance between the text and the side of the menu item. More...
 
void setInvertedMenuDirection (bool invertDirection)
 Changes whether the menus open above or below the menu bar. More...
 
bool getInvertedMenuDirection () const
 Returns whether the menus open above or below the menu bar. More...
 
void closeMenu ()
 Closes the open menu when one of the menus is open.
 
bool mouseOnWidget (Vector2f pos) const override
 Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget. More...
 
virtual void setSize (const Layout2d &size)
 Changes the size of the widget. More...
 
void setSize (Layout width, Layout height)
 Changes the size of the widget. More...
 
- Public Member Functions inherited from tgui::Widget
 Widget ()
 Default constructor.
 
 Widget (const Widget &)
 Copy constructor.
 
 Widget (Widget &&)
 Move constructor.
 
 ~Widget ()
 Destructor.
 
Widgetoperator= (const Widget &)
 Overload of copy assignment operator.
 
Widgetoperator= (Widget &&)
 Move assignment.
 
void setRenderer (std::shared_ptr< RendererData > rendererData)
 Sets a new renderer for the widget. The renderer determines how the widget looks. More...
 
WidgetRenderergetSharedRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
WidgetRenderergetRenderer ()
 Returns the renderer, which gives access to functions that determine how the widget is displayed. More...
 
virtual void setPosition (const Layout2d &position)
 sets the position of the widget More...
 
void setPosition (Layout x, Layout y)
 Sets the position of the widget. More...
 
Vector2f getPosition () const
 Gets the position of the widget. More...
 
void setSize (Layout width, Layout height)
 Changes the size of the widget. More...
 
Vector2f getSize () const
 Returns the size of the widget. More...
 
virtual Vector2f getFullSize () const
 Returns the entire size that the widget is using. More...
 
virtual Vector2f getAbsolutePosition () const
 Get the absolute position of the widget instead of the relative position to its parent. More...
 
virtual Vector2f getWidgetOffset () const
 Returns the distance between the position where the widget is drawn and where the widget is placed. More...
 
virtual void showWithEffect (ShowAnimationType type, sf::Time duration)
 Shows the widget by introducing it with an animation. More...
 
virtual void hideWithEffect (ShowAnimationType type, sf::Time duration)
 Hides the widget by making it leave with an animation. More...
 
virtual void setVisible (bool visible)
 Shows or hides a widget. More...
 
bool isVisible () const
 Returns true when the widget is visible. More...
 
bool isEnabled () const
 Returns true when the widget is enabled. More...
 
virtual void setFocused (bool focused)
 Focus or unfocus the widget. More...
 
bool isFocused () const
 Returns true when the widget is focused and false otherwise. More...
 
const std::string & getWidgetType () const
 Returns the type of the widget. More...
 
ContainergetParent () const
 Returns a pointer to the parent widget. More...
 
bool isAnimationPlaying () const
 Returns whether there is an active animation (started with showWithEffect or hideWithEffect) More...
 
virtual void moveToFront ()
 Places the widget before all other widgets. More...
 
virtual void moveToBack ()
 Places the widget behind all other widgets. More...
 
void setUserData (std::any userData)
 Stores some data into the widget. More...
 
template<typename T >
getUserData () const
 Returns data stored in the widget. More...
 
void setInheritedFont (const Font &font)
 Sets the font of the widget that is used when no font is set in the renderer. More...
 
const FontgetInheritedFont () const
 Returns the font of the widget that is used when no font is set in the renderer. More...
 
void setInheritedOpacity (float opacity)
 Sets the opacity of the widget that will be multiplied with the opacity set in the renderer. More...
 
float getInheritedOpacity () const
 Returns the opacity of the widget that is multiplied with the opacity set in the renderer. More...
 
virtual unsigned int getTextSize () const
 Returns the character size of text in this widget. More...
 
void setToolTip (Widget::Ptr toolTip)
 Sets the tool tip that should be displayed when hovering over the widget. More...
 
Widget::Ptr getToolTip () const
 Returns the tool tip that is displayed when hovering over the widget. More...
 
void setWidgetName (const sf::String &name)
 Changes the name of a widget. More...
 
sf::String getWidgetName () const
 Returns the name of a widget. More...
 
void setFocusable (bool focusable)
 Changes whether a widget could be focused. More...
 
bool isFocusable () const
 Returns whether a widget could be focused. More...
 
virtual bool canGainFocus () const
 Returns whether the widget can currently gain focus. More...
 
bool isContainer () const
 Returns whether the widget is a container widget or not. More...
 
template<typename WidgetType >
std::shared_ptr< const WidgetType > cast () const
 Downcast const widget.
 
template<typename WidgetType >
std::shared_ptr< WidgetType > cast ()
 Downcast widget.
 
- Public Member Functions inherited from tgui::SignalWidgetBase
template<typename Func , typename... Args, typename std::enable_if< std::is_convertible< Func, std::function< void(const Args &...)> >::value >::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, const Args &... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs, typename std::enable_if<!std::is_convertible< Func, std::function< void(const BoundArgs &...)> >::value &&std::is_convertible< Func, std::function< void(const BoundArgs &..., std::shared_ptr< Widget >, const std::string &)> >::value , ::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, BoundArgs &&... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs, typename std::enable_if<!std::is_convertible< Func, std::function< void(const BoundArgs &...)> >::value &&!std::is_convertible< Func, std::function< void(const BoundArgs &..., std::shared_ptr< Widget >, const std::string &)> >::value , ::type * = nullptr>
unsigned int connect (std::string signalName, Func &&handler, BoundArgs &&... args)
 Connects a signal handler that will be called when this signal is emitted. More...
 
template<typename Func , typename... BoundArgs>
unsigned int connect (std::initializer_list< std::string > signalNames, Func &&handler, BoundArgs &&... args)
 Connect a signal handler to multiple signals. More...
 
bool disconnect (unsigned int id)
 Disconnects a signal handler. More...
 
void disconnectAll (std::string signalName)
 Disconnects all signal handler from a certain signal. More...
 
void disconnectAll ()
 Disconnects all signal handlers from signals.
 

Static Public Member Functions

static MenuBar::Ptr create ()
 Creates a new menu bar widget. More...
 
static MenuBar::Ptr copy (MenuBar::ConstPtr menuBar)
 Makes a copy of another menu bar. More...
 

Public Attributes

SignalItemHierarchy onMenuItemClick = {"MenuItemClicked"}
 
- Public Attributes inherited from tgui::Widget
SignalVector2f onPositionChange = {"PositionChanged"}
 The position of the widget changed. Optional parameter: new position.
 
SignalVector2f onSizeChange = {"SizeChanged"}
 The size of the widget changed. Optional parameter: new size.
 
Signal onFocus = {"Focused"}
 The widget was focused.
 
Signal onUnfocus = {"Unfocused"}
 The widget was unfocused.
 
Signal onMouseEnter = {"MouseEntered"}
 The mouse entered the widget.
 
Signal onMouseLeave = {"MouseLeft"}
 The mouse left the widget.
 
SignalAnimation onAnimationFinished = {"AnimationFinished"}
 A show or hide animation finished. Optional parameters: animation type, new widget visibility or both.
 

Protected Member Functions

SignalgetSignal (std::string signalName) override
 Retrieves a signal based on its name. More...
 
void rendererChanged (const std::string &property) override
 Function called when one of the properties of the renderer is changed. More...
 
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.
 
void load (const std::unique_ptr< DataIO::Node > &node, const LoadingRenderersMap &renderers) override
 Loads the widget from a tree of nodes.
 
Widget::Ptr clone () const override
 Makes a copy of the widget if you don't know its exact type. More...
 
- Protected Member Functions inherited from tgui::Widget
virtual void mouseEnteredWidget ()
 This function is called when the mouse enters the widget.
 

Detailed Description

Menu bar widget.

Member Function Documentation

◆ addMenu()

void tgui::MenuBar::addMenu ( const sf::String &  text)

Adds a new menu.

Parameters
textThe text written on the menu

◆ addMenuItem() [1/3]

bool tgui::MenuBar::addMenuItem ( const sf::String &  menu,
const sf::String &  text 
)

Adds a new menu item to an existing menu.

Parameters
menuThe name of the menu to which the menu item will be added
textThe text written on this menu item
Returns
True when the item was added, false when menu was not found
menuBar->addMenu("File");
menuBar->addMenu("Edit");
menuBar->addMenuItem("File", "Load");
menuBar->addMenuItem("File", "Save");
menuBar->addMenuItem("Edit", "Undo");

◆ addMenuItem() [2/3]

bool tgui::MenuBar::addMenuItem ( const sf::String &  text)

Adds a new menu item to the last added menu.

Parameters
textThe text written on this menu item
Returns
True when the item was added, false when the menu bar doesn't contain any menus yet
menuBar->addMenu("File");
menuBar->addMenuItem("Load");
menuBar->addMenuItem("Save");
menuBar->addMenu("Edit");
menuBar->addMenuItem("Undo");

◆ addMenuItem() [3/3]

bool tgui::MenuBar::addMenuItem ( const std::vector< sf::String > &  hierarchy,
bool  createParents = true 
)

Adds a new menu item (or sub menu item)

Parameters
hierarchyHierarchy of the menu items, starting with the menu and ending with menu item to be added
createParentsShould the hierarchy be created if it did not exist yet?
Returns
True when the item was added, false when createParents was false and the parents hierarchy does not exist or if hierarchy does not contain at least 2 elements.
menuBar->addMenuItem({"File", "Save"});
menuBar->addMenuItem({"View", "Messages", "Tags", "Important"});

◆ clone()

Widget::Ptr tgui::MenuBar::clone ( ) const
inlineoverrideprotectedvirtual

Makes a copy of the widget if you don't know its exact type.

This function should only be used when you don't know the type of the widget. If you know what kind of widget you are copying, you should use the copy function.

Returns
Copy of the widget

Implements tgui::Widget.

◆ connectMenuItem() [1/2]

template<typename Func , typename... Args>
unsigned int tgui::MenuBar::connectMenuItem ( const sf::String &  menu,
const sf::String &  menuItem,
Func &&  handler,
const Args &...  args 
)
inline

Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific menu item was clicked.

Parameters
menuMenu containing the menu item
menuItemMenu item which should trigger the signal
handlerCallback function to call
argsOptional extra arguments to pass to the signal handler when the signal is emitted
Returns
Unique id of the connection

The hierarchy does not need to exist yet and the signal will still exist when removing and readding the menu items.

◆ connectMenuItem() [2/2]

template<typename Func , typename... Args>
unsigned int tgui::MenuBar::connectMenuItem ( const std::vector< sf::String > &  hierarchy,
Func &&  handler,
const Args &...  args 
)
inline

Connects a signal handler to the "MenuItemClicked" callback that will only be called when a specific menu item was clicked.

Parameters
hierarchyHierarchy of the menu items, starting with the menu and ending with menu item that should trigger the signal when pressed
handlerCallback function to call
argsOptional extra arguments to pass to the signal handler when the signal is emitted
Returns
Unique id of the connection

The hierarchy does not need to exist yet and the signal will still exist when removing and readding the menu items.

◆ copy()

static MenuBar::Ptr tgui::MenuBar::copy ( MenuBar::ConstPtr  menuBar)
static

Makes a copy of another menu bar.

Parameters
menuBarThe other menu bar
Returns
The new menu bar

◆ create()

static MenuBar::Ptr tgui::MenuBar::create ( )
static

Creates a new menu bar widget.

Returns
The new menu bar

◆ getInvertedMenuDirection()

bool tgui::MenuBar::getInvertedMenuDirection ( ) const

Returns whether the menus open above or below the menu bar.

Returns
Do menus open above the bar instead of below like it does by default?

◆ getMenuEnabled()

bool tgui::MenuBar::getMenuEnabled ( const sf::String &  menu) const

Check if an entire menu is enabled or disabled.

Parameters
menuThe name of the menu to check
Returns
True if the menu is enabled, false if it was disabled or when the menu did not exist

◆ getMenuItemEnabled() [1/2]

bool tgui::MenuBar::getMenuItemEnabled ( const sf::String &  menu,
const sf::String &  menuItem 
) const

Check if a menu item is enabled or disabled.

Parameters
menuThe name of the menu in which the menu item is located
menuItemThe name of the menu item to check
Returns
True if the menu item is enabled, false if it was disabled or when the menu or menuItem did not exist

◆ getMenuItemEnabled() [2/2]

bool tgui::MenuBar::getMenuItemEnabled ( const std::vector< sf::String > &  hierarchy) const

Check if a menu item is enabled or disabled.

Parameters
hierarchyHierarchy of menu items, starting with the menu and ending with the menu item to check
Returns
True if the menu item is enabled, false if it was disabled or when the hierarchy was incorrect

◆ getMinimumSubMenuWidth()

float tgui::MenuBar::getMinimumSubMenuWidth ( ) const

Returns the distance between the text and the side of the menu item.

Returns
minimum width of the submenus
See also
setMinimumSubMenuWidth

◆ getRenderer()

MenuBarRenderer * tgui::MenuBar::getRenderer ( )

Returns the renderer, which gives access to functions that determine how the widget is displayed.

Returns
Temporary pointer to the renderer
Warning
After calling this function, the widget has its own copy of the renderer and it will no longer be shared.

◆ getSharedRenderer()

MenuBarRenderer * tgui::MenuBar::getSharedRenderer ( )

Returns the renderer, which gives access to functions that determine how the widget is displayed.

Returns
Temporary pointer to the renderer that may be shared with other widgets using the same renderer

◆ getSignal()

Signal & tgui::MenuBar::getSignal ( std::string  signalName)
overrideprotectedvirtual

Retrieves a signal based on its name.

Parameters
signalNameName of the signal
Returns
Signal that corresponds to the name
Exceptions
Exceptionwhen the name does not match any signal

Reimplemented from tgui::Widget.

◆ mouseOnWidget()

bool tgui::MenuBar::mouseOnWidget ( Vector2f  pos) const
overridevirtual

Returns whether the mouse position (which is relative to the parent widget) lies on top of the widget.

Returns
Is the mouse on top of the widget?

Implements tgui::Widget.

◆ removeMenu()

bool tgui::MenuBar::removeMenu ( const sf::String &  menu)

Removes a menu.

Any menu items that belong to this menu will be removed as well.

Parameters
menuThe name of the menu to remove
Returns
True when the menu was removed, false when menu was not found

◆ removeMenuItem() [1/2]

bool tgui::MenuBar::removeMenuItem ( const sf::String &  menu,
const sf::String &  menuItem 
)

Removes a menu item.

Parameters
menuThe name of the menu in which the menu item is located
menuItemThe name of the menu item to remove
Returns
True when the item was removed, false when menu or menuItem was not found

◆ removeMenuItem() [2/2]

bool tgui::MenuBar::removeMenuItem ( const std::vector< sf::String > &  hierarchy,
bool  removeParentsWhenEmpty = true 
)

Removes a menu item (or sub menu item)

Parameters
hierarchyHierarchy of the menu item, starting with the menu and ending with menu item to be deleted
removeParentsWhenEmptyAlso delete the parent of the deleted menu item if it has no other children
Returns
True when the menu item existed and was removed, false when hierarchy was incorrect
menuBar->removeMenuItem({"File", "Save"});
menuBar->removeMenuItem({"View", "Messages", "Tags", "Important"});

◆ removeMenuItems()

bool tgui::MenuBar::removeMenuItems ( const sf::String &  menu)

Removes all menu items from a menu.

Parameters
menuThe name of the menu for which all menu items should be removed
Returns
True when the menu existed and its children were removed, false when menu was not found

◆ removeSubMenuItems()

bool tgui::MenuBar::removeSubMenuItems ( const std::vector< sf::String > &  hierarchy)

Removes a all menu items below a (sub) menu.

Parameters
hierarchyHierarchy of the menu item, starting with the menu and ending with the sub menu containing the items
Returns
True when the menu item existed and its children were removed, false when hierarchy was incorrect
menuBar->removeSubMenuItems({"File", "Recent files"});

◆ rendererChanged()

void tgui::MenuBar::rendererChanged ( const std::string &  property)
overrideprotectedvirtual

Function called when one of the properties of the renderer is changed.

Parameters
propertyLowercase name of the property that was changed

Reimplemented from tgui::Widget.

◆ setEnabled()

void tgui::MenuBar::setEnabled ( bool  enabled)
overridevirtual

Enables or disables the widget.

Parameters
enabledIs the widget enabled?

The disabled widget will no longer receive events and thus no longer send callbacks. All widgets are enabled by default.

Reimplemented from tgui::Widget.

◆ setInvertedMenuDirection()

void tgui::MenuBar::setInvertedMenuDirection ( bool  invertDirection)

Changes whether the menus open above or below the menu bar.

Parameters
invertDirectionShould the menus open above the bar instead of below like it does by default?

◆ setMenuEnabled()

bool tgui::MenuBar::setMenuEnabled ( const sf::String &  menu,
bool  enabled 
)

Enable or disable an entire menu.

Parameters
menuThe name of the menu to enable or disable
enabledShould the menu be enabled or disabled?
Returns
True when the menu exists, false when menu was not found

◆ setMenuItemEnabled() [1/2]

bool tgui::MenuBar::setMenuItemEnabled ( const sf::String &  menu,
const sf::String &  menuItem,
bool  enabled 
)

Enable or disable a menu item.

Parameters
menuThe name of the menu in which the menu item is located
menuItemThe name of the menu item to enable or disable
enabledShould the menu item be enabled or disabled?
Returns
True when the menu item exists, false when menu or menuItem was not found

◆ setMenuItemEnabled() [2/2]

bool tgui::MenuBar::setMenuItemEnabled ( const std::vector< sf::String > &  hierarchy,
bool  enabled 
)

Enable or disable a menu item.

Parameters
hierarchyHierarchy of menu items, starting with the menu and ending with the menu item to enable/disable
enabledShould the menu item be enabled or disabled?
Returns
True when the menu item exists, false when hierarchy was incorrect

◆ setMinimumSubMenuWidth()

void tgui::MenuBar::setMinimumSubMenuWidth ( float  minimumWidth)

Changes the minimum width of the submenus.

When a submenu is displayed, the width will be either this or the width of the longest text in the submenu. The default minimum width is 125 pixels.

Parameters
minimumWidthminimum width of the submenus

◆ setSize() [1/3]

virtual void tgui::Widget::setSize ( const Layout2d size)
virtual

Changes the size of the widget.

Parameters
sizeSize of the widget

Usage examples:

// Give the widget an exact size
widget->setSize({40, 30});
// Give the widget a size relative to the size of its parent
widget->setSize({"20%", "5%"});
// Make the widget 50 pixels higher than some other widget and automatically resize it when the other widget resizes
widget->setSize({200, tgui::bindHeight(otherWidget) + 50});
TGUI_API Layout bindHeight(std::shared_ptr< Widget > widget)
Bind to the height of the widget.

Reimplemented from tgui::Widget.

◆ setSize() [2/3]

void tgui::MenuBar::setSize ( const Layout2d size)
overridevirtual

Changes the size of the menu bar.

Parameters
sizeThe new size of the menu bar

By default, the menu bar has the same width as the window and the height is 20 pixels.

Reimplemented from tgui::Widget.

◆ setSize() [3/3]

void tgui::Widget::setSize ( Layout  width,
Layout  height 
)
inline

Changes the size of the widget.

Parameters
widthWidth of the widget
heightHeight of the widget
Warning
This setSize overload must never be used from internal TGUI code or by custom widget implementations

◆ setTextSize()

void tgui::MenuBar::setTextSize ( unsigned int  size)
overridevirtual

Changes the character size of the text.

Parameters
sizeThe new size of the text.

Reimplemented from tgui::Widget.

Member Data Documentation

◆ onMenuItemClick

SignalItemHierarchy tgui::MenuBar::onMenuItemClick = {"MenuItemClicked"}

One of the menu items was clicked. Optional parameters:

  • The text of the clicked menu item
  • List containing both the name of the menu and the menu item that was clicked

The documentation for this class was generated from the following file: