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.

148 lines
5.5KB

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