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.

144 lines
5.2KB

  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. BubbleComponent::BubbleComponent()
  19. : allowablePlacements (above | below | left | right)
  20. {
  21. setInterceptsMouseClicks (false, false);
  22. shadow.setShadowProperties (DropShadow (Colours::black.withAlpha (0.35f), 5, Point<int>()));
  23. setComponentEffect (&shadow);
  24. }
  25. BubbleComponent::~BubbleComponent() {}
  26. //==============================================================================
  27. void BubbleComponent::paint (Graphics& g)
  28. {
  29. getLookAndFeel().drawBubble (g, *this, arrowTip.toFloat(), content.toFloat());
  30. g.reduceClipRegion (content);
  31. g.setOrigin (content.getX(), content.getY());
  32. paintContent (g, content.getWidth(), content.getHeight());
  33. }
  34. void BubbleComponent::setAllowedPlacement (const int newPlacement)
  35. {
  36. allowablePlacements = newPlacement;
  37. }
  38. void BubbleComponent::setPosition (Component* componentToPointTo)
  39. {
  40. jassert (componentToPointTo != nullptr);
  41. if (getParentComponent() != nullptr)
  42. setPosition (getParentComponent()->getLocalArea (componentToPointTo, componentToPointTo->getLocalBounds()));
  43. else
  44. setPosition (componentToPointTo->getScreenBounds());
  45. }
  46. void BubbleComponent::setPosition (const Point<int>& pos)
  47. {
  48. setPosition (Rectangle<int> (pos.x, pos.y, 1, 1));
  49. }
  50. //==============================================================================
  51. void BubbleComponent::setPosition (const Rectangle<int>& rectangleToPointTo)
  52. {
  53. const int edgeSpace = 15;
  54. const int arrowLength = 10;
  55. {
  56. int contentW = 150, contentH = 30;
  57. getContentSize (contentW, contentH);
  58. content.setBounds (edgeSpace, edgeSpace, contentW, contentH);
  59. }
  60. const int totalW = content.getWidth() + edgeSpace * 2;
  61. const int totalH = content.getHeight() + edgeSpace * 2;
  62. const Rectangle<int> availableSpace (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  63. : getParentMonitorArea());
  64. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  65. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  66. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  67. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  68. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  69. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  70. && (spaceAbove > totalH + 20 || spaceBelow > totalH + 20))
  71. {
  72. spaceLeft = spaceRight = 0;
  73. }
  74. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  75. && (spaceLeft > totalW + 20 || spaceRight > totalW + 20))
  76. {
  77. spaceAbove = spaceBelow = 0;
  78. }
  79. int targetX, targetY;
  80. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  81. {
  82. targetX = rectangleToPointTo.getCentre().x;
  83. arrowTip.x = totalW / 2;
  84. if (spaceAbove >= spaceBelow)
  85. {
  86. // above
  87. targetY = rectangleToPointTo.getY();
  88. arrowTip.y = content.getBottom() + arrowLength;
  89. }
  90. else
  91. {
  92. // below
  93. targetY = rectangleToPointTo.getBottom();
  94. arrowTip.y = content.getY() - arrowLength;
  95. }
  96. }
  97. else
  98. {
  99. targetY = rectangleToPointTo.getCentre().y;
  100. arrowTip.y = totalH / 2;
  101. if (spaceLeft > spaceRight)
  102. {
  103. // on the left
  104. targetX = rectangleToPointTo.getX();
  105. arrowTip.x = content.getRight() + arrowLength;
  106. }
  107. else
  108. {
  109. // on the right
  110. targetX = rectangleToPointTo.getRight();
  111. arrowTip.x = content.getX() - arrowLength;
  112. }
  113. }
  114. setBounds (targetX - arrowTip.x, targetY - arrowTip.y, totalW, totalH);
  115. }