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.

188 lines
5.6KB

  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 parent_,
  19. const int millisecondsBeforeTipAppears_)
  20. : Component ("tooltip"),
  21. millisecondsBeforeTipAppears (millisecondsBeforeTipAppears_),
  22. mouseClicks (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 (parent_ != nullptr)
  32. parent_->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. && ! Component::isMouseButtonDownAnywhere())
  95. {
  96. TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c);
  97. if (ttc != nullptr && ! 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. const int clickCount = Desktop::getInstance().getMouseButtonClickCounter();
  117. const bool mouseWasClicked = clickCount > mouseClicks;
  118. mouseClicks = clickCount;
  119. const Point<int> mousePos (Desktop::getMousePosition());
  120. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  121. lastMousePos = mousePos;
  122. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  123. lastCompChangeTime = now;
  124. if (isVisible() || now < lastHideTime + 500)
  125. {
  126. // if a tip is currently visible (or has just disappeared), update to a new one
  127. // immediately if needed..
  128. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  129. {
  130. if (isVisible())
  131. {
  132. lastHideTime = now;
  133. hide();
  134. }
  135. }
  136. else if (tipChanged)
  137. {
  138. showFor (newTip);
  139. }
  140. }
  141. else
  142. {
  143. // if there isn't currently a tip, but one is needed, only let it
  144. // appear after a timeout..
  145. if (newTip.isNotEmpty()
  146. && newTip != tipShowing
  147. && now > lastCompChangeTime + millisecondsBeforeTipAppears)
  148. {
  149. showFor (newTip);
  150. }
  151. }
  152. }