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 8.0KB

7 years ago
7 years ago
10 years ago
7 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
7 years ago
10 years ago
10 years ago
10 years ago
7 years ago
10 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. CallOutBox::CallOutBox (Component& c, Rectangle<int> area, Component* const parent)
  22. : content (c)
  23. {
  24. addAndMakeVisible (content);
  25. if (parent != nullptr)
  26. {
  27. parent->addChildComponent (this);
  28. updatePosition (area, parent->getLocalBounds());
  29. setVisible (true);
  30. }
  31. else
  32. {
  33. setAlwaysOnTop (juce_areThereAnyAlwaysOnTopWindows());
  34. updatePosition (area, Desktop::getInstance().getDisplays()
  35. .getDisplayContaining (area.getCentre()).userArea);
  36. addToDesktop (ComponentPeer::windowIsTemporary);
  37. startTimer (100);
  38. }
  39. creationTime = Time::getCurrentTime();
  40. }
  41. CallOutBox::~CallOutBox()
  42. {
  43. }
  44. //==============================================================================
  45. class CallOutBoxCallback : public ModalComponentManager::Callback,
  46. private Timer
  47. {
  48. public:
  49. CallOutBoxCallback (Component* c, const Rectangle<int>& area, Component* parent)
  50. : content (c), callout (*c, area, parent)
  51. {
  52. callout.setVisible (true);
  53. callout.enterModalState (true, this);
  54. startTimer (200);
  55. }
  56. void modalStateFinished (int) override {}
  57. void timerCallback() override
  58. {
  59. if (! Process::isForegroundProcess())
  60. callout.dismiss();
  61. }
  62. ScopedPointer<Component> content;
  63. CallOutBox callout;
  64. JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback)
  65. };
  66. CallOutBox& CallOutBox::launchAsynchronously (Component* content, Rectangle<int> area, Component* parent)
  67. {
  68. jassert (content != nullptr); // must be a valid content component!
  69. return (new CallOutBoxCallback (content, area, parent))->callout;
  70. }
  71. //==============================================================================
  72. void CallOutBox::setArrowSize (const float newSize)
  73. {
  74. arrowSize = newSize;
  75. refreshPath();
  76. }
  77. int CallOutBox::getBorderSize() const noexcept
  78. {
  79. return jmax (getLookAndFeel().getCallOutBoxBorderSize (*this), (int) arrowSize);
  80. }
  81. void CallOutBox::paint (Graphics& g)
  82. {
  83. getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
  84. }
  85. void CallOutBox::resized()
  86. {
  87. auto borderSpace = getBorderSize();
  88. content.setTopLeftPosition (borderSpace, borderSpace);
  89. refreshPath();
  90. }
  91. void CallOutBox::moved()
  92. {
  93. refreshPath();
  94. }
  95. void CallOutBox::childBoundsChanged (Component*)
  96. {
  97. updatePosition (targetArea, availableArea);
  98. }
  99. bool CallOutBox::hitTest (int x, int y)
  100. {
  101. return outline.contains ((float) x, (float) y);
  102. }
  103. void CallOutBox::inputAttemptWhenModal()
  104. {
  105. if (dismissalMouseClicksAreAlwaysConsumed
  106. || targetArea.contains (getMouseXYRelative() + getBounds().getPosition()))
  107. {
  108. // if you click on the area that originally popped-up the callout, you expect it
  109. // to get rid of the box, but deleting the box here allows the click to pass through and
  110. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  111. // For touchscreens, we make sure not to dismiss the CallOutBox immediately,
  112. // as Windows still sends touch events before the CallOutBox had a chance
  113. // to really open.
  114. auto elapsed = Time::getCurrentTime() - creationTime;
  115. if (elapsed.inMilliseconds() > 200)
  116. dismiss();
  117. }
  118. else
  119. {
  120. exitModalState (0);
  121. setVisible (false);
  122. }
  123. }
  124. void CallOutBox::setDismissalMouseClicksAreAlwaysConsumed (bool b) noexcept
  125. {
  126. dismissalMouseClicksAreAlwaysConsumed = b;
  127. }
  128. enum { callOutBoxDismissCommandId = 0x4f83a04b };
  129. void CallOutBox::handleCommandMessage (int commandId)
  130. {
  131. Component::handleCommandMessage (commandId);
  132. if (commandId == callOutBoxDismissCommandId)
  133. {
  134. exitModalState (0);
  135. setVisible (false);
  136. }
  137. }
  138. void CallOutBox::dismiss()
  139. {
  140. postCommandMessage (callOutBoxDismissCommandId);
  141. }
  142. bool CallOutBox::keyPressed (const KeyPress& key)
  143. {
  144. if (key.isKeyCode (KeyPress::escapeKey))
  145. {
  146. inputAttemptWhenModal();
  147. return true;
  148. }
  149. return false;
  150. }
  151. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  152. {
  153. targetArea = newAreaToPointTo;
  154. availableArea = newAreaToFitIn;
  155. auto borderSpace = getBorderSize();
  156. Rectangle<int> newBounds (content.getWidth() + borderSpace * 2,
  157. content.getHeight() + borderSpace * 2);
  158. auto hw = newBounds.getWidth() / 2;
  159. auto hh = newBounds.getHeight() / 2;
  160. auto hwReduced = (float) (hw - borderSpace * 2);
  161. auto hhReduced = (float) (hh - borderSpace * 2);
  162. auto arrowIndent = borderSpace - arrowSize;
  163. Point<float> targets[4] = { { (float) targetArea.getCentreX(), (float) targetArea.getBottom() },
  164. { (float) targetArea.getRight(), (float) targetArea.getCentreY() },
  165. { (float) targetArea.getX(), (float) targetArea.getCentreY() },
  166. { (float) targetArea.getCentreX(), (float) targetArea.getY() } };
  167. Line<float> lines[4] = { { targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent) },
  168. { targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced) },
  169. { targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced) },
  170. { targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent)) } };
  171. auto centrePointArea = newAreaToFitIn.reduced (hw, hh).toFloat();
  172. auto targetCentre = targetArea.getCentre().toFloat();
  173. float nearest = 1.0e9f;
  174. for (int i = 0; i < 4; ++i)
  175. {
  176. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  177. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  178. auto centre = constrainedLine.findNearestPointTo (targetCentre);
  179. auto distanceFromCentre = centre.getDistanceFrom (targets[i]);
  180. if (! centrePointArea.intersects (lines[i]))
  181. distanceFromCentre += 1000.0f;
  182. if (distanceFromCentre < nearest)
  183. {
  184. nearest = distanceFromCentre;
  185. targetPoint = targets[i];
  186. newBounds.setPosition ((int) (centre.x - hw),
  187. (int) (centre.y - hh));
  188. }
  189. }
  190. setBounds (newBounds);
  191. }
  192. void CallOutBox::refreshPath()
  193. {
  194. repaint();
  195. background = {};
  196. outline.clear();
  197. const float gap = 4.5f;
  198. outline.addBubble (content.getBounds().toFloat().expanded (gap, gap),
  199. getLocalBounds().toFloat(),
  200. targetPoint - getPosition().toFloat(),
  201. 9.0f, arrowSize * 0.7f);
  202. }
  203. void CallOutBox::timerCallback()
  204. {
  205. toFront (true);
  206. stopTimer();
  207. }
  208. } // namespace juce