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.

168 lines
5.7KB

  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. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  59. if (options.content.willDeleteObject())
  60. setContentOwned (options.content.release(), true);
  61. else
  62. setContentNonOwned (options.content.release(), true);
  63. centreAroundComponent (options.componentToCentreAround, getWidth(), getHeight());
  64. setResizable (options.resizable, options.useBottomRightCornerResizer);
  65. }
  66. void closeButtonPressed()
  67. {
  68. setVisible (false);
  69. }
  70. private:
  71. JUCE_DECLARE_NON_COPYABLE (DefaultDialogWindow)
  72. };
  73. DialogWindow::LaunchOptions::LaunchOptions() noexcept
  74. : dialogBackgroundColour (Colours::lightgrey),
  75. componentToCentreAround (nullptr),
  76. escapeKeyTriggersCloseButton (true),
  77. useNativeTitleBar (true),
  78. resizable (true),
  79. useBottomRightCornerResizer (false)
  80. {
  81. }
  82. DialogWindow* DialogWindow::LaunchOptions::create()
  83. {
  84. jassert (content != nullptr); // You need to provide some kind of content for the dialog!
  85. return new DefaultDialogWindow (*this);
  86. }
  87. DialogWindow* DialogWindow::LaunchOptions::launchAsync()
  88. {
  89. DialogWindow* const d = create();
  90. d->enterModalState (true, nullptr, true);
  91. return d;
  92. }
  93. #if JUCE_MODAL_LOOPS_PERMITTED || DOXYGEN
  94. int DialogWindow::LaunchOptions::runModal()
  95. {
  96. return launchAsync()->runModalLoop();
  97. }
  98. #endif
  99. //==============================================================================
  100. void DialogWindow::showDialog (const String& dialogTitle,
  101. Component* const contentComponent,
  102. Component* const componentToCentreAround,
  103. const Colour& backgroundColour,
  104. const bool escapeKeyTriggersCloseButton,
  105. const bool resizable,
  106. const bool useBottomRightCornerResizer)
  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.useNativeTitleBar = false;
  115. o.resizable = resizable;
  116. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  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. const Colour& backgroundColour,
  124. const bool escapeKeyTriggersCloseButton,
  125. const bool resizable,
  126. const bool useBottomRightCornerResizer)
  127. {
  128. LaunchOptions o;
  129. o.dialogTitle = dialogTitle;
  130. o.content.setNonOwned (contentComponent);
  131. o.componentToCentreAround = componentToCentreAround;
  132. o.dialogBackgroundColour = backgroundColour;
  133. o.escapeKeyTriggersCloseButton = escapeKeyTriggersCloseButton;
  134. o.useNativeTitleBar = false;
  135. o.resizable = resizable;
  136. o.useBottomRightCornerResizer = useBottomRightCornerResizer;
  137. return o.runModal();
  138. }
  139. #endif