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.

222 lines
7.6KB

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