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_DocumentWindow.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
30{
31public:
32 ButtonListenerProxy (DocumentWindow& w) : owner (w) {}
33
34 void buttonClicked (Button* button) override
35 {
36 if (button == owner.getMinimiseButton()) owner.minimiseButtonPressed();
37 else if (button == owner.getMaximiseButton()) owner.maximiseButtonPressed();
38 else if (button == owner.getCloseButton()) owner.closeButtonPressed();
39 }
40
41private:
42 DocumentWindow& owner;
43
45};
46
47//==============================================================================
49 Colour backgroundColour,
51 bool addToDesktop_)
52 : ResizableWindow (title, backgroundColour, addToDesktop_),
53 requiredButtons (requiredButtons_),
55 positionTitleBarButtonsOnLeft (true)
56 #else
57 positionTitleBarButtonsOnLeft (false)
58 #endif
59{
60 setResizeLimits (128, 128, 32768, 32768);
61
63}
64
66{
67 // Don't delete or remove the resizer components yourself! They're managed by the
68 // DocumentWindow, and you should leave them alone! You may have deleted them
69 // accidentally by careless use of deleteAllChildren()..?
70 jassert (menuBar == nullptr || getIndexOfChildComponent (menuBar.get()) >= 0);
71 jassert (titleBarButtons[0] == nullptr || getIndexOfChildComponent (titleBarButtons[0].get()) >= 0);
72 jassert (titleBarButtons[1] == nullptr || getIndexOfChildComponent (titleBarButtons[1].get()) >= 0);
73 jassert (titleBarButtons[2] == nullptr || getIndexOfChildComponent (titleBarButtons[2].get()) >= 0);
74
75 for (auto& b : titleBarButtons)
76 b.reset();
77
78 menuBar.reset();
79}
80
81//==============================================================================
82void DocumentWindow::repaintTitleBar()
83{
85}
86
87void DocumentWindow::setName (const String& newName)
88{
89 if (newName != getName())
90 {
91 Component::setName (newName);
92 repaintTitleBar();
93 }
94}
95
97{
98 titleBarIcon = imageToUse;
99 repaintTitleBar();
100}
101
103{
104 titleBarHeight = newHeight;
105 resized();
106 repaintTitleBar();
107}
108
109void DocumentWindow::setTitleBarButtonsRequired (const int buttons, const bool onLeft)
110{
111 requiredButtons = buttons;
112 positionTitleBarButtonsOnLeft = onLeft;
114}
115
117{
118 drawTitleTextCentred = textShouldBeCentred;
119 repaintTitleBar();
120}
121
122//==============================================================================
124{
125 if (menuBarModel != newMenuBarModel)
126 {
127 menuBar.reset();
128
129 menuBarModel = newMenuBarModel;
130 menuBarHeight = newMenuBarHeight > 0 ? newMenuBarHeight
131 : getLookAndFeel().getDefaultMenuBarHeight();
132
133 if (menuBarModel != nullptr)
134 setMenuBarComponent (new MenuBarComponent (menuBarModel));
135
136 resized();
137 }
138}
139
141{
142 return menuBar.get();
143}
144
146{
147 menuBar.reset (newMenuBarComponent);
148 Component::addAndMakeVisible (menuBar.get()); // (call the superclass method directly to avoid the assertion in ResizableWindow)
149
150 if (menuBar != nullptr)
151 menuBar->setEnabled (isActiveWindow());
152
153 resized();
154}
155
156//==============================================================================
158{
159 /* If you've got a close button, you have to override this method to get
160 rid of your window!
161
162 If the window is just a pop-up, you should override this method and make
163 it delete the window in whatever way is appropriate for your app. E.g. you
164 might just want to call "delete this".
165
166 If your app is centred around this window such that the whole app should quit when
167 the window is closed, then you will probably want to use this method as an opportunity
168 to call JUCEApplicationBase::quit(), and leave the window to be deleted later by your
169 JUCEApplicationBase::shutdown() method. (Doing it this way means that your window will
170 still get cleaned-up if the app is quit by some other means (e.g. a cmd-Q on the mac
171 or closing it via the taskbar icon on Windows).
172 */
174}
175
180
185
186//==============================================================================
188{
190
193 g.setOrigin (titleBarArea.getPosition());
194
195 int titleSpaceX1 = 6;
196 int titleSpaceX2 = titleBarArea.getWidth() - 6;
197
198 for (auto& b : titleBarButtons)
199 {
200 if (b != nullptr)
201 {
202 if (positionTitleBarButtonsOnLeft)
203 titleSpaceX1 = jmax (titleSpaceX1, b->getRight() + (getWidth() - b->getRight()) / 8);
204 else
205 titleSpaceX2 = jmin (titleSpaceX2, b->getX() - (b->getX() / 8));
206 }
207 }
208
209 getLookAndFeel().drawDocumentWindowTitleBar (*this, g,
210 titleBarArea.getWidth(),
211 titleBarArea.getHeight(),
214 titleBarIcon.isValid() ? &titleBarIcon : nullptr,
215 ! drawTitleTextCentred);
216}
217
219{
221
222 if (auto* b = getMaximiseButton())
223 b->setToggleState (isFullScreen(), dontSendNotification);
224
226
228 .positionDocumentWindowButtons (*this,
229 titleBarArea.getX(), titleBarArea.getY(),
230 titleBarArea.getWidth(), titleBarArea.getHeight(),
231 titleBarButtons[0].get(),
232 titleBarButtons[1].get(),
233 titleBarButtons[2].get(),
234 positionTitleBarButtonsOnLeft);
235
236 if (menuBar != nullptr)
237 menuBar->setBounds (titleBarArea.getX(), titleBarArea.getBottom(),
238 titleBarArea.getWidth(), menuBarHeight);
239}
240
242{
244}
245
247{
248 auto border = getBorderThickness();
249
250 if (! isKioskMode())
251 border.setTop (border.getTop()
252 + (isUsingNativeTitleBar() ? 0 : titleBarHeight)
253 + (menuBar != nullptr ? menuBarHeight : 0));
254
255 return border;
256}
257
259{
260 return isUsingNativeTitleBar() ? 0 : jmin (titleBarHeight, getHeight() - 4);
261}
262
263Rectangle<int> DocumentWindow::getTitleBarArea()
264{
265 if (isKioskMode())
266 return {};
267
268 auto border = getBorderThickness();
269 return { border.getLeft(), border.getTop(), getWidth() - border.getLeftAndRight(), getTitleBarHeight() };
270}
271
272Button* DocumentWindow::getCloseButton() const noexcept { return titleBarButtons[2].get(); }
273Button* DocumentWindow::getMinimiseButton() const noexcept { return titleBarButtons[0].get(); }
274Button* DocumentWindow::getMaximiseButton() const noexcept { return titleBarButtons[1].get(); }
275
276int DocumentWindow::getDesktopWindowStyleFlags() const
277{
278 auto styleFlags = ResizableWindow::getDesktopWindowStyleFlags();
279
280 if ((requiredButtons & minimiseButton) != 0) styleFlags |= ComponentPeer::windowHasMinimiseButton;
281 if ((requiredButtons & maximiseButton) != 0) styleFlags |= ComponentPeer::windowHasMaximiseButton;
282 if ((requiredButtons & closeButton) != 0) styleFlags |= ComponentPeer::windowHasCloseButton;
283
284 return styleFlags;
285}
286
288{
289 for (auto& b : titleBarButtons)
290 b.reset();
291
292 if (! isUsingNativeTitleBar())
293 {
294 auto& lf = getLookAndFeel();
295
296 if ((requiredButtons & minimiseButton) != 0) titleBarButtons[0].reset (lf.createDocumentWindowButton (minimiseButton));
297 if ((requiredButtons & maximiseButton) != 0) titleBarButtons[1].reset (lf.createDocumentWindowButton (maximiseButton));
298 if ((requiredButtons & closeButton) != 0) titleBarButtons[2].reset (lf.createDocumentWindowButton (closeButton));
299
300 for (auto& b : titleBarButtons)
301 {
302 if (b != nullptr)
303 {
304 if (buttonListener == nullptr)
305 buttonListener.reset (new ButtonListenerProxy (*this));
306
307 b->addListener (buttonListener.get());
308 b->setWantsKeyboardFocus (false);
309
310 // (call the Component method directly to avoid the assertion in ResizableWindow)
312 }
313 }
314
315 if (auto* b = getCloseButton())
316 {
317 #if JUCE_MAC
318 b->addShortcut (KeyPress ('w', ModifierKeys::commandModifier, 0));
319 #else
320 b->addShortcut (KeyPress (KeyPress::F4Key, ModifierKeys::altModifier, 0));
321 #endif
322 }
323 }
324
326
328}
329
331{
333}
334
336{
338 bool isActive = isActiveWindow();
339
340 for (auto& b : titleBarButtons)
341 if (b != nullptr)
342 b->setEnabled (isActive);
343
344 if (menuBar != nullptr)
345 menuBar->setEnabled (isActive);
346}
347
348void DocumentWindow::mouseDoubleClick (const MouseEvent& e)
349{
350 if (getTitleBarArea().contains (e.x, e.y))
351 if (auto* maximise = getMaximiseButton())
352 maximise->triggerClick();
353}
354
356{
358}
359
360} // namespace juce
Used to receive callbacks when a button is clicked.
A base class for buttons.
Definition juce_Button.h:43
Represents a colour, also including a transparency value.
Definition juce_Colour.h:38
@ windowHasMaximiseButton
Indicates that if the window has a title bar, it should have a maximise button on it.
@ windowHasCloseButton
Indicates that if the window has a title bar, it should have a close button on it.
@ windowHasMinimiseButton
Indicates that if the window has a title bar, it should have a minimise button on it.
The base class for all JUCE user-interface objects.
bool contains(Point< int > localPoint)
Returns true if a given point lies within this component or one of its children.
virtual void parentHierarchyChanged()
Called to indicate that the component's parents have changed.
int getIndexOfChildComponent(const Component *child) const noexcept
Returns the index of this component in the list of child components.
int getHeight() const noexcept
Returns the component's height in pixels.
virtual void userTriedToCloseWindow()
For components on the desktop, this is called if the system wants to close the window.
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 setEnabled(bool shouldBeEnabled)
Enables or disables this component.
void repaint()
Marks the whole component as needing to be redrawn.
virtual void resized()
Called when this component's size has been changed.
virtual void setName(const String &newName)
Sets the name of this component.
void mouseDoubleClick(const MouseEvent &event) override
Called when a mouse button has been double-clicked on a component.
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.
virtual void lookAndFeelChanged()
Called to let the component react to a change in the look-and-feel setting.
virtual void paint(Graphics &g)
Components can override this method to draw their content.
String getName() const noexcept
Returns the name of this component.
void buttonClicked(Button *button) override
Called when the button is clicked.
A resizable window with a title bar and maximise, minimise and close buttons.
virtual void maximiseButtonPressed()
Callback that is triggered when the maximise button is pressed, or when the title-bar is double-click...
Button * getCloseButton() const noexcept
Returns the close button, (or nullptr if there isn't one).
Component * getMenuBarComponent() const noexcept
Returns the current menu bar component, or null if there isn't one.
DocumentWindow(const String &name, Colour backgroundColour, int requiredButtons, bool addToDesktop=true)
Creates a DocumentWindow.
Button * getMaximiseButton() const noexcept
Returns the maximise button, (or nullptr if there isn't one).
void setMenuBarComponent(Component *newMenuBarComponent)
Replaces the current menu bar with a custom component.
~DocumentWindow() override
Destructor.
virtual void minimiseButtonPressed()
Callback that is triggered when the minimise button is pressed.
void setTitleBarTextCentred(bool textShouldBeCentred)
Sets whether the title should be centred within the window.
void setIcon(const Image &imageToUse)
Sets an icon to show in the title bar, next to the title.
void setTitleBarHeight(int newHeight)
Changes the height of the title-bar.
Button * getMinimiseButton() const noexcept
Returns the minimise button, (or nullptr if there isn't one).
virtual void closeButtonPressed()
This method is called when the user tries to close the window.
void setMenuBar(MenuBarModel *menuBarModel, int menuBarHeight=0)
Creates a menu inside this window.
void setName(const String &newName) override
Changes the component's name.
void setTitleBarButtonsRequired(int requiredButtons, bool positionTitleBarButtonsOnLeft)
Changes the set of title-bar buttons being shown.
int getTitleBarHeight() const
Returns the current title bar height.
A graphics context, used for drawing a component or image.
bool reduceClipRegion(int x, int y, int width, int height)
Intersects the current clipping region with another region.
void setOrigin(Point< int > newOrigin)
Moves the position of the context's origin.
Holds a fixed-size bitmap.
Definition juce_Image.h:58
bool isValid() const noexcept
Returns true if this image isn't null.
Definition juce_Image.h:147
static const int F4Key
key-code for the F4 key
A menu bar component.
A class for controlling MenuBar components.
@ commandModifier
Command key flag - on windows this is the same as the CTRL key flag.
@ altModifier
ALT key flag.
Manages a rectangle and allows geometric operations to be performed on it.
A base class for top-level windows that can be dragged around and resized.
void setFullScreen(bool shouldBeFullScreen)
Puts the window into full-screen mode, or restores it to its normal size.
void resized() override
(if overriding this, make sure you call ResizableWindow::resized() in your subclass)
void setResizeLimits(int newMinimumWidth, int newMinimumHeight, int newMaximumWidth, int newMaximumHeight) noexcept
This sets the maximum and minimum sizes for the window.
bool isKioskMode() const
Returns true if the window has been placed in kiosk-mode.
void lookAndFeelChanged() override
Called to let the component react to a change in the look-and-feel setting.
virtual BorderSize< int > getContentComponentBorder()
Returns the insets to use when positioning the content component.
void paint(Graphics &) override
Components can override this method to draw their content.
virtual BorderSize< int > getBorderThickness()
Returns the width of the frame to use around the window.
bool isFullScreen() const
Returns true if the window is currently in full-screen mode.
void activeWindowStatusChanged() override
This callback happens when this window becomes active or inactive.
void setMinimised(bool shouldMinimise)
Minimises the window, or restores it to its previous position and size.
The JUCE String class!
Definition juce_String.h:53
bool isUsingNativeTitleBar() const noexcept
Returns true if the window is currently using an OS-native title bar.
bool isActiveWindow() const noexcept
True if this is currently the TopLevelWindow that is actively being used.
#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 ...
#define jassertfalse
This will always cause an assertion failure.
JUCE Namespace.
constexpr Type jmin(Type a, Type b)
Returns the smaller of two values.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
@ dontSendNotification
No notification message should be sent.
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