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.

175 lines
5.6KB

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