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

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