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.

175 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
  23. escapeKeyTriggersCloseButton (escapeCloses)
  24. {
  25. }
  26. DialogWindow::~DialogWindow()
  27. {
  28. }
  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. {
  65. setUsingNativeTitleBar (options.useNativeTitleBar);
  66. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  67. if (options.content.willDeleteObject())
  68. setContentOwned (options.content.release(), true);
  69. else
  70. setContentNonOwned (options.content.release(), true);
  71. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  72. setResizable (options.resizable, options.useBottomRightCornerResizer);
  73. }
  74. void closeButtonPressed() override
  75. {
  76. setVisible (false);
  77. }
  78. private:
  79. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  80. };
  81. DialogWindow::LaunchOptions::LaunchOptions() noexcept {}
  82. DialogWindow* DialogWindow::LaunchOptions::create()
  83. {
  84. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  85. return new DefaultDialogWindow (*this);
  86. }
  87. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  88. {
  89. auto* d = create();
  90. d->enterModalState (true, nullptr, true);
  91. return d;
  92. }
  93. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  94. int DialogWindow::LaunchOptions::runModal()
  95. {
  96. return launchAsync()->runModalLoop();
  97. }
  98. #endif
  99. //==============================================================================
  100. void DialogWindow::showDialog (const String& dialogTitle,
  101. Component* const contentComponent,
  102. Component* const componentToCentreAround,
  103. Colour backgroundColour,
  104. const bool escapeKeyTriggersCloseButton,
  105. const bool resizable,
  106. const bool useBottomRightCornerResizer,
  107. const bool useNativeTitleBar)
  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.resizable = resizable;
  116. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  117. o.useNativeTitleBar = useNativeTitleBar;
  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. Colour backgroundColour,
  125. const bool escapeKeyTriggersCloseButton,
  126. const bool resizable,
  127. const bool useBottomRightCornerResizer,
  128. const bool useNativeTitleBar)
  129. {
  130. LaunchOptions o;
  131. o.dialogTitle = dialogTitle;
  132. o.content.setNonOwned (contentComponent);
  133. o.componentToCentreAround = componentToCentreAround;
  134. o.dialogBackgroundColour = backgroundColour;
  135. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  136. o.resizable = resizable;
  137. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  138. o.useNativeTitleBar = useNativeTitleBar;
  139. return o.runModal();
  140. }
  141. #endif
  142. } // namespace juce