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.8KB

9 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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::escapeKeyPressed()
  27. {
  28. if (escapeKeyTriggersCloseButton)
  29. {
  30. setVisible (false);
  31. return true;
  32. }
  33. return false;
  34. }
  35. bool DialogWindow::keyPressed (const KeyPress& key)
  36. {
  37. if (key == KeyPress::escapeKey && escapeKeyPressed())
  38. return true;
  39. return DocumentWindow::keyPressed (key);
  40. }
  41. void DialogWindow::resized()
  42. {
  43. DocumentWindow::resized();
  44. if (escapeKeyTriggersCloseButton)
  45. {
  46. if (Button* const close = getCloseButton())
  47. {
  48. const KeyPress esc (KeyPress::escapeKey, 0, 0);
  49. if (! close->isRegisteredForShortcut (esc))
  50. close->addShortcut (esc);
  51. }
  52. }
  53. }
  54. //==============================================================================
  55. class DefaultDialogWindow : public DialogWindow
  56. {
  57. public:
  58. DefaultDialogWindow (LaunchOptions& options)
  59. : DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
  60. options.escapeKeyTriggersCloseButton, true)
  61. {
  62. setUsingNativeTitleBar (options.useNativeTitleBar);
  63. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  64. if (options.content.willDeleteObject())
  65. setContentOwned (options.content.release(), true);
  66. else
  67. setContentNonOwned (options.content.release(), true);
  68. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  69. setResizable (options.resizable, options.useBottomRightCornerResizer);
  70. }
  71. void closeButtonPressed() override
  72. {
  73. setVisible (false);
  74. }
  75. private:
  76. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  77. };
  78. DialogWindow::LaunchOptions::LaunchOptions() noexcept
  79. : dialogBackgroundColour (Colours::lightgrey),
  80. componentToCentreAround (nullptr),
  81. escapeKeyTriggersCloseButton (true),
  82. useNativeTitleBar (true),
  83. resizable (true),
  84. useBottomRightCornerResizer (false)
  85. {
  86. }
  87. DialogWindow* DialogWindow::LaunchOptions::create()
  88. {
  89. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  90. return new DefaultDialogWindow (*this);
  91. }
  92. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  93. {
  94. DialogWindow* const d = create();
  95. d->enterModalState (true, nullptr, true);
  96. return d;
  97. }
  98. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  99. int DialogWindow::LaunchOptions::runModal()
  100. {
  101. return launchAsync()->runModalLoop();
  102. }
  103. #endif
  104. //==============================================================================
  105. void DialogWindow::showDialog (const String& dialogTitle,
  106. Component* const contentComponent,
  107. Component* const componentToCentreAround,
  108. Colour backgroundColour,
  109. const bool escapeKeyTriggersCloseButton,
  110. const bool resizable,
  111. const bool useBottomRightCornerResizer,
  112. const bool useNativeTitleBar)
  113. {
  114. LaunchOptions o;
  115. o.dialogTitle = dialogTitle;
  116. o.content.setNonOwned (contentComponent);
  117. o.componentToCentreAround = componentToCentreAround;
  118. o.dialogBackgroundColour = backgroundColour;
  119. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  120. o.resizable = resizable;
  121. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  122. o.useNativeTitleBar = useNativeTitleBar;
  123. o.launchAsync();
  124. }
  125. #if JUCE_MODAL_LOOPS_PERMITTED
  126. int DialogWindow::showModalDialog (const String& dialogTitle,
  127. Component* const contentComponent,
  128. Component* const componentToCentreAround,
  129. Colour backgroundColour,
  130. const bool escapeKeyTriggersCloseButton,
  131. const bool resizable,
  132. const bool useBottomRightCornerResizer,
  133. const bool useNativeTitleBar)
  134. {
  135. LaunchOptions o;
  136. o.dialogTitle = dialogTitle;
  137. o.content.setNonOwned (contentComponent);
  138. o.componentToCentreAround = componentToCentreAround;
  139. o.dialogBackgroundColour = backgroundColour;
  140. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  141. o.resizable = resizable;
  142. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  143. o.useNativeTitleBar = useNativeTitleBar;
  144. return o.runModal();
  145. }
  146. #endif