Audio plugin host https://kx.studio/carla
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.

183 lines
7.9KB

  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. #ifndef JUCE_CALLOUTBOX_H_INCLUDED
  18. #define JUCE_CALLOUTBOX_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A box with a small arrow that can be used as a temporary pop-up window to show
  22. extra controls when a button or other component is clicked.
  23. Using one of these is similar to having a popup menu attached to a button or
  24. other component - but it looks fancier, and has an arrow that can indicate the
  25. object that it applies to.
  26. The class works best when shown modally, but obviously running modal loops is
  27. evil and must never be done, so the launchAsynchronously method is provided as
  28. a handy way of launching an instance of a CallOutBox and automatically managing
  29. its lifetime, e.g.
  30. @code
  31. void mouseUp (const MouseEvent&)
  32. {
  33. FoobarContentComp* content = new FoobarContentComp();
  34. content->setSize (300, 300);
  35. CallOutBox& myBox
  36. = CallOutBox::launchAsynchronously (content, getScreenBounds(), nullptr);
  37. }
  38. @endcode
  39. The call-out will resize and position itself when the content changes size.
  40. */
  41. class JUCE_API CallOutBox : public Component
  42. {
  43. public:
  44. //==============================================================================
  45. /** Creates a CallOutBox.
  46. @param contentComponent the component to display inside the call-out. This should
  47. already have a size set (although the call-out will also
  48. update itself when the component's size is changed later).
  49. Obviously this component must not be deleted until the
  50. call-out box has been deleted.
  51. @param areaToPointTo the area that the call-out's arrow should point towards. If
  52. a parentComponent is supplied, then this is relative to that
  53. parent; otherwise, it's a global screen coord.
  54. @param parentComponent if non-zero, this is the component to add the call-out to. If
  55. this is a nullptr, the call-out will be added to the desktop.
  56. */
  57. CallOutBox (Component& contentComponent,
  58. const Rectangle<int>& areaToPointTo,
  59. Component* parentComponent);
  60. /** Destructor. */
  61. ~CallOutBox();
  62. //==============================================================================
  63. /** Changes the length of the arrow. */
  64. void setArrowSize (float newSize);
  65. /** Updates the position and size of the box.
  66. You shouldn't normally need to call this, unless you need more precise control over the
  67. layout.
  68. @param newAreaToPointTo the rectangle to make the box's arrow point to
  69. @param newAreaToFitIn the area within which the box's position should be constrained
  70. */
  71. void updatePosition (const Rectangle<int>& newAreaToPointTo,
  72. const Rectangle<int>& newAreaToFitIn);
  73. /** This will launch a callout box containing the given content, pointing to the
  74. specified target component.
  75. This method will create and display a callout, returning immediately, after which
  76. the box will continue to run modally until the user clicks on some other component, at
  77. which point it will be dismissed and deleted automatically.
  78. It returns a reference to the newly-created box so that you can customise it, but don't
  79. keep a pointer to it, as it'll be deleted at some point when it gets closed.
  80. @param contentComponent the component to display inside the call-out. This should
  81. already have a size set (although the call-out will also
  82. update itself when the component's size is changed later).
  83. This component will be owned by the callout box and deleted
  84. later when the box is dismissed.
  85. @param areaToPointTo the area that the call-out's arrow should point towards. If
  86. a parentComponent is supplied, then this is relative to that
  87. parent; otherwise, it's a global screen coord.
  88. @param parentComponent if non-zero, this is the component to add the call-out to. If
  89. this is a nullptr, the call-out will be added to the desktop.
  90. */
  91. static CallOutBox& launchAsynchronously (Component* contentComponent,
  92. const Rectangle<int>& areaToPointTo,
  93. Component* parentComponent);
  94. /** Posts a message which will dismiss the callout box asynchronously.
  95. NB: it's safe to call this method from any thread.
  96. */
  97. void dismiss();
  98. /** Determines whether the mouse events for clicks outside the calloutbox are
  99. consumed, or allowed to arrive at the other component that they were aimed at.
  100. By default this is false, so that when you click on something outside the calloutbox,
  101. that event will also be sent to the component that was clicked on. If you set it to
  102. true, then the first click will always just dismiss the box and not be sent to
  103. anything else.
  104. */
  105. void setDismissalMouseClicksAreAlwaysConsumed (bool shouldAlwaysBeConsumed) noexcept;
  106. //==============================================================================
  107. /** This abstract base class is implemented by LookAndFeel classes. */
  108. struct JUCE_API LookAndFeelMethods
  109. {
  110. virtual ~LookAndFeelMethods() {}
  111. virtual void drawCallOutBoxBackground (CallOutBox&, Graphics&, const Path&, Image&) = 0;
  112. virtual int getCallOutBoxBorderSize (const CallOutBox&) = 0;
  113. };
  114. //==============================================================================
  115. /** @internal */
  116. void paint (Graphics&) override;
  117. /** @internal */
  118. void resized() override;
  119. /** @internal */
  120. void moved() override;
  121. /** @internal */
  122. void childBoundsChanged (Component*) override;
  123. /** @internal */
  124. bool hitTest (int x, int y) override;
  125. /** @internal */
  126. void inputAttemptWhenModal() override;
  127. /** @internal */
  128. bool keyPressed (const KeyPress&) override;
  129. /** @internal */
  130. void handleCommandMessage (int) override;
  131. /** @internal */
  132. int getBorderSize() const noexcept;
  133. private:
  134. //==============================================================================
  135. float arrowSize;
  136. Component& content;
  137. Path outline;
  138. Point<float> targetPoint;
  139. Rectangle<int> availableArea, targetArea;
  140. Image background;
  141. bool dismissalMouseClicksAreAlwaysConsumed;
  142. void refreshPath();
  143. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallOutBox)
  144. };
  145. #endif // JUCE_CALLOUTBOX_H_INCLUDED