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.

143 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. A window that displays a pop-up tooltip when the mouse hovers over another component.
  18. To enable tooltips in your app, just create a single instance of a TooltipWindow
  19. object. Note that if you instantiate more than one instance of this class with the
  20. same parentComponent (even if both TooltipWindow's parentComponent is nil), you'll
  21. end up with multiple tooltips being shown! To avoid this use a SharedResourcePointer
  22. to instantiate the TooltipWindow only once.
  23. For audio plug-ins (which should not be opening native windows) it is better
  24. to add a TooltipWindow as a member variable to the editor and ensure that the
  25. editor is the parentComponent of your TooltipWindow. This will ensure that your
  26. TooltipWindow is scaled according to your editor and the DAWs scaling setting.
  27. The TooltipWindow object will then stay invisible, waiting until the mouse
  28. hovers for the specified length of time - it will then see if it's currently
  29. over a component which implements the TooltipClient interface, and if so,
  30. it will make itself visible to show the tooltip in the appropriate place.
  31. @see TooltipClient, SettableTooltipClient, SharedResourcePointer
  32. @tags{GUI}
  33. */
  34. class JUCE_API TooltipWindow : public Component,
  35. private Timer
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates a tooltip window.
  40. Make sure your app only creates one instance of this class, otherwise you'll
  41. get multiple overlaid tooltips appearing. The window will initially be invisible
  42. and will make itself visible when it needs to display a tip.
  43. To change the style of tooltips, see the LookAndFeel class for its tooltip
  44. methods.
  45. @param parentComponent if set to nullptr, the TooltipWindow will appear on the desktop,
  46. otherwise the tooltip will be added to the given parent
  47. component.
  48. @param millisecondsBeforeTipAppears the time for which the mouse has to stay still
  49. before a tooltip will be shown
  50. @see TooltipClient, LookAndFeel::drawTooltip, LookAndFeel::getTooltipBounds
  51. */
  52. explicit TooltipWindow (Component* parentComponent = nullptr,
  53. int millisecondsBeforeTipAppears = 700);
  54. /** Destructor. */
  55. ~TooltipWindow() override;
  56. //==============================================================================
  57. /** Changes the time before the tip appears.
  58. This lets you change the value that was set in the constructor.
  59. */
  60. void setMillisecondsBeforeTipAppears (int newTimeMs = 700) noexcept;
  61. /** Can be called to manually force a tip to be shown at a particular location. */
  62. void displayTip (Point<int> screenPosition, const String& text);
  63. /** Can be called to manually hide the tip if it's showing. */
  64. void hideTip();
  65. /** Asks a component for its tooltip.
  66. This can be overridden if you need custom lookup behaviour or to modify the strings.
  67. */
  68. virtual String getTipFor (Component&);
  69. //==============================================================================
  70. /** A set of colour IDs to use to change the colour of various aspects of the tooltip.
  71. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  72. methods.
  73. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  74. */
  75. enum ColourIds
  76. {
  77. backgroundColourId = 0x1001b00, /**< The colour to fill the background with. */
  78. textColourId = 0x1001c00, /**< The colour to use for the text. */
  79. outlineColourId = 0x1001c10 /**< The colour to use to draw an outline around the tooltip. */
  80. };
  81. //==============================================================================
  82. /** This abstract base class is implemented by LookAndFeel classes to provide
  83. window drawing functionality.
  84. */
  85. struct JUCE_API LookAndFeelMethods
  86. {
  87. virtual ~LookAndFeelMethods() = default;
  88. /** returns the bounds for a tooltip at the given screen coordinate, constrained within the given desktop area. */
  89. virtual Rectangle<int> getTooltipBounds (const String& tipText, Point<int> screenPos, Rectangle<int> parentArea) = 0;
  90. virtual void drawTooltip (Graphics&, const String& text, int width, int height) = 0;
  91. #if JUCE_CATCH_DEPRECATED_CODE_MISUSE
  92. // This method has been replaced by getTooltipBounds()
  93. virtual int getTooltipSize (const String&, int&, int&) { return 0; }
  94. #endif
  95. };
  96. private:
  97. //==============================================================================
  98. Point<float> lastMousePos;
  99. Component* lastComponentUnderMouse = nullptr;
  100. String tipShowing, lastTipUnderMouse;
  101. int millisecondsBeforeTipAppears;
  102. int mouseClicks = 0, mouseWheelMoves = 0;
  103. unsigned int lastCompChangeTime = 0, lastHideTime = 0;
  104. bool reentrant = false;
  105. void paint (Graphics&) override;
  106. void mouseEnter (const MouseEvent&) override;
  107. void timerCallback() override;
  108. void updatePosition (const String&, Point<int>, Rectangle<int>);
  109. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TooltipWindow)
  110. };
  111. } // namespace juce