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.

173 lines
5.4KB

  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. {
  108. LaunchOptions o;
  109. o.dialogTitle = dialogTitle;
  110. o.content.setNonOwned (contentComponent);
  111. o.componentToCentreAround = componentToCentreAround;
  112. o.dialogBackgroundColour = backgroundColour;
  113. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  114. o.useNativeTitleBar = false;
  115. o.resizable = resizable;
  116. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  117. o.launchAsync();
  118. }
  119. #if JUCE_MODAL_LOOPS_PERMITTED
  120. int DialogWindow::showModalDialog (const String& dialogTitle,
  121. Component* const contentComponent,
  122. Component* const componentToCentreAround,
  123. Colour backgroundColour,
  124. const bool escapeKeyTriggersCloseButton,
  125. const bool resizable,
  126. const bool useBottomRightCornerResizer)
  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.useNativeTitleBar = false;
  135. o.resizable = resizable;
  136. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  137. return o.runModal();
  138. }
  139. #endif
  140. } // namespace juce