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.

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