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.

182 lines
7.8KB

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