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.

227 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. CallOutBox::CallOutBox (Component& c, const Rectangle<int>& area, Component* const parent)
  18. : borderSpace (20), arrowSize (16.0f), content (c)
  19. {
  20. addAndMakeVisible (content);
  21. if (parent != nullptr)
  22. {
  23. parent->addChildComponent (this);
  24. updatePosition (area, parent->getLocalBounds());
  25. setVisible (true);
  26. }
  27. else
  28. {
  29. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  30. updatePosition (area, Desktop::getInstance().getDisplays()
  31. .getDisplayContaining (area.getCentre()).userArea);
  32. addToDesktop (ComponentPeer::windowIsTemporary);
  33. }
  34. }
  35. CallOutBox::~CallOutBox()
  36. {
  37. }
  38. enum { callOutBoxDismissCommandId = 0x4f83a04b };
  39. //==============================================================================
  40. class CallOutBoxCallback : public ModalComponentManager::Callback,
  41. private Timer
  42. {
  43. public:
  44. CallOutBoxCallback (Component* c, const Rectangle<int>& area, Component* parent)
  45. : content (c), callout (*c, area, parent)
  46. {
  47. callout.setVisible (true);
  48. callout.enterModalState (true, this);
  49. startTimer (200);
  50. }
  51. void modalStateFinished (int) override {}
  52. void timerCallback() override
  53. {
  54. if (! Process::isForegroundProcess())
  55. callout.postCommandMessage (callOutBoxDismissCommandId);
  56. }
  57. ScopedPointer<Component> content;
  58. CallOutBox callout;
  59. JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback)
  60. };
  61. CallOutBox& CallOutBox::launchAsynchronously (Component* content, const Rectangle<int>& area, Component* parent)
  62. {
  63. jassert (content != nullptr); // must be a valid content component!
  64. return (new CallOutBoxCallback (content, area, parent))->callout;
  65. }
  66. //==============================================================================
  67. void CallOutBox::setArrowSize (const float newSize)
  68. {
  69. arrowSize = newSize;
  70. borderSpace = jmax (20, (int) arrowSize);
  71. refreshPath();
  72. }
  73. void CallOutBox::paint (Graphics& g)
  74. {
  75. getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
  76. }
  77. void CallOutBox::resized()
  78. {
  79. content.setTopLeftPosition (borderSpace, borderSpace);
  80. refreshPath();
  81. }
  82. void CallOutBox::moved()
  83. {
  84. refreshPath();
  85. }
  86. void CallOutBox::childBoundsChanged (Component*)
  87. {
  88. updatePosition (targetArea, availableArea);
  89. }
  90. bool CallOutBox::hitTest (int x, int y)
  91. {
  92. return outline.contains ((float) x, (float) y);
  93. }
  94. void CallOutBox::inputAttemptWhenModal()
  95. {
  96. const Point<int> mousePos (getMouseXYRelative() + getBounds().getPosition());
  97. if (targetArea.contains (mousePos))
  98. {
  99. // if you click on the area that originally popped-up the callout, you expect it
  100. // to get rid of the box, but deleting the box here allows the click to pass through and
  101. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  102. postCommandMessage (callOutBoxDismissCommandId);
  103. }
  104. else
  105. {
  106. exitModalState (0);
  107. setVisible (false);
  108. }
  109. }
  110. void CallOutBox::handleCommandMessage (int commandId)
  111. {
  112. Component::handleCommandMessage (commandId);
  113. if (commandId == callOutBoxDismissCommandId)
  114. {
  115. exitModalState (0);
  116. setVisible (false);
  117. }
  118. }
  119. bool CallOutBox::keyPressed (const KeyPress& key)
  120. {
  121. if (key.isKeyCode (KeyPress::escapeKey))
  122. {
  123. inputAttemptWhenModal();
  124. return true;
  125. }
  126. return false;
  127. }
  128. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  129. {
  130. targetArea = newAreaToPointTo;
  131. availableArea = newAreaToFitIn;
  132. Rectangle<int> newBounds (content.getWidth() + borderSpace * 2,
  133. content.getHeight() + borderSpace * 2);
  134. const int hw = newBounds.getWidth() / 2;
  135. const int hh = newBounds.getHeight() / 2;
  136. const float hwReduced = (float) (hw - borderSpace * 2);
  137. const float hhReduced = (float) (hh - borderSpace * 2);
  138. const float arrowIndent = borderSpace - arrowSize;
  139. Point<float> targets[4] = { Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getBottom()),
  140. Point<float> ((float) targetArea.getRight(), (float) targetArea.getCentreY()),
  141. Point<float> ((float) targetArea.getX(), (float) targetArea.getCentreY()),
  142. Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getY()) };
  143. Line<float> lines[4] = { Line<float> (targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent)),
  144. Line<float> (targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced)),
  145. Line<float> (targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced)),
  146. Line<float> (targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent))) };
  147. const Rectangle<float> centrePointArea (newAreaToFitIn.reduced (hw, hh).toFloat());
  148. const Point<float> targetCentre (targetArea.getCentre().toFloat());
  149. float nearest = 1.0e9f;
  150. for (int i = 0; i < 4; ++i)
  151. {
  152. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  153. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  154. const Point<float> centre (constrainedLine.findNearestPointTo (targetCentre));
  155. float distanceFromCentre = centre.getDistanceFrom (targets[i]);
  156. if (! centrePointArea.intersects (lines[i]))
  157. distanceFromCentre += 1000.0f;
  158. if (distanceFromCentre < nearest)
  159. {
  160. nearest = distanceFromCentre;
  161. targetPoint = targets[i];
  162. newBounds.setPosition ((int) (centre.x - hw),
  163. (int) (centre.y - hh));
  164. }
  165. }
  166. setBounds (newBounds);
  167. }
  168. void CallOutBox::refreshPath()
  169. {
  170. repaint();
  171. background = Image::null;
  172. outline.clear();
  173. const float gap = 4.5f;
  174. outline.addBubble (content.getBounds().toFloat().expanded (gap, gap),
  175. getLocalBounds().toFloat(),
  176. targetPoint - getPosition().toFloat(),
  177. 9.0f, arrowSize * 0.7f);
  178. }