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.

184 lines
6.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. DialogWindow::DialogWindow (const String& name, Colour colour,
  21. const bool escapeCloses, const bool onDesktop,
  22. const float scale)
  23. : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
  24. desktopScale (scale),
  25. escapeKeyTriggersCloseButton (escapeCloses)
  26. {
  27. }
  28. DialogWindow::~DialogWindow() = default;
  29. bool DialogWindow::escapeKeyPressed()
  30. {
  31. if (escapeKeyTriggersCloseButton)
  32. {
  33. setVisible (false);
  34. return true;
  35. }
  36. return false;
  37. }
  38. bool DialogWindow::keyPressed (const KeyPress& key)
  39. {
  40. if (key == KeyPress::escapeKey && escapeKeyPressed())
  41. return true;
  42. return DocumentWindow::keyPressed (key);
  43. }
  44. void DialogWindow::resized()
  45. {
  46. DocumentWindow::resized();
  47. if (escapeKeyTriggersCloseButton)
  48. {
  49. if (auto* close = getCloseButton())
  50. {
  51. const KeyPress esc (KeyPress::escapeKey, 0, 0);
  52. if (! close->isRegisteredForShortcut (esc))
  53. close->addShortcut (esc);
  54. }
  55. }
  56. }
  57. //==============================================================================
  58. class DefaultDialogWindow : public DialogWindow
  59. {
  60. public:
  61. DefaultDialogWindow (LaunchOptions& options)
  62. : DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
  63. options.escapeKeyTriggersCloseButton, true,
  64. options.componentToCentreAround != nullptr
  65. ? Component::getApproximateScaleFactorForComponent (options.componentToCentreAround)
  66. : 1.0f)
  67. {
  68. setUsingNativeTitleBar (options.useNativeTitleBar);
  69. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  70. if (options.content.willDeleteObject())
  71. setContentOwned (options.content.release(), true);
  72. else
  73. setContentNonOwned (options.content.release(), true);
  74. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  75. setResizable (options.resizable, options.useBottomRightCornerResizer);
  76. }
  77. void closeButtonPressed() override
  78. {
  79. setVisible (false);
  80. }
  81. private:
  82. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  83. };
  84. DialogWindow::LaunchOptions::LaunchOptions() noexcept {}
  85. DialogWindow* DialogWindow::LaunchOptions::create()
  86. {
  87. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  88. return new DefaultDialogWindow (*this);
  89. }
  90. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  91. {
  92. auto* d = create();
  93. d->enterModalState (true, nullptr, true);
  94. return d;
  95. }
  96. #if JUCE_MODAL_LOOPS_PERMITTED
  97. int DialogWindow::LaunchOptions::runModal()
  98. {
  99. return launchAsync()->runModalLoop();
  100. }
  101. #endif
  102. //==============================================================================
  103. void DialogWindow::showDialog (const String& dialogTitle,
  104. Component* const contentComponent,
  105. Component* const componentToCentreAround,
  106. Colour backgroundColour,
  107. const bool escapeKeyTriggersCloseButton,
  108. const bool resizable,
  109. const bool useBottomRightCornerResizer,
  110. const bool useNativeTitleBar)
  111. {
  112. LaunchOptions o;
  113. o.dialogTitle = dialogTitle;
  114. o.content.setNonOwned (contentComponent);
  115. o.componentToCentreAround = componentToCentreAround;
  116. o.dialogBackgroundColour = backgroundColour;
  117. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  118. o.resizable = resizable;
  119. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  120. o.useNativeTitleBar = useNativeTitleBar;
  121. o.launchAsync();
  122. }
  123. #if JUCE_MODAL_LOOPS_PERMITTED
  124. int DialogWindow::showModalDialog (const String& dialogTitle,
  125. Component* const contentComponent,
  126. Component* const componentToCentreAround,
  127. Colour backgroundColour,
  128. const bool escapeKeyTriggersCloseButton,
  129. const bool resizable,
  130. const bool useBottomRightCornerResizer,
  131. const bool useNativeTitleBar)
  132. {
  133. LaunchOptions o;
  134. o.dialogTitle = dialogTitle;
  135. o.content.setNonOwned (contentComponent);
  136. o.componentToCentreAround = componentToCentreAround;
  137. o.dialogBackgroundColour = backgroundColour;
  138. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  139. o.resizable = resizable;
  140. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  141. o.useNativeTitleBar = useNativeTitleBar;
  142. return o.runModal();
  143. }
  144. #endif
  145. //==============================================================================
  146. std::unique_ptr<AccessibilityHandler> DialogWindow::createAccessibilityHandler()
  147. {
  148. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::dialogWindow);
  149. }
  150. } // namespace juce