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.

194 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. BEGIN_JUCE_NAMESPACE
  19. //==============================================================================
  20. TooltipWindow::TooltipWindow (Component* const parent_,
  21. const int millisecondsBeforeTipAppears_)
  22. : Component ("tooltip"),
  23. millisecondsBeforeTipAppears (millisecondsBeforeTipAppears_),
  24. mouseClicks (0),
  25. lastHideTime (0),
  26. lastComponentUnderMouse (nullptr),
  27. changedCompsSinceShown (true)
  28. {
  29. if (Desktop::getInstance().getMainMouseSource().canHover())
  30. startTimer (123);
  31. setAlwaysOnTop (true);
  32. setOpaque (true);
  33. if (parent_ != nullptr)
  34. parent_->addChildComponent (this);
  35. }
  36. TooltipWindow::~TooltipWindow()
  37. {
  38. hide();
  39. }
  40. void TooltipWindow::setMillisecondsBeforeTipAppears (const int newTimeMs) noexcept
  41. {
  42. millisecondsBeforeTipAppears = newTimeMs;
  43. }
  44. void TooltipWindow::paint (Graphics& g)
  45. {
  46. getLookAndFeel().drawTooltip (g, tipShowing, getWidth(), getHeight());
  47. }
  48. void TooltipWindow::mouseEnter (const MouseEvent&)
  49. {
  50. hide();
  51. }
  52. void TooltipWindow::showFor (const String& tip)
  53. {
  54. jassert (tip.isNotEmpty());
  55. if (tipShowing != tip)
  56. repaint();
  57. tipShowing = tip;
  58. Point<int> mousePos (Desktop::getMousePosition());
  59. Rectangle<int> parentArea;
  60. if (getParentComponent() != nullptr)
  61. {
  62. mousePos = getParentComponent()->getLocalPoint (nullptr, mousePos);
  63. parentArea = getParentComponent()->getLocalBounds();
  64. }
  65. else
  66. {
  67. parentArea = Desktop::getInstance().getMonitorAreaContaining (mousePos);
  68. }
  69. int w, h;
  70. getLookAndFeel().getTooltipSize (tip, w, h);
  71. int x = mousePos.getX();
  72. if (x > parentArea.getCentreX())
  73. x -= (w + 12);
  74. else
  75. x += 24;
  76. int y = mousePos.getY();
  77. if (y > parentArea.getCentreY())
  78. y -= (h + 6);
  79. else
  80. y += 6;
  81. x = jlimit (parentArea.getX(), parentArea.getRight() - w, x);
  82. y = jlimit (parentArea.getY(), parentArea.getBottom() - h, y);
  83. setBounds (x, y, w, h);
  84. setVisible (true);
  85. if (getParentComponent() == nullptr)
  86. {
  87. addToDesktop (ComponentPeer::windowHasDropShadow
  88. | ComponentPeer::windowIsTemporary
  89. | ComponentPeer::windowIgnoresKeyPresses);
  90. }
  91. toFront (false);
  92. }
  93. String TooltipWindow::getTipFor (Component* const c)
  94. {
  95. if (c != nullptr
  96. && Process::isForegroundProcess()
  97. && ! Component::isMouseButtonDownAnywhere())
  98. {
  99. TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c);
  100. if (ttc != nullptr && ! c->isCurrentlyBlockedByAnotherModalComponent())
  101. return ttc->getTooltip();
  102. }
  103. return String::empty;
  104. }
  105. void TooltipWindow::hide()
  106. {
  107. tipShowing = String::empty;
  108. removeFromDesktop();
  109. setVisible (false);
  110. }
  111. void TooltipWindow::timerCallback()
  112. {
  113. const unsigned int now = Time::getApproximateMillisecondCounter();
  114. Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  115. const String newTip (getTipFor (newComp));
  116. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  117. lastComponentUnderMouse = newComp;
  118. lastTipUnderMouse = newTip;
  119. const int clickCount = Desktop::getInstance().getMouseButtonClickCounter();
  120. const bool mouseWasClicked = clickCount > mouseClicks;
  121. mouseClicks = clickCount;
  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 + millisecondsBeforeTipAppears)
  151. {
  152. showFor (newTip);
  153. }
  154. }
  155. }
  156. END_JUCE_NAMESPACE