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.

182 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. A component for showing a message or other graphics inside a speech-bubble-shaped
  21. outline, pointing at a location on the screen.
  22. This is a base class that just draws and positions the bubble shape, but leaves
  23. the drawing of any content up to a subclass. See BubbleMessageComponent for a subclass
  24. that draws a text message.
  25. To use it, create your subclass, then either add it to a parent component or
  26. put it on the desktop with addToDesktop (0), use setPosition() to
  27. resize and position it, then make it visible.
  28. @see BubbleMessageComponent
  29. */
  30. class JUCE_API BubbleComponent : public Component
  31. {
  32. protected:
  33. //==============================================================================
  34. /** Creates a BubbleComponent.
  35. Your subclass will need to implement the getContentSize() and paintContent()
  36. methods to draw the bubble's contents.
  37. */
  38. BubbleComponent();
  39. public:
  40. /** Destructor. */
  41. ~BubbleComponent();
  42. //==============================================================================
  43. /** A list of permitted placements for the bubble, relative to the coordinates
  44. at which it should be pointing.
  45. @see setAllowedPlacement
  46. */
  47. enum BubblePlacement
  48. {
  49. above = 1,
  50. below = 2,
  51. left = 4,
  52. right = 8
  53. };
  54. /** Tells the bubble which positions it's allowed to put itself in, relative to the
  55. point at which it's pointing.
  56. By default when setPosition() is called, the bubble will place itself either
  57. above, below, left, or right of the target area. You can pass in a bitwise-'or' of
  58. the values in BubblePlacement to restrict this choice.
  59. E.g. if you only want your bubble to appear above or below the target area,
  60. use setAllowedPlacement (above | below);
  61. @see BubblePlacement
  62. */
  63. void setAllowedPlacement (int newPlacement);
  64. //==============================================================================
  65. /** Moves and resizes the bubble to point at a given component.
  66. This will resize the bubble to fit its content, then find a position for it
  67. so that it's next to, but doesn't overlap the given component.
  68. It'll put itself either above, below, or to the side of the component depending
  69. on where there's the most space, honouring any restrictions that were set
  70. with setAllowedPlacement().
  71. */
  72. void setPosition (Component* componentToPointTo,
  73. int distanceFromTarget = 15, int arrowLength = 10);
  74. /** Moves and resizes the bubble to point at a given point.
  75. This will resize the bubble to fit its content, then position it
  76. so that the tip of the bubble points to the given coordinate. The coordinates
  77. are relative to either the bubble component's parent component if it has one, or
  78. they are screen coordinates if not.
  79. It'll put itself either above, below, or to the side of this point, depending
  80. on where there's the most space, honouring any restrictions that were set
  81. with setAllowedPlacement().
  82. */
  83. void setPosition (Point<int> arrowTipPosition, int arrowLength = 10);
  84. /** Moves and resizes the bubble to point at a given rectangle.
  85. This will resize the bubble to fit its content, then find a position for it
  86. so that it's next to, but doesn't overlap the given rectangle. The rectangle's
  87. coordinates are relative to either the bubble component's parent component
  88. if it has one, or they are screen coordinates if not.
  89. It'll put itself either above, below, or to the side of the component depending
  90. on where there's the most space, honouring any restrictions that were set
  91. with setAllowedPlacement().
  92. distanceFromTarget is the amount of space to leave between the bubble and the
  93. target rectangle, and arrowLength is the length of the arrow that it will draw.
  94. */
  95. void setPosition (Rectangle<int> rectangleToPointTo,
  96. int distanceFromTarget = 15, int arrowLength = 10);
  97. //==============================================================================
  98. /** A set of colour IDs to use to change the colour of various aspects of the bubble component.
  99. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  100. methods.
  101. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  102. */
  103. enum ColourIds
  104. {
  105. backgroundColourId = 0x1000af0, /**< A background colour to fill the bubble with. */
  106. outlineColourId = 0x1000af1 /**< The colour to use for an outline around the bubble. */
  107. };
  108. //==============================================================================
  109. /** This abstract base class is implemented by LookAndFeel classes.
  110. */
  111. struct JUCE_API LookAndFeelMethods
  112. {
  113. virtual ~LookAndFeelMethods() {}
  114. virtual void drawBubble (Graphics&, BubbleComponent&,
  115. const Point<float>& positionOfTip,
  116. const Rectangle<float>& body) = 0;
  117. };
  118. protected:
  119. //==============================================================================
  120. /** Subclasses should override this to return the size of the content they
  121. want to draw inside the bubble.
  122. */
  123. virtual void getContentSize (int& width, int& height) = 0;
  124. /** Subclasses should override this to draw their bubble's contents.
  125. The graphics object's clip region and the dimensions passed in here are
  126. set up to paint just the rectangle inside the bubble.
  127. */
  128. virtual void paintContent (Graphics& g, int width, int height) = 0;
  129. public:
  130. /** @internal */
  131. void paint (Graphics&) override;
  132. private:
  133. Rectangle<int> content;
  134. Point<int> arrowTip;
  135. int allowablePlacements;
  136. DropShadowEffect shadow;
  137. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (BubbleComponent)
  138. };