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.

221 lines
7.4KB

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