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.

juce_CallOutBox.h 8.0KB

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