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.

181 lines
7.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. auto content = std::make_unique<FoobarContentComp>();
  30. content->setSize (300, 300);
  31. auto& myBox = CallOutBox::launchAsynchronously (std::move (content),
  32. getScreenBounds(),
  33. nullptr);
  34. }
  35. @endcode
  36. The call-out will resize and position itself when the content changes size.
  37. @tags{GUI}
  38. */
  39. class JUCE_API CallOutBox : public Component,
  40. private Timer
  41. {
  42. public:
  43. //==============================================================================
  44. /** Creates a CallOutBox.
  45. @param contentComponent the component to display inside the call-out. This should
  46. already have a size set (although the call-out will also
  47. update itself when the component's size is changed later).
  48. Obviously this component must not be deleted until the
  49. call-out box has been deleted.
  50. @param areaToPointTo the area that the call-out's arrow should point towards. If
  51. a parentComponent is supplied, then this is relative to that
  52. parent; otherwise, it's a global screen coord.
  53. @param parentComponent if not a nullptr, this is the component to add the call-out to.
  54. If this is a nullptr, the call-out will be added to the desktop.
  55. */
  56. CallOutBox (Component& contentComponent,
  57. Rectangle<int> areaToPointTo,
  58. Component* parentComponent);
  59. //==============================================================================
  60. /** Changes the base width of the arrow. */
  61. void setArrowSize (float newSize);
  62. /** Updates the position and size of the box.
  63. You shouldn't normally need to call this, unless you need more precise control over the
  64. layout.
  65. @param newAreaToPointTo the rectangle to make the box's arrow point to
  66. @param newAreaToFitIn the area within which the box's position should be constrained
  67. */
  68. void updatePosition (const Rectangle<int>& newAreaToPointTo,
  69. const Rectangle<int>& newAreaToFitIn);
  70. /** This will launch a callout box containing the given content, pointing to the
  71. specified target component.
  72. This method will create and display a callout, returning immediately, after which
  73. the box will continue to run modally until the user clicks on some other component, at
  74. which point it will be dismissed and deleted automatically.
  75. It returns a reference to the newly-created box so that you can customise it, but don't
  76. keep a pointer to it, as it'll be deleted at some point when it gets closed.
  77. @param contentComponent the component to display inside the call-out. This should
  78. already have a size set (although the call-out will also
  79. update itself when the component's size is changed later).
  80. @param areaToPointTo the area that the call-out's arrow should point towards. If
  81. a parentComponent is supplied, then this is relative to that
  82. parent; otherwise, it's a global screen coord.
  83. @param parentComponent if not a nullptr, this is the component to add the call-out to.
  84. If this is a nullptr, the call-out will be added to the desktop.
  85. */
  86. static CallOutBox& launchAsynchronously (std::unique_ptr<Component> contentComponent,
  87. Rectangle<int> areaToPointTo,
  88. Component* parentComponent);
  89. /** Posts a message which will dismiss the callout box asynchronously.
  90. NB: it's safe to call this method from any thread.
  91. */
  92. void dismiss();
  93. /** Determines whether the mouse events for clicks outside the calloutbox are
  94. consumed, or allowed to arrive at the other component that they were aimed at.
  95. By default this is false, so that when you click on something outside the calloutbox,
  96. that event will also be sent to the component that was clicked on. If you set it to
  97. true, then the first click will always just dismiss the box and not be sent to
  98. anything else.
  99. */
  100. void setDismissalMouseClicksAreAlwaysConsumed (bool shouldAlwaysBeConsumed) noexcept;
  101. //==============================================================================
  102. /** This abstract base class is implemented by LookAndFeel classes. */
  103. struct JUCE_API LookAndFeelMethods
  104. {
  105. virtual ~LookAndFeelMethods() = default;
  106. virtual void drawCallOutBoxBackground (CallOutBox&, Graphics&, const Path&, Image&) = 0;
  107. virtual int getCallOutBoxBorderSize (const CallOutBox&) = 0;
  108. virtual float getCallOutBoxCornerSize (const CallOutBox&) = 0;
  109. };
  110. //==============================================================================
  111. /** @internal */
  112. void paint (Graphics&) override;
  113. /** @internal */
  114. void resized() override;
  115. /** @internal */
  116. void moved() override;
  117. /** @internal */
  118. void childBoundsChanged (Component*) override;
  119. /** @internal */
  120. bool hitTest (int x, int y) override;
  121. /** @internal */
  122. void inputAttemptWhenModal() override;
  123. /** @internal */
  124. bool keyPressed (const KeyPress&) override;
  125. /** @internal */
  126. void handleCommandMessage (int) override;
  127. /** @internal */
  128. int getBorderSize() const noexcept;
  129. /** @internal */
  130. void lookAndFeelChanged() override;
  131. private:
  132. //==============================================================================
  133. Component& content;
  134. Path outline;
  135. Point<float> targetPoint;
  136. Rectangle<int> availableArea, targetArea;
  137. Image background;
  138. float arrowSize = 16.0f;
  139. bool dismissalMouseClicksAreAlwaysConsumed = false;
  140. Time creationTime;
  141. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  142. void refreshPath();
  143. void timerCallback() override;
  144. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallOutBox)
  145. };
  146. } // namespace juce