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

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