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