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.

146 lines
5.4KB

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