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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 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. void displayTip (Point<int> screenPosition, const String& text);
  68. /** Can be called to manually hide the tip if it's showing. */
  69. void hideTip();
  70. /** Asks a component for its tooltip.
  71. This can be overridden if you need custom lookup behaviour or to modify the strings.
  72. */
  73. virtual String getTipFor (Component&);
  74. //==============================================================================
  75. /** A set of colour IDs to use to change the colour of various aspects of the tooltip.
  76. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  77. methods.
  78. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  79. */
  80. enum ColourIds
  81. {
  82. backgroundColourId = 0x1001b00, /**< The colour to fill the background with. */
  83. textColourId = 0x1001c00, /**< The colour to use for the text. */
  84. outlineColourId = 0x1001c10 /**< The colour to use to draw an outline around the tooltip. */
  85. };
  86. //==============================================================================
  87. /** This abstract base class is implemented by LookAndFeel classes to provide
  88. window drawing functionality.
  89. */
  90. struct JUCE_API LookAndFeelMethods
  91. {
  92. virtual ~LookAndFeelMethods() = default;
  93. /** returns the bounds for a tooltip at the given screen coordinate, constrained within the given desktop area. */
  94. virtual Rectangle<int> getTooltipBounds (const String& tipText, Point<int> screenPos, Rectangle<int> parentArea) = 0;
  95. virtual void drawTooltip (Graphics&, const String& text, int width, int height) = 0;
  96. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  97. // This method has been replaced by getTooltipBounds()
  98. virtual int getTooltipSize (const String&, int&, int&) { return 0; }
  99. #endif
  100. };
  101. private:
  102. //==============================================================================
  103. Point<float> lastMousePos;
  104. Component* lastComponentUnderMouse = nullptr;
  105. String tipShowing, lastTipUnderMouse;
  106. int millisecondsBeforeTipAppears;
  107. int mouseClicks = 0, mouseWheelMoves = 0;
  108. unsigned int lastCompChangeTime = 0, lastHideTime = 0;
  109. bool reentrant = false;
  110. void paint (Graphics&) override;
  111. void mouseEnter (const MouseEvent&) override;
  112. void timerCallback() override;
  113. void updatePosition (const String&, Point<int>, Rectangle<int>);
  114. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow)
  115. };
  116. } // namespace juce