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.

191 lines
6.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. : side (0),
  20. allowablePlacements (above | below | left | right),
  21. arrowTipX (0.0f),
  22. arrowTipY (0.0f)
  23. {
  24. setInterceptsMouseClicks (false, false);
  25. shadow.setShadowProperties (5.0f, 0.35f, 0, 0);
  26. setComponentEffect (&shadow);
  27. }
  28. BubbleComponent::~BubbleComponent()
  29. {
  30. }
  31. //==============================================================================
  32. void BubbleComponent::paint (Graphics& g)
  33. {
  34. int x = content.getX();
  35. int y = content.getY();
  36. int w = content.getWidth();
  37. int h = content.getHeight();
  38. int cw, ch;
  39. getContentSize (cw, ch);
  40. if (side == 3)
  41. x += w - cw;
  42. else if (side != 1)
  43. x += (w - cw) / 2;
  44. w = cw;
  45. if (side == 2)
  46. y += h - ch;
  47. else if (side != 0)
  48. y += (h - ch) / 2;
  49. h = ch;
  50. getLookAndFeel().drawBubble (g, arrowTipX, arrowTipY,
  51. (float) x, (float) y,
  52. (float) w, (float) h);
  53. const int cx = x + (w - cw) / 2;
  54. const int cy = y + (h - ch) / 2;
  55. const int indent = 3;
  56. g.setOrigin (cx + indent, cy + indent);
  57. g.reduceClipRegion (0, 0, cw - indent * 2, ch - indent * 2);
  58. paintContent (g, cw - indent * 2, ch - indent * 2);
  59. }
  60. //==============================================================================
  61. void BubbleComponent::setAllowedPlacement (const int newPlacement)
  62. {
  63. allowablePlacements = newPlacement;
  64. }
  65. void BubbleComponent::setPosition (Component* componentToPointTo)
  66. {
  67. jassert (componentToPointTo != nullptr);
  68. Point<int> pos;
  69. if (getParentComponent() != nullptr)
  70. pos = getParentComponent()->getLocalPoint (componentToPointTo, pos);
  71. else
  72. pos = componentToPointTo->localPointToGlobal (pos);
  73. setPosition (Rectangle<int> (pos.x, pos.y, componentToPointTo->getWidth(), componentToPointTo->getHeight()));
  74. }
  75. void BubbleComponent::setPosition (const int arrowTipX_,
  76. const int arrowTipY_)
  77. {
  78. setPosition (Rectangle<int> (arrowTipX_, arrowTipY_, 1, 1));
  79. }
  80. //==============================================================================
  81. void BubbleComponent::setPosition (const Rectangle<int>& rectangleToPointTo)
  82. {
  83. Rectangle<int> availableSpace (getParentComponent() != nullptr ? getParentComponent()->getLocalBounds()
  84. : getParentMonitorArea());
  85. int x = 0;
  86. int y = 0;
  87. int w = 150;
  88. int h = 30;
  89. getContentSize (w, h);
  90. w += 30;
  91. h += 30;
  92. const float edgeIndent = 2.0f;
  93. const int arrowLength = jmin (10, h / 3, w / 3);
  94. int spaceAbove = ((allowablePlacements & above) != 0) ? jmax (0, rectangleToPointTo.getY() - availableSpace.getY()) : -1;
  95. int spaceBelow = ((allowablePlacements & below) != 0) ? jmax (0, availableSpace.getBottom() - rectangleToPointTo.getBottom()) : -1;
  96. int spaceLeft = ((allowablePlacements & left) != 0) ? jmax (0, rectangleToPointTo.getX() - availableSpace.getX()) : -1;
  97. int spaceRight = ((allowablePlacements & right) != 0) ? jmax (0, availableSpace.getRight() - rectangleToPointTo.getRight()) : -1;
  98. // look at whether the component is elongated, and if so, try to position next to its longer dimension.
  99. if (rectangleToPointTo.getWidth() > rectangleToPointTo.getHeight() * 2
  100. && (spaceAbove > h + 20 || spaceBelow > h + 20))
  101. {
  102. spaceLeft = spaceRight = 0;
  103. }
  104. else if (rectangleToPointTo.getWidth() < rectangleToPointTo.getHeight() / 2
  105. && (spaceLeft > w + 20 || spaceRight > w + 20))
  106. {
  107. spaceAbove = spaceBelow = 0;
  108. }
  109. if (jmax (spaceAbove, spaceBelow) >= jmax (spaceLeft, spaceRight))
  110. {
  111. x = rectangleToPointTo.getX() + (rectangleToPointTo.getWidth() - w) / 2;
  112. arrowTipX = w * 0.5f;
  113. content.setSize (w, h - arrowLength);
  114. if (spaceAbove >= spaceBelow)
  115. {
  116. // above
  117. y = rectangleToPointTo.getY() - h;
  118. content.setPosition (0, 0);
  119. arrowTipY = h - edgeIndent;
  120. side = 2;
  121. }
  122. else
  123. {
  124. // below
  125. y = rectangleToPointTo.getBottom();
  126. content.setPosition (0, arrowLength);
  127. arrowTipY = edgeIndent;
  128. side = 0;
  129. }
  130. }
  131. else
  132. {
  133. y = rectangleToPointTo.getY() + (rectangleToPointTo.getHeight() - h) / 2;
  134. arrowTipY = h * 0.5f;
  135. content.setSize (w - arrowLength, h);
  136. if (spaceLeft > spaceRight)
  137. {
  138. // on the left
  139. x = rectangleToPointTo.getX() - w;
  140. content.setPosition (0, 0);
  141. arrowTipX = w - edgeIndent;
  142. side = 3;
  143. }
  144. else
  145. {
  146. // on the right
  147. x = rectangleToPointTo.getRight();
  148. content.setPosition (arrowLength, 0);
  149. arrowTipX = edgeIndent;
  150. side = 1;
  151. }
  152. }
  153. setBounds (x, y, w, h);
  154. }