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.

196 lines
6.3KB

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