1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-13 00:04:19 +00:00
JUCE/modules/juce_gui_basics/windows/juce_DialogWindow.cpp
reuk 0f6f2728f2
AlertWindow: Scale window according to scale of associatedComponent
For DialogWindow, uses the scale of componentToCentreAround.

This allows drawing dialog windows at the expected scale in plugins.
An associated component must be supplied in order for this to work.
2021-03-23 12:09:35 +00:00

175 lines
5.7 KiB
C++

/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2020 - Raw Material Software Limited
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 6 End-User License
Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
End User License Agreement: www.juce.com/juce-6-licence
Privacy Policy: www.juce.com/juce-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
namespace juce
{
DialogWindow::DialogWindow (const String& name, Colour colour,
const bool escapeCloses, const bool onDesktop,
const float scale)
: DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
desktopScale (scale),
escapeKeyTriggersCloseButton (escapeCloses)
{
}
DialogWindow::~DialogWindow() = default;
bool DialogWindow::escapeKeyPressed()
{
if (escapeKeyTriggersCloseButton)
{
setVisible (false);
return true;
}
return false;
}
bool DialogWindow::keyPressed (const KeyPress& key)
{
if (key == KeyPress::escapeKey && escapeKeyPressed())
return true;
return DocumentWindow::keyPressed (key);
}
void DialogWindow::resized()
{
DocumentWindow::resized();
if (escapeKeyTriggersCloseButton)
{
if (auto* close = getCloseButton())
{
const KeyPress esc (KeyPress::escapeKey, 0, 0);
if (! close->isRegisteredForShortcut (esc))
close->addShortcut (esc);
}
}
}
//==============================================================================
class DefaultDialogWindow : public DialogWindow
{
public:
DefaultDialogWindow (LaunchOptions& options)
: DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
options.escapeKeyTriggersCloseButton, true,
options.componentToCentreAround != nullptr
? Component::getApproximateScaleFactorForComponent (options.componentToCentreAround)
: 1.0f)
{
setUsingNativeTitleBar (options.useNativeTitleBar);
setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
if (options.content.willDeleteObject())
setContentOwned (options.content.release(), true);
else
setContentNonOwned (options.content.release(), true);
centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
setResizable (options.resizable, options.useBottomRightCornerResizer);
}
void closeButtonPressed() override
{
setVisible (false);
}
private:
JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
};
DialogWindow::LaunchOptions::LaunchOptions() noexcept {}
DialogWindow* DialogWindow::LaunchOptions::create()
{
jassert (content != nullptr); // You need to provide some kind of content for the dialog!
return new DefaultDialogWindow (*this);
}
DialogWindow* DialogWindow::LaunchOptions::launchAsync()
{
auto* d = create();
d->enterModalState (true, nullptr, true);
return d;
}
#if JUCE_MODAL_LOOPS_PERMITTED
int DialogWindow::LaunchOptions::runModal()
{
return launchAsync()->runModalLoop();
}
#endif
//==============================================================================
void DialogWindow::showDialog (const String& dialogTitle,
Component* const contentComponent,
Component* const componentToCentreAround,
Colour backgroundColour,
const bool escapeKeyTriggersCloseButton,
const bool resizable,
const bool useBottomRightCornerResizer)
{
LaunchOptions o;
o.dialogTitle = dialogTitle;
o.content.setNonOwned (contentComponent);
o.componentToCentreAround = componentToCentreAround;
o.dialogBackgroundColour = backgroundColour;
o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
o.useNativeTitleBar = false;
o.resizable = resizable;
o.useBottomRightCornerResizer = useBottomRightCornerResizer;
o.launchAsync();
}
#if JUCE_MODAL_LOOPS_PERMITTED
int DialogWindow::showModalDialog (const String& dialogTitle,
Component* const contentComponent,
Component* const componentToCentreAround,
Colour backgroundColour,
const bool escapeKeyTriggersCloseButton,
const bool resizable,
const bool useBottomRightCornerResizer)
{
LaunchOptions o;
o.dialogTitle = dialogTitle;
o.content.setNonOwned (contentComponent);
o.componentToCentreAround = componentToCentreAround;
o.dialogBackgroundColour = backgroundColour;
o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
o.useNativeTitleBar = false;
o.resizable = resizable;
o.useBottomRightCornerResizer = useBottomRightCornerResizer;
return o.runModal();
}
#endif
} // namespace juce