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_CallOutBox.cpp 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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,
  62. const Rectangle<int>& area,
  63. Component* parent)
  64. {
  65. jassert (content != nullptr); // must be a valid content component!
  66. return (new CallOutBoxCallback (content, area, parent))->callout;
  67. }
  68. //==============================================================================
  69. void CallOutBox::setArrowSize (const float newSize)
  70. {
  71. arrowSize = newSize;
  72. borderSpace = jmax (20, (int) arrowSize);
  73. refreshPath();
  74. }
  75. void CallOutBox::paint (Graphics& g)
  76. {
  77. getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
  78. }
  79. void CallOutBox::resized()
  80. {
  81. content.setTopLeftPosition (borderSpace, borderSpace);
  82. refreshPath();
  83. }
  84. void CallOutBox::moved()
  85. {
  86. refreshPath();
  87. }
  88. void CallOutBox::childBoundsChanged (Component*)
  89. {
  90. updatePosition (targetArea, availableArea);
  91. }
  92. bool CallOutBox::hitTest (int x, int y)
  93. {
  94. return outline.contains ((float) x, (float) y);
  95. }
  96. void CallOutBox::inputAttemptWhenModal()
  97. {
  98. const Point<int> mousePos (getMouseXYRelative() + getBounds().getPosition());
  99. if (targetArea.contains (mousePos))
  100. {
  101. // if you click on the area that originally popped-up the callout, you expect it
  102. // to get rid of the box, but deleting the box here allows the click to pass through and
  103. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  104. postCommandMessage (callOutBoxDismissCommandId);
  105. }
  106. else
  107. {
  108. exitModalState (0);
  109. setVisible (false);
  110. }
  111. }
  112. void CallOutBox::handleCommandMessage (int commandId)
  113. {
  114. Component::handleCommandMessage (commandId);
  115. if (commandId == callOutBoxDismissCommandId)
  116. {
  117. exitModalState (0);
  118. setVisible (false);
  119. }
  120. }
  121. bool CallOutBox::keyPressed (const KeyPress& key)
  122. {
  123. if (key.isKeyCode (KeyPress::escapeKey))
  124. {
  125. inputAttemptWhenModal();
  126. return true;
  127. }
  128. return false;
  129. }
  130. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  131. {
  132. targetArea = newAreaToPointTo;
  133. availableArea = newAreaToFitIn;
  134. Rectangle<int> newBounds (content.getWidth() + borderSpace * 2,
  135. content.getHeight() + borderSpace * 2);
  136. const int hw = newBounds.getWidth() / 2;
  137. const int hh = newBounds.getHeight() / 2;
  138. const float hwReduced = (float) (hw - borderSpace * 3);
  139. const float hhReduced = (float) (hh - borderSpace * 3);
  140. const float arrowIndent = borderSpace - arrowSize;
  141. Point<float> targets[4] = { Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getBottom()),
  142. Point<float> ((float) targetArea.getRight(), (float) targetArea.getCentreY()),
  143. Point<float> ((float) targetArea.getX(), (float) targetArea.getCentreY()),
  144. Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getY()) };
  145. Line<float> lines[4] = { Line<float> (targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent)),
  146. Line<float> (targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced)),
  147. Line<float> (targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced)),
  148. Line<float> (targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent))) };
  149. const Rectangle<float> centrePointArea (newAreaToFitIn.reduced (hw, hh).toFloat());
  150. const Point<float> targetCentre (targetArea.getCentre().toFloat());
  151. float nearest = 1.0e9f;
  152. for (int i = 0; i < 4; ++i)
  153. {
  154. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  155. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  156. const Point<float> centre (constrainedLine.findNearestPointTo (targetCentre));
  157. float distanceFromCentre = centre.getDistanceFrom (targets[i]);
  158. if (! (centrePointArea.contains (lines[i].getStart()) || centrePointArea.contains (lines[i].getEnd())))
  159. distanceFromCentre += 1000.0f;
  160. if (distanceFromCentre < nearest)
  161. {
  162. nearest = distanceFromCentre;
  163. targetPoint = targets[i];
  164. newBounds.setPosition ((int) (centre.x - hw),
  165. (int) (centre.y - hh));
  166. }
  167. }
  168. setBounds (newBounds);
  169. }
  170. void CallOutBox::refreshPath()
  171. {
  172. repaint();
  173. background = Image::null;
  174. outline.clear();
  175. const float gap = 4.5f;
  176. outline.addBubble (content.getBounds().toFloat().expanded (gap, gap),
  177. getLocalBounds().toFloat(),
  178. targetPoint - getPosition().toFloat(),
  179. 9.0f, arrowSize * 0.7f);
  180. }