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 7.9KB

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