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.

267 lines
8.0KB

  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().findDisplayForRect (area).userArea);
  35. addToDesktop (ComponentPeer::windowIsTemporary);
  36. startTimer (100);
  37. }
  38. creationTime = Time::getCurrentTime();
  39. }
  40. CallOutBox::~CallOutBox()
  41. {
  42. }
  43. //==============================================================================
  44. class CallOutBoxCallback : public ModalComponentManager::Callback,
  45. private Timer
  46. {
  47. public:
  48. CallOutBoxCallback (Component* c, const Rectangle<int>& area, Component* parent)
  49. : content (c), callout (*c, area, parent)
  50. {
  51. callout.setVisible (true);
  52. callout.enterModalState (true, this);
  53. startTimer (200);
  54. }
  55. void modalStateFinished (int) override {}
  56. void timerCallback() override
  57. {
  58. if (! Process::isForegroundProcess())
  59. callout.dismiss();
  60. }
  61. std::unique_ptr<Component> content;
  62. CallOutBox callout;
  63. JUCE_DECLARE_NON_COPYABLE (CallOutBoxCallback)
  64. };
  65. CallOutBox& CallOutBox::launchAsynchronously (Component* content, Rectangle<int> area, Component* parent)
  66. {
  67. jassert (content != nullptr); // must be a valid content component!
  68. return (new CallOutBoxCallback (content, area, parent))->callout;
  69. }
  70. //==============================================================================
  71. void CallOutBox::setArrowSize (const float newSize)
  72. {
  73. arrowSize = newSize;
  74. refreshPath();
  75. }
  76. int CallOutBox::getBorderSize() const noexcept
  77. {
  78. return jmax (getLookAndFeel().getCallOutBoxBorderSize (*this), (int) arrowSize);
  79. }
  80. void CallOutBox::paint (Graphics& g)
  81. {
  82. getLookAndFeel().drawCallOutBoxBackground (*this, g, outline, background);
  83. }
  84. void CallOutBox::resized()
  85. {
  86. auto borderSpace = getBorderSize();
  87. content.setTopLeftPosition (borderSpace, borderSpace);
  88. refreshPath();
  89. }
  90. void CallOutBox::moved()
  91. {
  92. refreshPath();
  93. }
  94. void CallOutBox::childBoundsChanged (Component*)
  95. {
  96. updatePosition (targetArea, availableArea);
  97. }
  98. bool CallOutBox::hitTest (int x, int y)
  99. {
  100. return outline.contains ((float) x, (float) y);
  101. }
  102. void CallOutBox::inputAttemptWhenModal()
  103. {
  104. if (dismissalMouseClicksAreAlwaysConsumed
  105. || targetArea.contains (getMouseXYRelative() + getBounds().getPosition()))
  106. {
  107. // if you click on the area that originally popped-up the callout, you expect it
  108. // to get rid of the box, but deleting the box here allows the click to pass through and
  109. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  110. // For touchscreens, we make sure not to dismiss the CallOutBox immediately,
  111. // as Windows still sends touch events before the CallOutBox had a chance
  112. // to really open.
  113. auto elapsed = Time::getCurrentTime() - creationTime;
  114. if (elapsed.inMilliseconds() > 200)
  115. dismiss();
  116. }
  117. else
  118. {
  119. exitModalState (0);
  120. setVisible (false);
  121. }
  122. }
  123. void CallOutBox::setDismissalMouseClicksAreAlwaysConsumed (bool b) noexcept
  124. {
  125. dismissalMouseClicksAreAlwaysConsumed = b;
  126. }
  127. enum { callOutBoxDismissCommandId = 0x4f83a04b };
  128. void CallOutBox::handleCommandMessage (int commandId)
  129. {
  130. Component::handleCommandMessage (commandId);
  131. if (commandId == callOutBoxDismissCommandId)
  132. {
  133. exitModalState (0);
  134. setVisible (false);
  135. }
  136. }
  137. void CallOutBox::dismiss()
  138. {
  139. postCommandMessage (callOutBoxDismissCommandId);
  140. }
  141. bool CallOutBox::keyPressed (const KeyPress& key)
  142. {
  143. if (key.isKeyCode (KeyPress::escapeKey))
  144. {
  145. inputAttemptWhenModal();
  146. return true;
  147. }
  148. return false;
  149. }
  150. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  151. {
  152. targetArea = newAreaToPointTo;
  153. availableArea = newAreaToFitIn;
  154. auto borderSpace = getBorderSize();
  155. Rectangle<int> newBounds (content.getWidth() + borderSpace * 2,
  156. content.getHeight() + borderSpace * 2);
  157. auto hw = newBounds.getWidth() / 2;
  158. auto hh = newBounds.getHeight() / 2;
  159. auto hwReduced = (float) (hw - borderSpace * 2);
  160. auto hhReduced = (float) (hh - borderSpace * 2);
  161. auto arrowIndent = borderSpace - arrowSize;
  162. Point<float> targets[4] = { { (float) targetArea.getCentreX(), (float) targetArea.getBottom() },
  163. { (float) targetArea.getRight(), (float) targetArea.getCentreY() },
  164. { (float) targetArea.getX(), (float) targetArea.getCentreY() },
  165. { (float) targetArea.getCentreX(), (float) targetArea.getY() } };
  166. Line<float> lines[4] = { { targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent) },
  167. { targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced) },
  168. { targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced) },
  169. { targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent)) } };
  170. auto centrePointArea = newAreaToFitIn.reduced (hw, hh).toFloat();
  171. auto targetCentre = targetArea.getCentre().toFloat();
  172. float nearest = 1.0e9f;
  173. for (int i = 0; i < 4; ++i)
  174. {
  175. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  176. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  177. auto centre = constrainedLine.findNearestPointTo (targetCentre);
  178. auto distanceFromCentre = centre.getDistanceFrom (targets[i]);
  179. if (! centrePointArea.intersects (lines[i]))
  180. distanceFromCentre += 1000.0f;
  181. if (distanceFromCentre < nearest)
  182. {
  183. nearest = distanceFromCentre;
  184. targetPoint = targets[i];
  185. newBounds.setPosition ((int) (centre.x - hw),
  186. (int) (centre.y - hh));
  187. }
  188. }
  189. setBounds (newBounds);
  190. }
  191. void CallOutBox::refreshPath()
  192. {
  193. repaint();
  194. background = {};
  195. outline.clear();
  196. const float gap = 4.5f;
  197. outline.addBubble (content.getBounds().toFloat().expanded (gap, gap),
  198. getLocalBounds().toFloat(),
  199. targetPoint - getPosition().toFloat(),
  200. 9.0f, arrowSize * 0.7f);
  201. }
  202. void CallOutBox::timerCallback()
  203. {
  204. toFront (true);
  205. stopTimer();
  206. }
  207. } // namespace juce