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.

189 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. millisecondsBeforeTipAppears (delayMs),
  20. mouseClicks (0),
  21. mouseWheelMoves (0),
  22. lastHideTime (0),
  23. lastComponentUnderMouse (nullptr),
  24. changedCompsSinceShown (true)
  25. {
  26. if (Desktop::getInstance().getMainMouseSource().canHover())
  27. startTimer (123);
  28. setAlwaysOnTop (true);
  29. setOpaque (true);
  30. if (parentComp != nullptr)
  31. parentComp->addChildComponent (this);
  32. }
  33. TooltipWindow::~TooltipWindow()
  34. {
  35. hide();
  36. }
  37. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  38. {
  39. millisecondsBeforeTipAppears = newTimeMs;
  40. }
  41. void TooltipWindow::paint (Graphics& g)
  42. {
  43. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  44. }
  45. void TooltipWindow::mouseEnter (const MouseEvent&)
  46. {
  47. hide();
  48. }
  49. void TooltipWindow::showFor (const String& tip)
  50. {
  51. jassert (tip.isNotEmpty());
  52. if (tipShowing != tip)
  53. repaint();
  54. tipShowing = tip;
  55. Point<int> mousePos (Desktop::getMousePosition());
  56. Rectangle<int> parentArea;
  57. Component* const parent = getParentComponent();
  58. if (parent != nullptr)
  59. {
  60. mousePos = parent->getLocalPoint (nullptr, mousePos);
  61. parentArea = parent->getLocalBounds();
  62. }
  63. else
  64. {
  65. parentArea = Desktop::getInstance().getDisplays().getDisplayContaining (mousePos).userArea;
  66. }
  67. int w, h;
  68. getLookAndFeel().getTooltipSize (tip, w, h);
  69. int x = mousePos.x;
  70. if (x > parentArea.getCentreX())
  71. x -= (w + 12);
  72. else
  73. x += 24;
  74. int y = mousePos.y;
  75. if (y > parentArea.getCentreY())
  76. y -= (h + 6);
  77. else
  78. y += 6;
  79. x = jlimit (parentArea.getX(), parentArea.getRight() - w, x);
  80. y = jlimit (parentArea.getY(), parentArea.getBottom() - h, y);
  81. setBounds (x, y, w, h);
  82. setVisible (true);
  83. if (parent == nullptr)
  84. addToDesktop (ComponentPeer::windowHasDropShadow
  85. | ComponentPeer::windowIsTemporary
  86. | ComponentPeer::windowIgnoresKeyPresses);
  87. toFront (false);
  88. }
  89. String TooltipWindow::getTipFor (Component* const c)
  90. {
  91. if (c != nullptr
  92. && Process::isForegroundProcess()
  93. && ! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  94. {
  95. if (TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c))
  96. if (! c->isCurrentlyBlockedByAnotherModalComponent())
  97. return ttc->getTooltip();
  98. }
  99. return String::empty;
  100. }
  101. void TooltipWindow::hide()
  102. {
  103. tipShowing = String::empty;
  104. removeFromDesktop();
  105. setVisible (false);
  106. }
  107. void TooltipWindow::timerCallback()
  108. {
  109. const unsigned int now = Time::getApproximateMillisecondCounter();
  110. Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  111. const String newTip (getTipFor (newComp));
  112. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  113. lastComponentUnderMouse = newComp;
  114. lastTipUnderMouse = newTip;
  115. Desktop& desktop = Desktop::getInstance();
  116. const int clickCount = desktop.getMouseButtonClickCounter();
  117. const int wheelCount = desktop.getMouseWheelMoveCounter();
  118. const bool mouseWasClicked = (clickCount > mouseClicks || wheelCount > mouseWheelMoves);
  119. mouseClicks = clickCount;
  120. mouseWheelMoves = wheelCount;
  121. const Point<int> mousePos (Desktop::getMousePosition());
  122. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  123. lastMousePos = mousePos;
  124. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  125. lastCompChangeTime = now;
  126. if (isVisible() || now < lastHideTime + 500)
  127. {
  128. // if a tip is currently visible (or has just disappeared), update to a new one
  129. // immediately if needed..
  130. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  131. {
  132. if (isVisible())
  133. {
  134. lastHideTime = now;
  135. hide();
  136. }
  137. }
  138. else if (tipChanged)
  139. {
  140. showFor (newTip);
  141. }
  142. }
  143. else
  144. {
  145. // if there isn't currently a tip, but one is needed, only let it
  146. // appear after a timeout..
  147. if (newTip.isNotEmpty()
  148. && newTip != tipShowing
  149. && now > lastCompChangeTime + (unsigned int) millisecondsBeforeTipAppears)
  150. {
  151. showFor (newTip);
  152. }
  153. }
  154. }