The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

185 lines
7.9KB

  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. #pragma once
  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. FoobarContentComp* content = new FoobarContentComp();
  35. content->setSize (300, 300);
  36. CallOutBox& myBox
  37. = CallOutBox::launchAsynchronously (content, getScreenBounds(), nullptr);
  38. }
  39. @endcode
  40. The call-out will resize and position itself when the content changes size.
  41. */
  42. class JUCE_API CallOutBox : public Component,
  43. private Timer
  44. {
  45. public:
  46. //==============================================================================
  47. /** Creates a CallOutBox.
  48. @param contentComponent the component to display inside the call-out. This should
  49. already have a size set (although the call-out will also
  50. update itself when the component's size is changed later).
  51. Obviously this component must not be deleted until the
  52. call-out box has been deleted.
  53. @param areaToPointTo the area that the call-out's arrow should point towards. If
  54. a parentComponent is supplied, then this is relative to that
  55. parent; otherwise, it's a global screen coord.
  56. @param parentComponent if not a nullptr, this is the component to add the call-out to.
  57. If this is a nullptr, the call-out will be added to the desktop.
  58. */
  59. CallOutBox (Component& contentComponent,
  60. const Rectangle<int>& areaToPointTo,
  61. Component* parentComponent);
  62. /** Destructor. */
  63. ~CallOutBox();
  64. //==============================================================================
  65. /** Changes the length 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. This component will be owned by the callout box and deleted
  86. later when the box is dismissed.
  87. @param areaToPointTo the area that the call-out's arrow should point towards. If
  88. a parentComponent is supplied, then this is relative to that
  89. parent; otherwise, it's a global screen coord.
  90. @param parentComponent if not a nullptr, this is the component to add the call-out to.
  91. If this is a nullptr, the call-out will be added to the desktop.
  92. */
  93. static CallOutBox& launchAsynchronously (Component* contentComponent,
  94. const Rectangle<int>& areaToPointTo,
  95. Component* parentComponent);
  96. /** Posts a message which will dismiss the callout box asynchronously.
  97. NB: it's safe to call this method from any thread.
  98. */
  99. void dismiss();
  100. /** Determines whether the mouse events for clicks outside the calloutbox are
  101. consumed, or allowed to arrive at the other component that they were aimed at.
  102. By default this is false, so that when you click on something outside the calloutbox,
  103. that event will also be sent to the component that was clicked on. If you set it to
  104. true, then the first click will always just dismiss the box and not be sent to
  105. anything else.
  106. */
  107. void setDismissalMouseClicksAreAlwaysConsumed (bool shouldAlwaysBeConsumed) noexcept;
  108. //==============================================================================
  109. /** This abstract base class is implemented by LookAndFeel classes. */
  110. struct JUCE_API LookAndFeelMethods
  111. {
  112. virtual ~LookAndFeelMethods() {}
  113. virtual void drawCallOutBoxBackground (CallOutBox&, Graphics&, const Path&, Image&) = 0;
  114. virtual int getCallOutBoxBorderSize (const CallOutBox&) = 0;
  115. };
  116. //==============================================================================
  117. /** @internal */
  118. void paint (Graphics&) override;
  119. /** @internal */
  120. void resized() override;
  121. /** @internal */
  122. void moved() override;
  123. /** @internal */
  124. void childBoundsChanged (Component*) override;
  125. /** @internal */
  126. bool hitTest (int x, int y) override;
  127. /** @internal */
  128. void inputAttemptWhenModal() override;
  129. /** @internal */
  130. bool keyPressed (const KeyPress&) override;
  131. /** @internal */
  132. void handleCommandMessage (int) override;
  133. /** @internal */
  134. int getBorderSize() const noexcept;
  135. private:
  136. //==============================================================================
  137. float arrowSize;
  138. Component& content;
  139. Path outline;
  140. Point<float> targetPoint;
  141. Rectangle<int> availableArea, targetArea;
  142. Image background;
  143. bool dismissalMouseClicksAreAlwaysConsumed;
  144. Time creationTime;
  145. void refreshPath();
  146. void timerCallback() override;
  147. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallOutBox)
  148. };