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_TooltipWindow.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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 window that displays a pop-up tooltip when the mouse hovers over another component.
  23. To enable tooltips in your app, just create a single instance of a TooltipWindow
  24. object. Note that if you instantiate more than one instance of this class with the
  25. same parentComponent (even if both TooltipWindow's parentComponent is nil), you'll
  26. end up with multiple tooltips being shown! To avoid this use a SharedResourcePointer
  27. to instantiate the TooltipWindow only once.
  28. For audio plug-ins (which should not be opening native windows) it is better
  29. to add a TooltipWindow as a member variable to the editor and ensure that the
  30. editor is the parentComponent of your TooltipWindow. This will ensure that your
  31. TooltipWindow is scaled according to your editor and the DAWs scaling setting.
  32. The TooltipWindow object will then stay invisible, waiting until the mouse
  33. hovers for the specified length of time - it will then see if it's currently
  34. over a component which implements the TooltipClient interface, and if so,
  35. it will make itself visible to show the tooltip in the appropriate place.
  36. @see TooltipClient, SettableTooltipClient, SharedResourcePointer
  37. @tags{GUI}
  38. */
  39. class JUCE_API TooltipWindow : public Component,
  40. private Timer
  41. {
  42. public:
  43. //==============================================================================
  44. /** Creates a tooltip window.
  45. Make sure your app only creates one instance of this class, otherwise you'll
  46. get multiple overlaid tooltips appearing. The window will initially be invisible
  47. and will make itself visible when it needs to display a tip.
  48. To change the style of tooltips, see the LookAndFeel class for its tooltip
  49. methods.
  50. @param parentComponent if set to nullptr, the TooltipWindow will appear on the desktop,
  51. otherwise the tooltip will be added to the given parent
  52. component.
  53. @param millisecondsBeforeTipAppears the time for which the mouse has to stay still
  54. before a tooltip will be shown
  55. @see TooltipClient, LookAndFeel::drawTooltip, LookAndFeel::getTooltipBounds
  56. */
  57. explicit TooltipWindow (Component* parentComponent = nullptr,
  58. int millisecondsBeforeTipAppears = 700);
  59. /** Destructor. */
  60. ~TooltipWindow() override;
  61. //==============================================================================
  62. /** Changes the time before the tip appears.
  63. This lets you change the value that was set in the constructor.
  64. */
  65. void setMillisecondsBeforeTipAppears (int newTimeMs = 700) noexcept;
  66. /** Can be called to manually force a tip to be shown at a particular location.
  67. The tip will be shown until hideTip() is called, or a dismissal mouse event
  68. occurs.
  69. @see hideTip
  70. */
  71. void displayTip (Point<int> screenPosition, const String& text);
  72. /** Can be called to manually hide the tip if it's showing. */
  73. void hideTip();
  74. /** Asks a component for its tooltip.
  75. This can be overridden if you need custom lookup behaviour or to modify the strings.
  76. */
  77. virtual String getTipFor (Component&);
  78. //==============================================================================
  79. /** A set of colour IDs to use to change the colour of various aspects of the tooltip.
  80. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  81. methods.
  82. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  83. */
  84. enum ColourIds
  85. {
  86. backgroundColourId = 0x1001b00, /**< The colour to fill the background with. */
  87. textColourId = 0x1001c00, /**< The colour to use for the text. */
  88. outlineColourId = 0x1001c10 /**< The colour to use to draw an outline around the tooltip. */
  89. };
  90. //==============================================================================
  91. /** This abstract base class is implemented by LookAndFeel classes to provide
  92. window drawing functionality.
  93. */
  94. struct JUCE_API LookAndFeelMethods
  95. {
  96. virtual ~LookAndFeelMethods() = default;
  97. /** returns the bounds for a tooltip at the given screen coordinate, constrained within the given desktop area. */
  98. virtual Rectangle<int> getTooltipBounds (const String& tipText, Point<int> screenPos, Rectangle<int> parentArea) = 0;
  99. virtual void drawTooltip (Graphics&, const String& text, int width, int height) = 0;
  100. };
  101. //==============================================================================
  102. /** @internal */
  103. float getDesktopScaleFactor() const override;
  104. private:
  105. //==============================================================================
  106. Point<float> lastMousePos;
  107. SafePointer<Component> lastComponentUnderMouse;
  108. String tipShowing, lastTipUnderMouse, manuallyShownTip;
  109. int millisecondsBeforeTipAppears;
  110. unsigned int lastCompChangeTime = 0, lastHideTime = 0;
  111. bool reentrant = false, dismissalMouseEventOccurred = false;
  112. enum ShownManually { yes, no };
  113. void displayTipInternal (Point<int>, const String&, ShownManually);
  114. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override;
  115. void paint (Graphics&) override;
  116. void mouseEnter (const MouseEvent&) override;
  117. void mouseDown (const MouseEvent&) override;
  118. void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
  119. void timerCallback() override;
  120. void updatePosition (const String&, Point<int>, Rectangle<int>);
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow)
  122. };
  123. } // namespace juce