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.

181 lines
5.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. TooltipWindow::TooltipWindow (Component* const parentComp, const int delayMs)
  18. : Component ("tooltip"),
  19. lastComponentUnderMouse (nullptr),
  20. millisecondsBeforeTipAppears (delayMs),
  21. mouseClicks (0), mouseWheelMoves (0),
  22. lastCompChangeTime (0), lastHideTime (0),
  23. reentrant (false)
  24. {
  25. if (Desktop::getInstance().getMainMouseSource().canHover())
  26. startTimer (123);
  27. setAlwaysOnTop (true);
  28. setOpaque (true);
  29. if (parentComp != nullptr)
  30. parentComp->addChildComponent (this);
  31. }
  32. TooltipWindow::~TooltipWindow()
  33. {
  34. hideTip();
  35. }
  36. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  37. {
  38. millisecondsBeforeTipAppears = newTimeMs;
  39. }
  40. void TooltipWindow::paint (Graphics& g)
  41. {
  42. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  43. }
  44. void TooltipWindow::mouseEnter (const MouseEvent&)
  45. {
  46. hideTip();
  47. }
  48. void TooltipWindow::updatePosition (const String& tip, Point<int> pos, Rectangle<int> parentArea)
  49. {
  50. setBounds (getLookAndFeel().getTooltipBounds (tip, pos, parentArea));
  51. setVisible (true);
  52. }
  53. void TooltipWindow::displayTip (Point<int> screenPos, const String& tip)
  54. {
  55. jassert (tip.isNotEmpty());
  56. if (! reentrant)
  57. {
  58. ScopedValueSetter<bool> setter (reentrant, true, false);
  59. if (tipShowing != tip)
  60. {
  61. tipShowing = tip;
  62. repaint();
  63. }
  64. if (Component* const parent = getParentComponent())
  65. {
  66. updatePosition (tip, parent->getLocalPoint (nullptr, screenPos),
  67. parent->getLocalBounds());
  68. }
  69. else
  70. {
  71. updatePosition (tip, screenPos, Desktop::getInstance().getDisplays()
  72. .getDisplayContaining (screenPos).userArea);
  73. addToDesktop (ComponentPeer::windowHasDropShadow
  74. | ComponentPeer::windowIsTemporary
  75. | ComponentPeer::windowIgnoresKeyPresses
  76. | ComponentPeer::windowIgnoresMouseClicks);
  77. }
  78. toFront (false);
  79. }
  80. }
  81. String TooltipWindow::getTipFor (Component* const c)
  82. {
  83. if (c != nullptr
  84. && Process::isForegroundProcess()
  85. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  86. {
  87. if (TooltipClient* const ttc = dynamic_cast<TooltipClient*> (c))
  88. if (! c->isCurrentlyBlockedByAnotherModalComponent())
  89. return ttc->getTooltip();
  90. }
  91. return String();
  92. }
  93. void TooltipWindow::hideTip()
  94. {
  95. if (! reentrant)
  96. {
  97. tipShowing.clear();
  98. removeFromDesktop();
  99. setVisible (false);
  100. }
  101. }
  102. void TooltipWindow::timerCallback()
  103. {
  104. Desktop& desktop = Desktop::getInstance();
  105. const MouseInputSource mouseSource (desktop.getMainMouseSource());
  106. const unsigned int now = Time::getApproximateMillisecondCounter();
  107. Component* const newComp = mouseSource.isMouse() ? mouseSource.getComponentUnderMouse() : nullptr;
  108. const String newTip (getTipFor (newComp));
  109. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  110. lastComponentUnderMouse = newComp;
  111. lastTipUnderMouse = newTip;
  112. const int clickCount = desktop.getMouseButtonClickCounter();
  113. const int wheelCount = desktop.getMouseWheelMoveCounter();
  114. const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  115. mouseClicks = clickCount;
  116. mouseWheelMoves = wheelCount;
  117. const Point<float> mousePos (mouseSource.getScreenPosition());
  118. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  119. lastMousePos = mousePos;
  120. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  121. lastCompChangeTime = now;
  122. if (isVisible() || now < lastHideTime + 500)
  123. {
  124. // if a tip is currently visible (or has just disappeared), update to a new one
  125. // immediately if needed..
  126. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  127. {
  128. if (isVisible())
  129. {
  130. lastHideTime = now;
  131. hideTip();
  132. }
  133. }
  134. else if (tipChanged)
  135. {
  136. displayTip (mousePos.roundToInt(), newTip);
  137. }
  138. }
  139. else
  140. {
  141. // if there isn't currently a tip, but one is needed, only let it
  142. // appear after a timeout..
  143. if (newTip.isNotEmpty()
  144. && newTip != tipShowing
  145. && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
  146. {
  147. displayTip (mousePos.roundToInt(), newTip);
  148. }
  149. }
  150. }