Creates and displays a popup-menu. More...
#include <juce_PopupMenu.h>
Classes | |
class | CustomCallback |
A user-defined callback that can be used for specific items in a popup menu. More... | |
class | CustomComponent |
A user-defined component that can be used as an item in a popup menu. More... | |
struct | Item |
Describes a popup menu item. More... | |
struct | LookAndFeelMethods |
This abstract base class is implemented by LookAndFeel classes to provide menu drawing functionality. More... | |
class | MenuItemIterator |
Allows you to iterate through the items in a pop-up menu, and examine their properties. More... | |
class | Options |
Class used to create a set of options to pass to the show() method. More... | |
Public Types | |
enum | ColourIds { backgroundColourId = 0x1000700, textColourId = 0x1000600, headerTextColourId = 0x1000601, highlightedBackgroundColourId = 0x1000900, highlightedTextColourId = 0x1000800 } |
A set of colour IDs to use to change the colour of various aspects of the menu. More... | |
Public Member Functions | |
PopupMenu () | |
Creates an empty popup menu. More... | |
PopupMenu (const PopupMenu &) | |
Creates a copy of another menu. More... | |
PopupMenu (PopupMenu &&) noexcept | |
Move constructor. More... | |
~PopupMenu () | |
Destructor. More... | |
void | addColouredItem (int itemResultID, const String &itemText, Colour itemTextColour, bool isEnabled=true, bool isTicked=false, const Image &iconToUse=Image()) |
Appends a text item with a special colour. More... | |
void | addColouredItem (int itemResultID, const String &itemText, Colour itemTextColour, bool isEnabled, bool isTicked, Drawable *iconToUse) |
Appends a text item with a special colour. More... | |
void | addCommandItem (ApplicationCommandManager *commandManager, CommandID commandID, const String &displayName=String(), Drawable *iconToUse=nullptr) |
Adds an item that represents one of the commands in a command manager object. More... | |
void | addCustomItem (int itemResultID, CustomComponent *customComponent, const PopupMenu *optionalSubMenu=nullptr) |
Appends a custom menu item. More... | |
void | addCustomItem (int itemResultID, Component *customComponent, int idealWidth, int idealHeight, bool triggerMenuItemAutomaticallyWhenClicked, const PopupMenu *optionalSubMenu=nullptr) |
Appends a custom menu item that can't be used to trigger a result. More... | |
void | addItem (const Item &newItem) |
Adds an item to the menu. More... | |
void | addItem (int itemResultID, const String &itemText, bool isEnabled=true, bool isTicked=false) |
Appends a new text item for this menu to show. More... | |
void | addItem (int itemResultID, const String &itemText, bool isEnabled, bool isTicked, const Image &iconToUse) |
Appends a new item with an icon. More... | |
void | addItem (int itemResultID, const String &itemText, bool isEnabled, bool isTicked, Drawable *iconToUse) |
Appends a new item with an icon. More... | |
void | addSectionHeader (const String &title) |
Adds a non-clickable text item to the menu. More... | |
void | addSeparator () |
Appends a separator to the menu, to help break it up into sections. More... | |
void | addSubMenu (const String &subMenuName, const PopupMenu &subMenu, bool isEnabled=true) |
Appends a sub-menu. More... | |
void | addSubMenu (const String &subMenuName, const PopupMenu &subMenu, bool isEnabled, const Image &iconToUse, bool isTicked=false, int itemResultID=0) |
Appends a sub-menu with an icon. More... | |
void | addSubMenu (const String &subMenuName, const PopupMenu &subMenu, bool isEnabled, Drawable *iconToUse, bool isTicked=false, int itemResultID=0) |
Appends a sub-menu with an icon. More... | |
void | clear () |
Resets the menu, removing all its items. More... | |
bool | containsAnyActiveItems () const noexcept |
Returns true if the menu contains any items that can be used. More... | |
bool | containsCommandItem (int commandID) const |
Returns true if the menu contains a command item that triggers the given command. More... | |
int | getNumItems () const noexcept |
Returns the number of items that the menu currently contains. More... | |
PopupMenu & | operator= (const PopupMenu &) |
Copies this menu from another one. More... | |
PopupMenu & | operator= (PopupMenu &&) noexcept |
Move assignment operator. More... | |
void | setLookAndFeel (LookAndFeel *newLookAndFeel) |
Specifies a look-and-feel for the menu and any sub-menus that it has. More... | |
int | show (int itemIDThatMustBeVisible=0, int minimumWidth=0, int maximumNumColumns=0, int standardItemHeight=0, ModalComponentManager::Callback *callback=nullptr) |
Displays the menu and waits for the user to pick something. More... | |
int | showAt (Rectangle< int > screenAreaToAttachTo, int itemIDThatMustBeVisible=0, int minimumWidth=0, int maximumNumColumns=0, int standardItemHeight=0, ModalComponentManager::Callback *callback=nullptr) |
Displays the menu at a specific location. More... | |
int | showAt (Component *componentToAttachTo, int itemIDThatMustBeVisible=0, int minimumWidth=0, int maximumNumColumns=0, int standardItemHeight=0, ModalComponentManager::Callback *callback=nullptr) |
Displays the menu as if it's attached to a component such as a button. More... | |
int | showMenu (const Options &options) |
Displays and runs the menu modally, with a set of options. More... | |
void | showMenuAsync (const Options &options, ModalComponentManager::Callback *callback) |
Runs the menu asynchronously, with a user-provided callback that will receive the result. More... | |
void | showMenuAsync (const Options &options, std::function< void(int)> callback) |
Runs the menu asynchronously, with a user-provided callback that will receive the result. More... | |
Static Public Member Functions | |
static bool | dismissAllActiveMenus () |
Closes any menus that are currently open. More... | |
Private Member Functions | |
Component * | createWindow (const Options &, ApplicationCommandManager **) const |
int | showWithOptionalCallback (const Options &, ModalComponentManager::Callback *, bool) |
Private Attributes | |
OwnedArray< Item > | items |
WeakReference< LookAndFeel > | lookAndFeel |
Friends | |
struct | HelperClasses |
class | MenuBarComponent |
Creates and displays a popup-menu.
To show a popup-menu, you create one of these, add some items to it, then call its show() method, which returns the id of the item the user selects.
E.g.
Submenus are easy too:
{GUI}
A set of colour IDs to use to change the colour of various aspects of the menu.
These constants can be used either via the LookAndFeel::setColour() method for the look and feel that is set for this menu with setLookAndFeel()
Enumerator | |
---|---|
backgroundColourId | The colour to fill the menu's background with. |
textColourId | The colour for normal menu item text, (unless the colour is specified when the item is added). |
headerTextColourId | The colour for section header item text (see the addSectionHeader() method). |
highlightedBackgroundColourId | The colour to fill the background of the currently highlighted menu item. |
highlightedTextColourId | The colour to use for the text of the currently highlighted item. |
juce::PopupMenu::PopupMenu | ( | ) |
Creates an empty popup menu.
juce::PopupMenu::PopupMenu | ( | const PopupMenu & | ) |
Creates a copy of another menu.
juce::PopupMenu::~PopupMenu | ( | ) |
Destructor.
|
noexcept |
Move constructor.
void juce::PopupMenu::addCommandItem | ( | ApplicationCommandManager * | commandManager, |
CommandID | commandID, | ||
const String & | displayName = String() , |
||
Drawable * | iconToUse = nullptr |
||
) |
Adds an item that represents one of the commands in a command manager object.
commandManager | the manager to use to trigger the command and get information about it |
commandID | the ID of the command |
displayName | if this is non-empty, then this string will be used instead of the command's registered name |
iconToUse | an optional Drawable object to use as the icon to the left of the item. The menu will take ownership of this drawable object and will delete it later when no longer needed |
void juce::PopupMenu::addCustomItem | ( | int | itemResultID, |
CustomComponent * | customComponent, | ||
const PopupMenu * | optionalSubMenu = nullptr |
||
) |
Appends a custom menu item.
This will add a user-defined component to use as a menu item. The component passed in will be deleted by this menu when it's no longer needed.
Note that native macOS menus do not support custom components.
void juce::PopupMenu::addCustomItem | ( | int | itemResultID, |
Component * | customComponent, | ||
int | idealWidth, | ||
int | idealHeight, | ||
bool | triggerMenuItemAutomaticallyWhenClicked, | ||
const PopupMenu * | optionalSubMenu = nullptr |
||
) |
Appends a custom menu item that can't be used to trigger a result.
This will add a user-defined component to use as a menu item. It's the caller's responsibility to delete the component that is passed-in when it's no longer needed after the menu has been hidden.
If triggerMenuItemAutomaticallyWhenClicked is true, the menu itself will handle detection of a mouse-click on your component, and use that to trigger the menu ID specified in itemResultID. If this is false, the menu item can't be triggered, so itemResultID is not used.
Note that native macOS menus do support custom components.
void juce::PopupMenu::addItem | ( | const Item & | newItem | ) |
Adds an item to the menu.
You can call this method for full control over the item that is added, or use the other addItem helper methods if you want to pass arguments rather than creating an Item object.
Referenced by juce::StandaloneFilterWindow::buttonClicked().
void juce::PopupMenu::addItem | ( | int | itemResultID, |
const String & | itemText, | ||
bool | isEnabled = true , |
||
bool | isTicked = false |
||
) |
Appends a new text item for this menu to show.
itemResultID | the number that will be returned from the show() method if the user picks this item. The value should never be zero, because that's used to indicate that the user didn't select anything. |
itemText | the text to show. |
isEnabled | if false, the item will be shown 'greyed-out' and can't be picked |
isTicked | if true, the item will be shown with a tick next to it |
void juce::PopupMenu::addItem | ( | int | itemResultID, |
const String & | itemText, | ||
bool | isEnabled, | ||
bool | isTicked, | ||
const Image & | iconToUse | ||
) |
Appends a new item with an icon.
itemResultID | the number that will be returned from the show() method if the user picks this item. The value should never be zero, because that's used to indicate that the user didn't select anything. |
itemText | the text to show. |
isEnabled | if false, the item will be shown 'greyed-out' and can't be picked |
isTicked | if true, the item will be shown with a tick next to it |
iconToUse | if this is a valid image, it will be displayed to the left of the item. |
void juce::PopupMenu::addItem | ( | int | itemResultID, |
const String & | itemText, | ||
bool | isEnabled, | ||
bool | isTicked, | ||
Drawable * | iconToUse | ||
) |
Appends a new item with an icon.
itemResultID | the number that will be returned from the show() method if the user picks this item. The value should never be zero, because that's used to indicate that the user didn't select anything. |
itemText | the text to show. |
isEnabled | if false, the item will be shown 'greyed-out' and can't be picked |
isTicked | if true, the item will be shown with a tick next to it |
iconToUse | a Drawable object to use as the icon to the left of the item. The menu will take ownership of this drawable object and will delete it later when no longer needed |
void juce::PopupMenu::addSectionHeader | ( | const String & | title | ) |
Adds a non-clickable text item to the menu.
This is a bold-font items which can be used as a header to separate the items into named groups.
void juce::PopupMenu::addSeparator | ( | ) |
Appends a separator to the menu, to help break it up into sections.
The menu class is smart enough not to display separators at the top or bottom of the menu, and it will replace mutliple adjacent separators with a single one, so your code can be quite free and easy about adding these, and it'll always look ok.
Referenced by juce::StandaloneFilterWindow::buttonClicked().
void juce::PopupMenu::addSubMenu | ( | const String & | subMenuName, |
const PopupMenu & | subMenu, | ||
bool | isEnabled = true |
||
) |
Appends a sub-menu.
If the menu that's passed in is empty, it will appear as an inactive item. If the itemResultID argument is non-zero, then the sub-menu item itself can be clicked to trigger it as a command.
void juce::PopupMenu::addSubMenu | ( | const String & | subMenuName, |
const PopupMenu & | subMenu, | ||
bool | isEnabled, | ||
const Image & | iconToUse, | ||
bool | isTicked = false , |
||
int | itemResultID = 0 |
||
) |
Appends a sub-menu with an icon.
If the menu that's passed in is empty, it will appear as an inactive item. If the itemResultID argument is non-zero, then the sub-menu item itself can be clicked to trigger it as a command.
void juce::PopupMenu::addSubMenu | ( | const String & | subMenuName, |
const PopupMenu & | subMenu, | ||
bool | isEnabled, | ||
Drawable * | iconToUse, | ||
bool | isTicked = false , |
||
int | itemResultID = 0 |
||
) |
Appends a sub-menu with an icon.
If the menu that's passed in is empty, it will appear as an inactive item. If the itemResultID argument is non-zero, then the sub-menu item itself can be clicked to trigger it as a command.
The iconToUse parameter is a Drawable object to use as the icon to the left of the item. The menu will take ownership of this drawable object and will delete it later when no longer needed
void juce::PopupMenu::clear | ( | ) |
Resets the menu, removing all its items.
|
noexcept |
Returns true if the menu contains any items that can be used.
Returns true if the menu contains a command item that triggers the given command.
|
private |
|
static |
Closes any menus that are currently open.
This might be useful if you have a situation where your window is being closed by some means other than a user action, and you'd like to make sure that menus aren't left hanging around.
|
noexcept |
Returns the number of items that the menu currently contains.
(This doesn't count separators).
void juce::PopupMenu::setLookAndFeel | ( | LookAndFeel * | newLookAndFeel | ) |
Specifies a look-and-feel for the menu and any sub-menus that it has.
This can be called before show() if you need a customised menu. Be careful not to delete the LookAndFeel object before the menu has been deleted.
int juce::PopupMenu::show | ( | int | itemIDThatMustBeVisible = 0 , |
int | minimumWidth = 0 , |
||
int | maximumNumColumns = 0 , |
||
int | standardItemHeight = 0 , |
||
ModalComponentManager::Callback * | callback = nullptr |
||
) |
Displays the menu and waits for the user to pick something.
This will display the menu modally, and return the ID of the item that the user picks. If they click somewhere off the menu to get rid of it without choosing anything, this will return 0.
The current location of the mouse will be used as the position to show the menu - to explicitly set the menu's position, use showAt() instead. Depending on where this point is on the screen, the menu will appear above, below or to the side of the point.
itemIDThatMustBeVisible | if you set this to the ID of one of the menu items, then when the menu first appears, it will make sure that this item is visible. So if the menu has too many items to fit on the screen, it will be scrolled to a position where this item is visible. |
minimumWidth | a minimum width for the menu, in pixels. It may be wider than this if some items are too long to fit. |
maximumNumColumns | if there are too many items to fit on-screen in a single vertical column, the menu may be laid out as a series of columns - this is the maximum number allowed. To use the default value for this (probably about 7), you can pass in zero. |
standardItemHeight | if this is non-zero, it will be used as the standard height for menu items (apart from custom items) |
callback | if this is not a nullptr, the menu will be launched asynchronously, returning immediately, and the callback will receive a call when the menu is either dismissed or has an item selected. This object will be owned and deleted by the system, so make sure that it works safely and that any pointers that it uses are safely within scope. |
int juce::PopupMenu::showAt | ( | Rectangle< int > | screenAreaToAttachTo, |
int | itemIDThatMustBeVisible = 0 , |
||
int | minimumWidth = 0 , |
||
int | maximumNumColumns = 0 , |
||
int | standardItemHeight = 0 , |
||
ModalComponentManager::Callback * | callback = nullptr |
||
) |
Displays the menu at a specific location.
This is the same as show(), but uses a specific location (in global screen coordinates) rather than the current mouse position.
The screenAreaToAttachTo parameter indicates a screen area to which the menu will be adjacent. Depending on where this is, the menu will decide which edge to attach itself to, in order to fit itself fully on-screen. If you just want to trigger a menu at a specific point, you can pass in a rectangle of size (0, 0) with the position that you want.
Displays and runs the menu modally, with a set of options.
void juce::PopupMenu::showMenuAsync | ( | const Options & | options, |
ModalComponentManager::Callback * | callback | ||
) |
Runs the menu asynchronously, with a user-provided callback that will receive the result.
Referenced by juce::StandaloneFilterWindow::buttonClicked().
void juce::PopupMenu::showMenuAsync | ( | const Options & | options, |
std::function< void(int)> | callback | ||
) |
Runs the menu asynchronously, with a user-provided callback that will receive the result.
|
private |
|
friend |
|
friend |
|
private |
|
private |