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.

260 lines
8.3KB

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