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_BurgerMenuComponent.cpp
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//==============================================================================
31{
33 {
34 setInterceptsMouseClicks (false, true);
35 update (customComponent);
36 }
37
39 {
40 jassert (newComponent != nullptr);
41
42 if (newComponent != custom)
43 {
44 if (custom != nullptr)
45 removeChildComponent (custom.get());
46
47 custom = newComponent;
48 addAndMakeVisible (*custom);
49 resized();
50 }
51 }
52
53 void resized() override
54 {
55 custom->setBounds (getLocalBounds());
56 }
57
59
61};
62
63//==============================================================================
72
74{
75 if (model != nullptr)
76 model->removeListener (this);
77}
78
80{
81 if (newModel != model)
82 {
83 if (model != nullptr)
84 model->removeListener (this);
85
86 model = newModel;
87
88 if (model != nullptr)
89 model->addListener (this);
90
91 refresh();
92 listBox.updateContent();
93 }
94}
95
97{
98 return model;
99}
100
101void BurgerMenuComponent::refresh()
102{
103 lastRowClicked = inputSourceIndexOfLastClick = -1;
104
105 rows.clear();
106
107 if (model != nullptr)
108 {
109 auto menuBarNames = model->getMenuBarNames();
110
111 for (auto menuIdx = 0; menuIdx < menuBarNames.size(); ++menuIdx)
112 {
113 PopupMenu::Item menuItem;
114 menuItem.text = menuBarNames[menuIdx];
115
116 String ignore;
117 auto menu = model->getMenuForIndex (menuIdx, ignore);
118
119 rows.add (Row { true, menuIdx, menuItem });
120 addMenuBarItemsForMenu (menu, menuIdx);
121 }
122 }
123}
124
125void BurgerMenuComponent::addMenuBarItemsForMenu (PopupMenu& menu, int menuIdx)
126{
127 for (PopupMenu::MenuItemIterator it (menu); it.next();)
128 {
129 auto& item = it.getItem();
130
131 if (item.isSeparator)
132 continue;
133
134 if (hasSubMenu (item))
135 addMenuBarItemsForMenu (*item.subMenu, menuIdx);
136 else
137 rows.add (Row {false, menuIdx, it.getItem()});
138 }
139}
140
141int BurgerMenuComponent::getNumRows()
142{
143 return rows.size();
144}
145
146void BurgerMenuComponent::paint (Graphics& g)
147{
149}
150
151void BurgerMenuComponent::paintListBoxItem (int rowIndex, Graphics& g, int w, int h, bool highlight)
152{
153 auto& lf = getLookAndFeel();
154 Rectangle<int> r (w, h);
155
156 auto row = (rowIndex < rows.size() ? rows.getReference (rowIndex)
157 : Row { true, 0, {} });
158
160
161 if (row.isMenuHeader)
162 {
163 lf.drawPopupMenuSectionHeader (g, r.reduced (20, 0), row.item.text);
164 g.setColour (Colours::grey);
165 g.fillRect (r.withHeight (1));
166 }
167 else
168 {
169 auto& item = row.item;
170 auto* colour = item.colour != Colour() ? &item.colour : nullptr;
171
172 if (item.customComponent == nullptr)
173 lf.drawPopupMenuItem (g, r.reduced (20, 0),
174 item.isSeparator,
175 item.isEnabled,
176 highlight,
177 item.isTicked,
178 hasSubMenu (item),
179 item.text,
180 item.shortcutKeyDescription,
181 item.image.get(),
182 colour);
183 }
184}
185
186bool BurgerMenuComponent::hasSubMenu (const PopupMenu::Item& item)
187{
188 return item.subMenu != nullptr && (item.itemID == 0 || item.subMenu->getNumItems() > 0);
189}
190
191void BurgerMenuComponent::listBoxItemClicked (int rowIndex, const MouseEvent& e)
192{
193 auto row = rowIndex < rows.size() ? rows.getReference (rowIndex)
194 : Row { true, 0, {} };
195
196 if (! row.isMenuHeader)
197 {
198 lastRowClicked = rowIndex;
199 inputSourceIndexOfLastClick = e.source.getIndex();
200 }
201}
202
203Component* BurgerMenuComponent::refreshComponentForRow (int rowIndex, bool isRowSelected, Component* existing)
204{
205 auto row = rowIndex < rows.size() ? rows.getReference (rowIndex)
206 : Row { true, 0, {} };
207
208 auto hasCustomComponent = (row.item.customComponent != nullptr);
209
210 if (existing == nullptr && hasCustomComponent)
211 return new CustomMenuBarItemHolder (row.item.customComponent);
212
213 if (existing != nullptr)
214 {
215 auto* componentToUpdate = dynamic_cast<CustomMenuBarItemHolder*> (existing);
216 jassert (componentToUpdate != nullptr);
217
218 if (hasCustomComponent && componentToUpdate != nullptr)
219 {
220 row.item.customComponent->setHighlighted (isRowSelected);
221 componentToUpdate->update (row.item.customComponent);
222 }
223 else
224 {
225 delete existing;
226 existing = nullptr;
227 }
228 }
229
230 return existing;
231}
232
233void BurgerMenuComponent::resized()
234{
235 listBox.setBounds (getLocalBounds());
236}
237
238void BurgerMenuComponent::menuBarItemsChanged (MenuBarModel* menuBarModel)
239{
240 setModel (menuBarModel);
241}
242
243void BurgerMenuComponent::menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo&)
244{
245}
246
247void BurgerMenuComponent::mouseUp (const MouseEvent& event)
248{
249 auto rowIndex = listBox.getSelectedRow();
250
251 if (rowIndex == lastRowClicked && rowIndex < rows.size()
252 && event.source.getIndex() == inputSourceIndexOfLastClick)
253 {
254 auto& row = rows.getReference (rowIndex);
255
256 if (! row.isMenuHeader)
257 {
258 listBox.selectRow (-1);
259
260 lastRowClicked = -1;
261 inputSourceIndexOfLastClick = -1;
262
263 topLevelIndexClicked = row.topLevelMenuIndex;
264 auto& item = row.item;
265
266 if (auto* managerOfChosenCommand = item.commandManager)
267 {
268 ApplicationCommandTarget::InvocationInfo info (item.itemID);
270
271 managerOfChosenCommand->invoke (info, true);
272 }
273
274 postCommandMessage (item.itemID);
275 }
276 }
277}
278
279void BurgerMenuComponent::handleCommandMessage (int commandID)
280{
281 if (model != nullptr)
282 {
283 model->menuItemSelected (commandID, topLevelIndexClicked);
284 topLevelIndexClicked = -1;
285
286 refresh();
287 listBox.updateContent();
288 }
289}
290
292{
293 listBox.setRowHeight (roundToInt (getLookAndFeel().getPopupMenuFont().getHeight() * 2.0f));
294}
295
296//==============================================================================
298{
299 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::menuBar);
300}
301
302} // namespace juce
int size() const noexcept
Returns the current number of elements in the array.
Definition juce_Array.h:215
void add(const ElementType &newElement)
Appends a new element at the end of the array.
Definition juce_Array.h:418
void clear()
Removes all elements from the array.
Definition juce_Array.h:188
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:267
void lookAndFeelChanged() override
Called to let the component react to a change in the look-and-feel setting.
void setModel(MenuBarModel *newModel)
Changes the model object to use to control the burger menu.
BurgerMenuComponent(MenuBarModel *model=nullptr)
Creates a burger menu component.
MenuBarModel * getModel() const noexcept
Returns the current burger menu model being used.
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Override this method to return a custom AccessibilityHandler for this component.
~BurgerMenuComponent() override
Destructor.
The base class for all JUCE user-interface objects.
void setInterceptsMouseClicks(bool allowClicksOnThisComponent, bool allowClicksOnChildComponents) noexcept
Changes the default return value for the hitTest() method.
int getHeight() const noexcept
Returns the component's height in pixels.
void addAndMakeVisible(Component *child, int zOrder=-1)
Adds a child component to this one, and also makes the child visible if it isn't already.
void addMouseListener(MouseListener *newListener, bool wantsEventsForAllNestedChildComponents)
Registers a listener to be told when mouse events occur in this component.
void postCommandMessage(int commandId)
Dispatches a numbered message to this component.
void removeChildComponent(Component *childToRemove)
Removes one of this component's child-components.
void setBounds(int x, int y, int width, int height)
Changes the component's position and size.
Colour findColour(int colourID, bool inheritFromParent=false) const
Looks for a colour that has been registered with the given colour ID number.
int getWidth() const noexcept
Returns the component's width in pixels.
LookAndFeel & getLookAndFeel() const noexcept
Finds the appropriate look-and-feel to use for this component.
Rectangle< int > getLocalBounds() const noexcept
Returns the component's bounds, relative to its own origin.
void setRowHeight(int newHeight)
Sets the height of each row in the list.
int getSelectedRow(int index=0) const
Returns the row number of a selected row.
void updateContent()
Causes the list to refresh its content.
void selectRow(int rowNumber, bool dontScrollToShowThisRow=false, bool deselectOthersFirst=true)
Selects a row.
A class for controlling MenuBar components.
virtual PopupMenu getMenuForIndex(int topLevelMenuIndex, const String &menuName)=0
This should return the popup menu to display for a given top-level menu.
void removeListener(Listener *listenerToRemove)
Removes a listener.
virtual StringArray getMenuBarNames()=0
This method must return a list of the names of the menus.
void addListener(Listener *listenerToAdd)
Registers a listener for callbacks when the menu items in this model change.
virtual void menuItemSelected(int menuItemID, int topLevelMenuIndex)=0
This is called when a menu item has been clicked on.
@ backgroundColourId
The colour to fill the menu's background with.
A smart-pointer class which points to a reference-counted object.
The JUCE String class!
Definition juce_String.h:53
#define jassert(expression)
Platform-independent assertion macro.
#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 ...
JUCE Namespace.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
@ fromMenu
The command is being invoked by a menu selection.
void resized() override
Called when this component's size has been changed.
Describes a popup menu item.
virtual void drawPopupMenuBackground(Graphics &, int width, int height)
Fills the background of a popup menu component.