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.

182 lines
5.9KB

  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. 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. {
  111. LaunchOptions o;
  112. o.dialogTitle = dialogTitle;
  113. o.content.setNonOwned (contentComponent);
  114. o.componentToCentreAround = componentToCentreAround;
  115. o.dialogBackgroundColour = backgroundColour;
  116. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  117. o.useNativeTitleBar = false;
  118. o.resizable = resizable;
  119. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  120. o.launchAsync();
  121. }
  122. #if JUCE_MODAL_LOOPS_PERMITTED
  123. int DialogWindow::showModalDialog (const String& dialogTitle,
  124. Component* const contentComponent,
  125. Component* const componentToCentreAround,
  126. Colour backgroundColour,
  127. const bool escapeKeyTriggersCloseButton,
  128. const bool resizable,
  129. const bool useBottomRightCornerResizer)
  130. {
  131. LaunchOptions o;
  132. o.dialogTitle = dialogTitle;
  133. o.content.setNonOwned (contentComponent);
  134. o.componentToCentreAround = componentToCentreAround;
  135. o.dialogBackgroundColour = backgroundColour;
  136. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  137. o.useNativeTitleBar = false;
  138. o.resizable = resizable;
  139. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  140. return o.runModal();
  141. }
  142. #endif
  143. //==============================================================================
  144. std::unique_ptr<AccessibilityHandler> DialogWindow::createAccessibilityHandler()
  145. {
  146. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::dialogWindow);
  147. }
  148. } // namespace juce