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.

juce_DialogWindow.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. DialogWindow::DialogWindow (const String& name, Colour colour,
  18. const bool escapeCloses, const bool onDesktop)
  19. : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
  20. escapeKeyTriggersCloseButton (escapeCloses)
  21. {
  22. }
  23. DialogWindow::~DialogWindow()
  24. {
  25. }
  26. bool DialogWindow::keyPressed (const KeyPress& key)
  27. {
  28. if (escapeKeyTriggersCloseButton && key == KeyPress::escapeKey)
  29. {
  30. setVisible (false);
  31. return true;
  32. }
  33. return DocumentWindow::keyPressed (key);
  34. }
  35. void DialogWindow::resized()
  36. {
  37. DocumentWindow::resized();
  38. if (escapeKeyTriggersCloseButton)
  39. {
  40. if (Button* const close = getCloseButton())
  41. {
  42. const KeyPress esc (KeyPress::escapeKey, 0, 0);
  43. if (! close->isRegisteredForShortcut (esc))
  44. close->addShortcut (esc);
  45. }
  46. }
  47. }
  48. //==============================================================================
  49. class DefaultDialogWindow : public DialogWindow
  50. {
  51. public:
  52. DefaultDialogWindow (LaunchOptions& options)
  53. : DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
  54. options.escapeKeyTriggersCloseButton, true)
  55. {
  56. setUsingNativeTitleBar (options.useNativeTitleBar);
  57. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  58. if (options.content.willDeleteObject())
  59. setContentOwned (options.content.release(), true);
  60. else
  61. setContentNonOwned (options.content.release(), true);
  62. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  63. setResizable (options.resizable, options.useBottomRightCornerResizer);
  64. }
  65. void closeButtonPressed() override
  66. {
  67. setVisible (false);
  68. }
  69. private:
  70. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  71. };
  72. DialogWindow::LaunchOptions::LaunchOptions() noexcept
  73. : dialogBackgroundColour (Colours::lightgrey),
  74. componentToCentreAround (nullptr),
  75. escapeKeyTriggersCloseButton (true),
  76. useNativeTitleBar (true),
  77. resizable (true),
  78. useBottomRightCornerResizer (false)
  79. {
  80. }
  81. DialogWindow* DialogWindow::LaunchOptions::create()
  82. {
  83. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  84. return new DefaultDialogWindow (*this);
  85. }
  86. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  87. {
  88. DialogWindow* const d = create();
  89. d->enterModalState (true, nullptr, true);
  90. return d;
  91. }
  92. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  93. int DialogWindow::LaunchOptions::runModal()
  94. {
  95. return launchAsync()->runModalLoop();
  96. }
  97. #endif
  98. //==============================================================================
  99. void DialogWindow::showDialog (const String& dialogTitle,
  100. Component* const contentComponent,
  101. Component* const componentToCentreAround,
  102. Colour backgroundColour,
  103. const bool escapeKeyTriggersCloseButton,
  104. const bool resizable,
  105. const bool useBottomRightCornerResizer,
  106. const bool useNativeTitleBar)
  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.resizable = resizable;
  115. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  116. o.useNativeTitleBar = useNativeTitleBar;
  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. const bool useNativeTitleBar)
  128. {
  129. LaunchOptions o;
  130. o.dialogTitle = dialogTitle;
  131. o.content.setNonOwned (contentComponent);
  132. o.componentToCentreAround = componentToCentreAround;
  133. o.dialogBackgroundColour = backgroundColour;
  134. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  135. o.resizable = resizable;
  136. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  137. o.useNativeTitleBar = useNativeTitleBar;
  138. return o.runModal();
  139. }
  140. #endif