The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. DialogWindow::DialogWindow (const String& name, const Colour& colour,
  19. const bool escapeCloses, const bool onDesktop)
  20. : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
  21. escapeKeyTriggersCloseButton (escapeCloses)
  22. {
  23. }
  24. DialogWindow::~DialogWindow()
  25. {
  26. }
  27. bool DialogWindow::keyPressed (const KeyPress& key)
  28. {
  29. if (escapeKeyTriggersCloseButton && key == KeyPress::escapeKey)
  30. {
  31. setVisible (false);
  32. return true;
  33. }
  34. return DocumentWindow::keyPressed (key);
  35. }
  36. void DialogWindow::resized()
  37. {
  38. DocumentWindow::resized();
  39. if (escapeKeyTriggersCloseButton)
  40. {
  41. if (Button* const close = getCloseButton())
  42. {
  43. const KeyPress esc (KeyPress::escapeKey, 0, 0);
  44. if (! close->isRegisteredForShortcut (esc))
  45. close->addShortcut (esc);
  46. }
  47. }
  48. }
  49. //==============================================================================
  50. class DefaultDialogWindow : public DialogWindow
  51. {
  52. public:
  53. DefaultDialogWindow (LaunchOptions& options)
  54. : DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
  55. options.escapeKeyTriggersCloseButton, true)
  56. {
  57. setUsingNativeTitleBar (options.useNativeTitleBar);
  58. if (! JUCEApplication::isStandaloneApp())
  59. setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level
  60. if (options.content.willDeleteObject())
  61. setContentOwned (options.content.release(), true);
  62. else
  63. setContentNonOwned (options.content.release(), true);
  64. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  65. setResizable (options.resizable, options.useBottomRightCornerResizer);
  66. }
  67. void closeButtonPressed()
  68. {
  69. setVisible (false);
  70. }
  71. private:
  72. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  73. };
  74. DialogWindow::LaunchOptions::LaunchOptions() noexcept
  75. : dialogBackgroundColour (Colours::lightgrey),
  76. componentToCentreAround (nullptr),
  77. escapeKeyTriggersCloseButton (true),
  78. useNativeTitleBar (true),
  79. resizable (true),
  80. useBottomRightCornerResizer (false)
  81. {
  82. }
  83. DialogWindow* DialogWindow::LaunchOptions::create()
  84. {
  85. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  86. return new DefaultDialogWindow (*this);
  87. }
  88. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  89. {
  90. DialogWindow* const d = create();
  91. d->enterModalState (true, nullptr, true);
  92. return d;
  93. }
  94. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  95. int DialogWindow::LaunchOptions::runModal()
  96. {
  97. return launchAsync()->runModalLoop();
  98. }
  99. #endif
  100. //==============================================================================
  101. void DialogWindow::showDialog (const String& dialogTitle,
  102. Component* const contentComponent,
  103. Component* const componentToCentreAround,
  104. const Colour& backgroundColour,
  105. const bool escapeKeyTriggersCloseButton,
  106. const bool resizable,
  107. const bool useBottomRightCornerResizer)
  108. {
  109. LaunchOptions o;
  110. o.dialogTitle = dialogTitle;
  111. o.content.setNonOwned (contentComponent);
  112. o.componentToCentreAround = componentToCentreAround;
  113. o.dialogBackgroundColour = backgroundColour;
  114. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  115. o.useNativeTitleBar = false;
  116. o.resizable = resizable;
  117. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  118. o.launchAsync();
  119. }
  120. #if JUCE_MODAL_LOOPS_PERMITTED
  121. int DialogWindow::showModalDialog (const String& dialogTitle,
  122. Component* const contentComponent,
  123. Component* const componentToCentreAround,
  124. const Colour& backgroundColour,
  125. const bool escapeKeyTriggersCloseButton,
  126. const bool resizable,
  127. const bool useBottomRightCornerResizer)
  128. {
  129. LaunchOptions o;
  130. o.dialogTitle = dialogTitle;
  131. o.content.setNonOwned (contentComponent);
  132. o.componentToCentreAround = componentToCentreAround;
  133. o.dialogBackgroundColour = backgroundColour;
  134. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  135. o.useNativeTitleBar = false;
  136. o.resizable = resizable;
  137. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  138. return o.runModal();
  139. }
  140. #endif