JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_PopupMenu.h
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29//==============================================================================
79class JUCE_API PopupMenu
80{
81public:
82 //==============================================================================
84 PopupMenu() = default;
85
87 PopupMenu (const PopupMenu&);
88
91
93 PopupMenu& operator= (const PopupMenu&);
94
96 PopupMenu (PopupMenu&&) noexcept;
97
99 PopupMenu& operator= (PopupMenu&&) noexcept;
100
101 //==============================================================================
102 class CustomComponent;
103 class CustomCallback;
104
105 //==============================================================================
107 void clear();
108
110 struct JUCE_API Item
111 {
117
122 Item (String text);
123
124 Item (const Item&);
125 Item& operator= (const Item&);
126 Item (Item&&);
127 Item& operator= (Item&&);
128
131
137 int itemID = 0;
138
141
144
147
150
153
155 ApplicationCommandManager* commandManager = nullptr;
156
164
169
171 bool isEnabled = true;
172
174 bool isTicked = false;
175
177 bool isSeparator = false;
178
180 bool isSectionHeader = false;
181
183 bool shouldBreakAfter = false;
184
186 Item& setTicked (bool shouldBeTicked = true) & noexcept;
188 Item& setEnabled (bool shouldBeEnabled) & noexcept;
190 Item& setAction (std::function<void()> action) & noexcept;
192 Item& setID (int newID) & noexcept;
194 Item& setColour (Colour) & noexcept;
196 Item& setCustomComponent (ReferenceCountedObjectPtr<CustomComponent> customComponent) & noexcept;
198 Item& setImage (std::unique_ptr<Drawable>) & noexcept;
199
201 Item&& setTicked (bool shouldBeTicked = true) && noexcept;
203 Item&& setEnabled (bool shouldBeEnabled) && noexcept;
205 Item&& setAction (std::function<void()> action) && noexcept;
207 Item&& setID (int newID) && noexcept;
209 Item&& setColour (Colour) && noexcept;
211 Item&& setCustomComponent (ReferenceCountedObjectPtr<CustomComponent> customComponent) && noexcept;
213 Item&& setImage (std::unique_ptr<Drawable>) && noexcept;
214 };
215
220 void addItem (Item newItem);
221
223 void addItem (String itemText,
224 std::function<void()> action);
225
227 void addItem (String itemText,
228 bool isEnabled,
229 bool isTicked,
230 std::function<void()> action);
231
244 void addItem (int itemResultID,
245 String itemText,
246 bool isEnabled = true,
247 bool isTicked = false);
248
262 void addItem (int itemResultID,
263 String itemText,
264 bool isEnabled,
265 bool isTicked,
266 const Image& iconToUse);
267
282 void addItem (int itemResultID,
283 String itemText,
284 bool isEnabled,
285 bool isTicked,
286 std::unique_ptr<Drawable> iconToUse);
287
299 void addCommandItem (ApplicationCommandManager* commandManager,
300 CommandID commandID,
301 String displayName,
302 std::unique_ptr<Drawable> iconToUse);
303
304 void addCommandItem (ApplicationCommandManager* commandManager,
305 CommandID commandID,
306 String displayName = {});
307
314 void addColouredItem (int itemResultID,
315 String itemText,
316 Colour itemTextColour,
317 bool isEnabled = true,
318 bool isTicked = false,
319 const Image& iconToUse = {});
320
327 void addColouredItem (int itemResultID,
328 String itemText,
329 Colour itemTextColour,
330 bool isEnabled,
331 bool isTicked,
332 std::unique_ptr<Drawable> iconToUse);
333
345 void addCustomItem (int itemResultID,
346 std::unique_ptr<CustomComponent> customComponent,
347 std::unique_ptr<const PopupMenu> optionalSubMenu = nullptr,
348 const String& itemTitle = {});
349
366 void addCustomItem (int itemResultID,
367 Component& customComponent,
368 int idealWidth,
369 int idealHeight,
370 bool triggerMenuItemAutomaticallyWhenClicked,
371 std::unique_ptr<const PopupMenu> optionalSubMenu = nullptr,
372 const String& itemTitle = {});
373
380 void addSubMenu (String subMenuName,
381 PopupMenu subMenu,
382 bool isEnabled = true);
383
390 void addSubMenu (String subMenuName,
391 PopupMenu subMenu,
392 bool isEnabled,
393 const Image& iconToUse,
394 bool isTicked = false,
395 int itemResultID = 0);
396
407 void addSubMenu (String subMenuName,
408 PopupMenu subMenu,
409 bool isEnabled,
411 bool isTicked = false,
412 int itemResultID = 0);
413
420 void addSeparator();
421
426 void addSectionHeader (String title);
427
435 void addColumnBreak();
436
440 int getNumItems() const noexcept;
441
443 bool containsCommandItem (int commandID) const;
444
446 bool containsAnyActiveItems() const noexcept;
447
448 //==============================================================================
460 class JUCE_API Options
461 {
462 public:
464 Options();
465
466 Options (const Options&) = default;
467 Options& operator= (const Options&) = default;
468
469 enum class PopupDirection
470 {
471 upwards,
472 downwards
473 };
474
475 //==============================================================================
490 [[nodiscard]] Options withTargetComponent (Component* targetComponent) const;
491 [[nodiscard]] Options withTargetComponent (Component& targetComponent) const;
492
507 [[nodiscard]] Options withTargetScreenArea (Rectangle<int> targetArea) const;
508
515 [[nodiscard]] Options withMousePosition() const;
516
524 [[nodiscard]] Options withDeletionCheck (Component& componentToWatchForDeletion) const;
525
527 [[nodiscard]] Options withMinimumWidth (int minWidth) const;
528
530 [[nodiscard]] Options withMinimumNumColumns (int minNumColumns) const;
531
533 [[nodiscard]] Options withMaximumNumColumns (int maxNumColumns) const;
534
536 [[nodiscard]] Options withStandardItemHeight (int standardHeight) const;
537
543 [[nodiscard]] Options withItemThatMustBeVisible (int idOfItemToBeVisible) const;
544
554 [[nodiscard]] Options withParentComponent (Component* parentComponent) const;
555
557 [[nodiscard]] Options withPreferredPopupDirection (PopupDirection direction) const;
558
567 [[nodiscard]] Options withInitiallySelectedItem (int idOfItemToBeSelected) const;
568
574 [[nodiscard]] Options forSubmenu() const;
575
576 //==============================================================================
581 Component* getParentComponent() const noexcept { return parentComponent; }
582
587 Component* getTargetComponent() const noexcept { return targetComponent; }
588
595 Component* getTopLevelTargetComponent() const noexcept { return topLevelTarget; }
596
601 bool hasWatchedComponentBeenDeleted() const noexcept { return isWatchingForDeletion && componentToWatchForDeletion == nullptr; }
602
607 Rectangle<int> getTargetScreenArea() const noexcept { return targetArea; }
608
613 int getMinimumWidth() const noexcept { return minWidth; }
614
619 int getMaximumNumColumns() const noexcept { return maxColumns; }
620
625 int getMinimumNumColumns() const noexcept { return minColumns; }
626
631 int getStandardItemHeight() const noexcept { return standardHeight; }
632
637 int getItemThatMustBeVisible() const noexcept { return visibleItemID; }
638
643 PopupDirection getPreferredPopupDirection() const noexcept { return preferredPopupDirection; }
644
649 int getInitiallySelectedItemId() const noexcept { return initiallySelectedItemId; }
650
651 private:
652 //==============================================================================
653 Rectangle<int> targetArea;
654 WeakReference<Component> targetComponent, parentComponent, componentToWatchForDeletion, topLevelTarget;
655 int visibleItemID = 0, minWidth = 0, minColumns = 1, maxColumns = 0, standardHeight = 0, initiallySelectedItemId = 0;
656 bool isWatchingForDeletion = false;
657 PopupDirection preferredPopupDirection = PopupDirection::downwards;
658 };
659
660 //==============================================================================
661 #if JUCE_MODAL_LOOPS_PERMITTED
695 int show (int itemIDThatMustBeVisible = 0,
696 int minimumWidth = 0,
697 int maximumNumColumns = 0,
698 int standardItemHeight = 0,
699 ModalComponentManager::Callback* callback = nullptr);
700
701
715 int showAt (Rectangle<int> screenAreaToAttachTo,
716 int itemIDThatMustBeVisible = 0,
717 int minimumWidth = 0,
718 int maximumNumColumns = 0,
719 int standardItemHeight = 0,
720 ModalComponentManager::Callback* callback = nullptr);
721
728 int showAt (Component* componentToAttachTo,
729 int itemIDThatMustBeVisible = 0,
730 int minimumWidth = 0,
731 int maximumNumColumns = 0,
732 int standardItemHeight = 0,
733 ModalComponentManager::Callback* callback = nullptr);
734
737 int showMenu (const Options& options);
738 #endif
739
741 void showMenuAsync (const Options& options);
742
744 void showMenuAsync (const Options& options,
746
748 void showMenuAsync (const Options& options,
749 std::function<void (int)> callback);
750
751 //==============================================================================
758 static bool JUCE_CALLTYPE dismissAllActiveMenus();
759
760
761 //==============================================================================
767 void setLookAndFeel (LookAndFeel* newLookAndFeel);
768
769 //==============================================================================
778 {
779 backgroundColourId = 0x1000700,
780 textColourId = 0x1000600,
782 headerTextColourId = 0x1000601,
784 highlightedBackgroundColourId = 0x1000900,
786 highlightedTextColourId = 0x1000800,
788 };
789
790 //==============================================================================
800 class JUCE_API MenuItemIterator
801 {
802 public:
803 //==============================================================================
815 MenuItemIterator (const PopupMenu& menu, bool searchRecursively = false);
816
819
823 bool next();
824
828 Item& getItem() const;
829
830 private:
831 //==============================================================================
832 bool searchRecursively;
833
834 Array<int> index;
836 PopupMenu::Item* currentItem = nullptr;
837
838 MenuItemIterator& operator= (const MenuItemIterator&);
840 };
841
842 //==============================================================================
846 class JUCE_API CustomComponent : public Component,
848 {
849 public:
852
865 explicit CustomComponent (bool isTriggeredAutomatically);
866
872 virtual void getIdealSize (int& idealWidth, int& idealHeight) = 0;
873
879 void triggerMenuItem();
880
885 bool isItemHighlighted() const noexcept { return isHighlighted; }
886
892 const PopupMenu::Item* getItem() const noexcept { return item; }
893
895 bool isTriggeredAutomatically() const noexcept { return triggeredAutomatically; }
897 void setHighlighted (bool shouldBeHighlighted);
898
899 private:
900 //==============================================================================
901 bool isHighlighted = false, triggeredAutomatically;
902 const PopupMenu::Item* item = nullptr;
903
904 friend PopupMenu;
905
907 };
908
909 //==============================================================================
914 {
915 public:
917 ~CustomCallback() override;
918
923 virtual bool menuItemTriggered() = 0;
924
925 private:
927 };
928
929 //==============================================================================
933 struct JUCE_API LookAndFeelMethods
934 {
935 virtual ~LookAndFeelMethods() = default;
936
938 virtual void drawPopupMenuBackground (Graphics&, int width, int height);
939
942 int width,
943 int height,
944 const Options&) = 0;
945
947 virtual void drawPopupMenuItem (Graphics&, const Rectangle<int>& area,
948 bool isSeparator, bool isActive, bool isHighlighted,
949 bool isTicked, bool hasSubMenu,
950 const String& text,
951 const String& shortcutKeyText,
952 const Drawable* icon,
953 const Colour* textColour);
954
957 bool isHighlighted,
958 const Item& item,
959 const Options&) = 0;
960
961 virtual void drawPopupMenuSectionHeader (Graphics&, const Rectangle<int>&,
962 const String&);
963
964 virtual void drawPopupMenuSectionHeaderWithOptions (Graphics&, const Rectangle<int>& area,
965 const String& sectionName,
966 const Options&) = 0;
967
969 virtual Font getPopupMenuFont() = 0;
970
971 virtual void drawPopupMenuUpDownArrow (Graphics&,
972 int width, int height,
973 bool isScrollUpArrow);
974
975 virtual void drawPopupMenuUpDownArrowWithOptions (Graphics&,
976 int width, int height,
977 bool isScrollUpArrow,
978 const Options&) = 0;
979
981 virtual void getIdealPopupMenuItemSize (const String& text,
982 bool isSeparator,
983 int standardMenuItemHeight,
984 int& idealWidth,
985 int& idealHeight);
986
989 bool isSeparator,
990 int standardMenuItemHeight,
991 int& idealWidth,
992 int& idealHeight,
993 const Options&) = 0;
994
995 virtual int getMenuWindowFlags() = 0;
996
997 virtual void drawMenuBarBackground (Graphics&, int width, int height,
998 bool isMouseOverBar,
999 MenuBarComponent&) = 0;
1000
1001 virtual int getDefaultMenuBarHeight() = 0;
1002
1003 virtual int getMenuBarItemWidth (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
1004
1005 virtual Font getMenuBarFont (MenuBarComponent&, int itemIndex, const String& itemText) = 0;
1006
1007 virtual void drawMenuBarItem (Graphics&, int width, int height,
1008 int itemIndex,
1009 const String& itemText,
1010 bool isMouseOverItem,
1011 bool isMenuOpen,
1012 bool isMouseOverBar,
1013 MenuBarComponent&) = 0;
1014
1015 virtual Component* getParentComponentForMenuOptions (const Options& options) = 0;
1016
1017 virtual void preparePopupMenuWindow (Component& newWindow) = 0;
1018
1022 virtual bool shouldPopupMenuScaleWithTargetComponent (const Options& options) = 0;
1023
1024 virtual int getPopupMenuBorderSize();
1025
1026 virtual int getPopupMenuBorderSizeWithOptions (const Options&) = 0;
1027
1034 const Rectangle<int>& bounds,
1035 const Options&) = 0;
1036
1039 };
1040
1041 //==============================================================================
1042 #ifndef DOXYGEN
1043 [[deprecated ("Use the new method.")]]
1044 int drawPopupMenuItem (Graphics&, int, int, bool, bool, bool, bool, bool, const String&, const String&, Image*, const Colour*) { return 0; }
1045 #endif
1046
1047private:
1048 //==============================================================================
1049 JUCE_PUBLIC_IN_DLL_BUILD (struct HelperClasses)
1050 class Window;
1051 friend struct HelperClasses;
1052 friend class MenuBarComponent;
1053
1054 Array<Item> items;
1055 WeakReference<LookAndFeel> lookAndFeel;
1056
1057 Component* createWindow (const Options&, ApplicationCommandManager**) const;
1058 int showWithOptionalCallback (const Options&, ModalComponentManager::Callback*, bool);
1059
1060 static void setItem (CustomComponent&, const Item*);
1061
1063};
1064
1065} // namespace juce
One of these objects holds a list of all the commands your app can perform, and despatches these comm...
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
The base class for all JUCE user-interface objects.
The base class for objects which can draw themselves, e.g.
Represents a particular font, including its size, style, etc.
Definition juce_Font.h:42
A graphics context, used for drawing a component or image.
Holds a fixed-size bitmap.
Definition juce_Image.h:58
LookAndFeel objects define the appearance of all the JUCE widgets, and subclasses can be used to appl...
A menu bar component.
Receives callbacks when a modal component is dismissed.
A user-defined callback that can be used for specific items in a popup menu.
virtual bool menuItemTriggered()=0
Callback to indicate this item has been triggered.
A user-defined component that can be used as an item in a popup menu.
virtual void getIdealSize(int &idealWidth, int &idealHeight)=0
Returns a rectangle with the size that this component would like to have.
bool isItemHighlighted() const noexcept
Returns true if this item should be highlighted because the mouse is over it.
const PopupMenu::Item * getItem() const noexcept
Returns a pointer to the Item that holds this custom component, if this component is currently held b...
Allows you to iterate through the items in a pop-up menu, and examine their properties.
Class used to create a set of options to pass to the show() method.
int getInitiallySelectedItemId() const noexcept
Gets the ID of the item that must be selected when the menu is initially shown.
bool hasWatchedComponentBeenDeleted() const noexcept
Returns true if the menu was watching a component, and that component has been deleted,...
PopupDirection getPreferredPopupDirection() const noexcept
Gets the preferred popup menu direction.
int getMaximumNumColumns() const noexcept
Gets the maximum number of columns.
Component * getParentComponent() const noexcept
Gets the parent component.
int getMinimumNumColumns() const noexcept
Gets the minimum number of columns.
Rectangle< int > getTargetScreenArea() const noexcept
Gets the target screen area.
int getMinimumWidth() const noexcept
Gets the minimum width.
int getStandardItemHeight() const noexcept
Gets the default height of items in the menu.
int getItemThatMustBeVisible() const noexcept
Gets the ID of the item that must be visible when the menu is initially shown.
Component * getTopLevelTargetComponent() const noexcept
Gets the target component that was set for the top-level menu.
Component * getTargetComponent() const noexcept
Gets the target component.
Creates and displays a popup-menu.
~PopupMenu()
Destructor.
ColourIds
A set of colour IDs to use to change the colour of various aspects of the menu.
PopupMenu()=default
Creates an empty popup menu.
Manages a rectangle and allows geometric operations to be performed on it.
A smart-pointer class which points to a reference-counted object.
The JUCE String class!
Definition juce_String.h:53
This class acts as a pointer which will automatically become null if the object to which it points is...
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define JUCE_CALLTYPE
This macro defines the C calling convention used as the standard for JUCE calls.
JUCE Namespace.
@ showMenu
Represents the user showing a contextual menu for a UI element.
int CommandID
A type used to hold the unique ID for an application command.
Describes a popup menu item.
ReferenceCountedObjectPtr< CustomCallback > customCallback
A custom callback for the item to use, or nullptr if there isn't one.
std::unique_ptr< Drawable > image
A drawable to use as an icon, or nullptr if there isn't one.
String text
The menu item's name.
std::unique_ptr< PopupMenu > subMenu
A sub-menu, or nullptr if there isn't one.
ReferenceCountedObjectPtr< CustomComponent > customComponent
A custom component for the item to display, or nullptr if there isn't one.
std::function< void()> action
An optional function which should be invoked when this menu item is triggered.
String shortcutKeyDescription
An optional string describing the shortcut key for this item.
Colour colour
A colour to use to draw the menu text.
Item()
Creates a null item.
This abstract base class is implemented by LookAndFeel classes to provide menu drawing functionality.
virtual int getPopupMenuColumnSeparatorWidthWithOptions(const Options &)=0
Return the amount of space that should be left between popup menu columns.
virtual void getIdealPopupMenuItemSizeWithOptions(const String &text, bool isSeparator, int standardMenuItemHeight, int &idealWidth, int &idealHeight, const Options &)=0
Finds the best size for an item in a popup menu.
virtual void drawPopupMenuItemWithOptions(Graphics &, const Rectangle< int > &area, bool isHighlighted, const Item &item, const Options &)=0
Draws one of the items in a popup menu.
virtual void drawPopupMenuBackgroundWithOptions(Graphics &, int width, int height, const Options &)=0
Fills the background of a popup menu component.
virtual void drawPopupMenuColumnSeparatorWithOptions(Graphics &g, const Rectangle< int > &bounds, const Options &)=0
Implement this to draw some custom decoration between the columns of the popup menu.
virtual bool shouldPopupMenuScaleWithTargetComponent(const Options &options)=0
Return true if you want your popup menus to scale with the target component's AffineTransform or scal...
virtual Font getPopupMenuFont()=0
Returns the size and style of font to use in popup menus.