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.

190 lines
5.7KB

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