|  | /*
  ==============================================================================
   This file is part of the JUCE library.
   Copyright (c) 2015 - ROLI Ltd.
   Permission is granted to use this software under the terms of either:
   a) the GPL v2 (or any later version)
   b) the Affero GPL v3
   Details of these licenses can be found at: www.gnu.org/licenses
   JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
   ------------------------------------------------------------------------------
   To release a closed-source product which uses JUCE, commercial licenses are
   available: visit www.juce.com for more information.
  ==============================================================================
*/
DialogWindow::DialogWindow (const String& name, Colour colour,
                            const bool escapeCloses, const bool onDesktop)
    : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
      escapeKeyTriggersCloseButton (escapeCloses)
{
}
DialogWindow::~DialogWindow()
{
}
bool DialogWindow::keyPressed (const KeyPress& key)
{
    if (escapeKeyTriggersCloseButton && key == KeyPress::escapeKey)
    {
        setVisible (false);
        return true;
    }
    return DocumentWindow::keyPressed (key);
}
void DialogWindow::resized()
{
    DocumentWindow::resized();
    if (escapeKeyTriggersCloseButton)
    {
        if (Button* const 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)
    {
        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
    : dialogBackgroundColour (Colours::lightgrey),
      componentToCentreAround (nullptr),
      escapeKeyTriggersCloseButton (true),
      useNativeTitleBar (true),
      resizable (true),
      useBottomRightCornerResizer (false)
{
}
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()
{
    DialogWindow* const d = create();
    d->enterModalState (true, nullptr, true);
    return d;
}
#if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
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,
                               const bool useNativeTitleBar)
{
    LaunchOptions o;
    o.dialogTitle = dialogTitle;
    o.content.setNonOwned (contentComponent);
    o.componentToCentreAround = componentToCentreAround;
    o.dialogBackgroundColour = backgroundColour;
    o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
    o.resizable = resizable;
    o.useBottomRightCornerResizer = useBottomRightCornerResizer;
    o.useNativeTitleBar = useNativeTitleBar;
    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,
                                   const bool useNativeTitleBar)
{
    LaunchOptions o;
    o.dialogTitle = dialogTitle;
    o.content.setNonOwned (contentComponent);
    o.componentToCentreAround = componentToCentreAround;
    o.dialogBackgroundColour = backgroundColour;
    o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
    o.resizable = resizable;
    o.useBottomRightCornerResizer = useBottomRightCornerResizer;
    o.useNativeTitleBar = useNativeTitleBar;
    return o.runModal();
}
#endif
 |