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.

165 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. DialogWindow::DialogWindow (const String& name, const Colour& colour,
  19. const bool escapeCloses, const bool onDesktop)
  20. : DocumentWindow (name, colour, DocumentWindow::closeButton, onDesktop),
  21. escapeKeyTriggersCloseButton (escapeCloses)
  22. {
  23. }
  24. DialogWindow::~DialogWindow()
  25. {
  26. }
  27. bool DialogWindow::keyPressed (const KeyPress& key)
  28. {
  29. if (escapeKeyTriggersCloseButton && key == KeyPress::escapeKey)
  30. {
  31. setVisible (false);
  32. return true;
  33. }
  34. return DocumentWindow::keyPressed (key);
  35. }
  36. void DialogWindow::resized()
  37. {
  38. DocumentWindow::resized();
  39. if (escapeKeyTriggersCloseButton)
  40. {
  41. if (Button* const close = getCloseButton())
  42. {
  43. const KeyPress esc (KeyPress::escapeKey, 0, 0);
  44. if (! close->isRegisteredForShortcut (esc))
  45. close->addShortcut (esc);
  46. }
  47. }
  48. }
  49. //==============================================================================
  50. class DefaultDialogWindow : public DialogWindow
  51. {
  52. public:
  53. DefaultDialogWindow (LaunchOptions& options)
  54. : DialogWindow (options.dialogTitle, options.dialogBackgroundColour,
  55. options.escapeKeyTriggersCloseButton, true)
  56. {
  57. setUsingNativeTitleBar (options.useNativeTitleBar);
  58. if (! JUCEApplication::isStandaloneApp())
  59. setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level
  60. if (options.content.willDeleteObject())
  61. setContentOwned (options.content.release(), true);
  62. else
  63. setContentNonOwned (options.content.release(), true);
  64. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  65. setResizable (options.resizable, options.useBottomRightCornerResizer);
  66. }
  67. void closeButtonPressed()
  68. {
  69. setVisible (false);
  70. }
  71. private:
  72. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow);
  73. };
  74. DialogWindow::LaunchOptions::LaunchOptions() noexcept
  75. : dialogBackgroundColour (Colours::lightgrey),
  76. componentToCentreAround (nullptr),
  77. escapeKeyTriggersCloseButton (true),
  78. useNativeTitleBar (true),
  79. resizable (true),
  80. useBottomRightCornerResizer (false)
  81. {
  82. }
  83. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  84. {
  85. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  86. DefaultDialogWindow* const d = new DefaultDialogWindow (*this);
  87. d->enterModalState (true, nullptr, true);
  88. return d;
  89. }
  90. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  91. int DialogWindow::LaunchOptions::runModal()
  92. {
  93. return launchAsync()->runModalLoop();
  94. }
  95. #endif
  96. //==============================================================================
  97. void DialogWindow::showDialog (const String& dialogTitle,
  98. Component* const contentComponent,
  99. Component* const componentToCentreAround,
  100. const Colour& backgroundColour,
  101. const bool escapeKeyTriggersCloseButton,
  102. const bool resizable,
  103. const bool useBottomRightCornerResizer)
  104. {
  105. LaunchOptions o;
  106. o.dialogTitle = dialogTitle;
  107. o.content.setNonOwned (contentComponent);
  108. o.componentToCentreAround = componentToCentreAround;
  109. o.dialogBackgroundColour = backgroundColour;
  110. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  111. o.useNativeTitleBar = false;
  112. o.resizable = resizable;
  113. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  114. o.launchAsync();
  115. }
  116. #if JUCE_MODAL_LOOPS_PERMITTED
  117. int DialogWindow::showModalDialog (const String& dialogTitle,
  118. Component* const contentComponent,
  119. Component* const componentToCentreAround,
  120. const Colour& backgroundColour,
  121. const bool escapeKeyTriggersCloseButton,
  122. const bool resizable,
  123. const bool useBottomRightCornerResizer)
  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.useNativeTitleBar = false;
  132. o.resizable = resizable;
  133. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  134. return o.runModal();
  135. }
  136. #endif