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.

263 lines
9.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. CallOutBox::CallOutBox (Component& contentComponent,
  19. Component& componentToPointTo,
  20. Component* const parent)
  21. : borderSpace (20), arrowSize (16.0f), content (contentComponent)
  22. {
  23. addAndMakeVisible (&content);
  24. if (parent != nullptr)
  25. {
  26. parent->addChildComponent (this);
  27. updatePosition (parent->getLocalArea (&componentToPointTo, componentToPointTo.getLocalBounds()),
  28. parent->getLocalBounds());
  29. setVisible (true);
  30. }
  31. else
  32. {
  33. if (! JUCEApplication::isStandaloneApp())
  34. setAlwaysOnTop (true); // for a plugin, make it always-on-top because the host windows are often top-level
  35. updatePosition (componentToPointTo.getScreenBounds(),
  36. componentToPointTo.getParentMonitorArea());
  37. addToDesktop (ComponentPeer::windowIsTemporary);
  38. }
  39. }
  40. CallOutBox::~CallOutBox()
  41. {
  42. }
  43. //==============================================================================
  44. void CallOutBox::setArrowSize (const float newSize)
  45. {
  46. arrowSize = newSize;
  47. borderSpace = jmax (20, (int) arrowSize);
  48. refreshPath();
  49. }
  50. void CallOutBox::paint (Graphics& g)
  51. {
  52. if (background.isNull())
  53. {
  54. background = Image (Image::ARGB, getWidth(), getHeight(), true);
  55. Graphics g2 (background);
  56. getLookAndFeel().drawCallOutBoxBackground (*this, g2, outline);
  57. }
  58. g.setColour (Colours::black);
  59. g.drawImageAt (background, 0, 0);
  60. }
  61. void CallOutBox::resized()
  62. {
  63. content.setTopLeftPosition (borderSpace, borderSpace);
  64. refreshPath();
  65. }
  66. void CallOutBox::moved()
  67. {
  68. refreshPath();
  69. }
  70. void CallOutBox::childBoundsChanged (Component*)
  71. {
  72. updatePosition (targetArea, availableArea);
  73. }
  74. bool CallOutBox::hitTest (int x, int y)
  75. {
  76. return outline.contains ((float) x, (float) y);
  77. }
  78. enum { callOutBoxDismissCommandId = 0x4f83a04b };
  79. void CallOutBox::inputAttemptWhenModal()
  80. {
  81. const Point<int> mousePos (getMouseXYRelative() + getBounds().getPosition());
  82. if (targetArea.contains (mousePos))
  83. {
  84. // if you click on the area that originally popped-up the callout, you expect it
  85. // to get rid of the box, but deleting the box here allows the click to pass through and
  86. // probably re-trigger it, so we need to dismiss the box asynchronously to consume the click..
  87. postCommandMessage (callOutBoxDismissCommandId);
  88. }
  89. else
  90. {
  91. exitModalState (0);
  92. setVisible (false);
  93. }
  94. }
  95. void CallOutBox::handleCommandMessage (int commandId)
  96. {
  97. Component::handleCommandMessage (commandId);
  98. if (commandId == callOutBoxDismissCommandId)
  99. {
  100. exitModalState (0);
  101. setVisible (false);
  102. }
  103. }
  104. bool CallOutBox::keyPressed (const KeyPress& key)
  105. {
  106. if (key.isKeyCode (KeyPress::escapeKey))
  107. {
  108. inputAttemptWhenModal();
  109. return true;
  110. }
  111. return false;
  112. }
  113. void CallOutBox::updatePosition (const Rectangle<int>& newAreaToPointTo, const Rectangle<int>& newAreaToFitIn)
  114. {
  115. targetArea = newAreaToPointTo;
  116. availableArea = newAreaToFitIn;
  117. Rectangle<int> newBounds (0, 0,
  118. content.getWidth() + borderSpace * 2,
  119. content.getHeight() + borderSpace * 2);
  120. const int hw = newBounds.getWidth() / 2;
  121. const int hh = newBounds.getHeight() / 2;
  122. const float hwReduced = (float) (hw - borderSpace * 3);
  123. const float hhReduced = (float) (hh - borderSpace * 3);
  124. const float arrowIndent = borderSpace - arrowSize;
  125. Point<float> targets[4] = { Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getBottom()),
  126. Point<float> ((float) targetArea.getRight(), (float) targetArea.getCentreY()),
  127. Point<float> ((float) targetArea.getX(), (float) targetArea.getCentreY()),
  128. Point<float> ((float) targetArea.getCentreX(), (float) targetArea.getY()) };
  129. Line<float> lines[4] = { Line<float> (targets[0].translated (-hwReduced, hh - arrowIndent), targets[0].translated (hwReduced, hh - arrowIndent)),
  130. Line<float> (targets[1].translated (hw - arrowIndent, -hhReduced), targets[1].translated (hw - arrowIndent, hhReduced)),
  131. Line<float> (targets[2].translated (-(hw - arrowIndent), -hhReduced), targets[2].translated (-(hw - arrowIndent), hhReduced)),
  132. Line<float> (targets[3].translated (-hwReduced, -(hh - arrowIndent)), targets[3].translated (hwReduced, -(hh - arrowIndent))) };
  133. const Rectangle<float> centrePointArea (newAreaToFitIn.reduced (hw, hh).toFloat());
  134. float nearest = 1.0e9f;
  135. for (int i = 0; i < 4; ++i)
  136. {
  137. Line<float> constrainedLine (centrePointArea.getConstrainedPoint (lines[i].getStart()),
  138. centrePointArea.getConstrainedPoint (lines[i].getEnd()));
  139. const Point<float> centre (constrainedLine.findNearestPointTo (centrePointArea.getCentre()));
  140. float distanceFromCentre = centre.getDistanceFrom (centrePointArea.getCentre());
  141. if (! (centrePointArea.contains (lines[i].getStart()) || centrePointArea.contains (lines[i].getEnd())))
  142. distanceFromCentre *= 2.0f;
  143. if (distanceFromCentre < nearest)
  144. {
  145. nearest = distanceFromCentre;
  146. targetPoint = targets[i];
  147. newBounds.setPosition ((int) (centre.getX() - hw),
  148. (int) (centre.getY() - hh));
  149. }
  150. }
  151. setBounds (newBounds);
  152. }
  153. void CallOutBox::refreshPath()
  154. {
  155. repaint();
  156. background = Image::null;
  157. outline.clear();
  158. const float gap = 4.5f;
  159. const float cornerSize = 9.0f;
  160. const float cornerSize2 = 2.0f * cornerSize;
  161. const float arrowBaseWidth = arrowSize * 0.7f;
  162. const Rectangle<float> area (content.getBounds().toFloat().expanded (gap, gap));
  163. const Point<float> target (targetPoint - getPosition().toFloat());
  164. outline.startNewSubPath (area.getX() + cornerSize, area.getY());
  165. const float targetLimitX = area.getX() + cornerSize + arrowBaseWidth;
  166. const float targetLimitW = area.getWidth() - cornerSize2 - arrowBaseWidth * 2.0f;
  167. const float targetLimitY = area.getY() + cornerSize + arrowBaseWidth;
  168. const float targetLimitH = area.getHeight() - cornerSize2 - arrowBaseWidth * 2.0f;
  169. if (Rectangle<float> (targetLimitX, 1.0f,
  170. targetLimitW, area.getY() - 2.0f).contains (target))
  171. {
  172. outline.lineTo (target.x - arrowBaseWidth, area.getY());
  173. outline.lineTo (target.x, target.y);
  174. outline.lineTo (target.x + arrowBaseWidth, area.getY());
  175. }
  176. outline.lineTo (area.getRight() - cornerSize, area.getY());
  177. outline.addArc (area.getRight() - cornerSize2, area.getY(), cornerSize2, cornerSize2, 0, float_Pi * 0.5f);
  178. if (Rectangle<float> (area.getRight() + 1.0f, targetLimitY,
  179. getWidth() - area.getRight() - 2.0f, targetLimitH).contains (target))
  180. {
  181. outline.lineTo (area.getRight(), target.y - arrowBaseWidth);
  182. outline.lineTo (target.x, target.y);
  183. outline.lineTo (area.getRight(), target.y + arrowBaseWidth);
  184. }
  185. outline.lineTo (area.getRight(), area.getBottom() - cornerSize);
  186. outline.addArc (area.getRight() - cornerSize2, area.getBottom() - cornerSize2, cornerSize2, cornerSize2, float_Pi * 0.5f, float_Pi);
  187. if (Rectangle<float> (targetLimitX, area.getBottom() + 1.0f,
  188. targetLimitW, getHeight() - area.getBottom() - 2.0f).contains (target))
  189. {
  190. outline.lineTo (target.x + arrowBaseWidth, area.getBottom());
  191. outline.lineTo (target.x, target.y);
  192. outline.lineTo (target.x - arrowBaseWidth, area.getBottom());
  193. }
  194. outline.lineTo (area.getX() + cornerSize, area.getBottom());
  195. outline.addArc (area.getX(), area.getBottom() - cornerSize2, cornerSize2, cornerSize2, float_Pi, float_Pi * 1.5f);
  196. if (Rectangle<float> (1.0f, targetLimitY, area.getX() - 2.0f, targetLimitH).contains (target))
  197. {
  198. outline.lineTo (area.getX(), target.y + arrowBaseWidth);
  199. outline.lineTo (target.x, target.y);
  200. outline.lineTo (area.getX(), target.y - arrowBaseWidth);
  201. }
  202. outline.lineTo (area.getX(), area.getY() + cornerSize);
  203. outline.addArc (area.getX(), area.getY(), cornerSize2, cornerSize2, float_Pi * 1.5f, float_Pi * 2.0f - 0.05f);
  204. outline.closeSubPath();
  205. }