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.

179 lines
5.7KB

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