Audio plugin host https://kx.studio/carla
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.

177 lines
5.8KB

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