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.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. if (getParentComponent() != nullptr)
  59. {
  60. mousePos = getParentComponent()->getLocalPoint (nullptr, mousePos);
  61. parentArea = getParentComponent()->getLocalBounds();
  62. }
  63. else
  64. {
  65. parentArea = Desktop::getInstance().getMonitorAreaContaining (mousePos);
  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 (getParentComponent() == nullptr)
  84. {
  85. addToDesktop (ComponentPeer::windowHasDropShadow
  86. | ComponentPeer::windowIsTemporary
  87. | ComponentPeer::windowIgnoresKeyPresses);
  88. }
  89. toFront (false);
  90. }
  91. String TooltipWindow::getTipFor (Component* const c)
  92. {
  93. if (c != nullptr
  94. && Process::isForegroundProcess()
  95. && ! Component::isMouseButtonDownAnywhere())
  96. {
  97. TooltipClient* const ttc = dynamic_cast <TooltipClient*> (c);
  98. if (ttc != nullptr && ! c->isCurrentlyBlockedByAnotherModalComponent())
  99. return ttc->getTooltip();
  100. }
  101. return String::empty;
  102. }
  103. void TooltipWindow::hide()
  104. {
  105. tipShowing = String::empty;
  106. removeFromDesktop();
  107. setVisible (false);
  108. }
  109. void TooltipWindow::timerCallback()
  110. {
  111. const unsigned int now = Time::getApproximateMillisecondCounter();
  112. Component* const newComp = Desktop::getInstance().getMainMouseSource().getComponentUnderMouse();
  113. const String newTip (getTipFor (newComp));
  114. const bool tipChanged = (newTip != lastTipUnderMouse || newComp != lastComponentUnderMouse);
  115. lastComponentUnderMouse = newComp;
  116. lastTipUnderMouse = newTip;
  117. const int clickCount = Desktop::getInstance().getMouseButtonClickCounter();
  118. const bool mouseWasClicked = clickCount > mouseClicks;
  119. mouseClicks = clickCount;
  120. const Point<int> mousePos (Desktop::getMousePosition());
  121. const bool mouseMovedQuickly = mousePos.getDistanceFrom (lastMousePos) > 12;
  122. lastMousePos = mousePos;
  123. if (tipChanged || mouseWasClicked || mouseMovedQuickly)
  124. lastCompChangeTime = now;
  125. if (isVisible() || now < lastHideTime + 500)
  126. {
  127. // if a tip is currently visible (or has just disappeared), update to a new one
  128. // immediately if needed..
  129. if (newComp == nullptr || mouseWasClicked || newTip.isEmpty())
  130. {
  131. if (isVisible())
  132. {
  133. lastHideTime = now;
  134. hide();
  135. }
  136. }
  137. else if (tipChanged)
  138. {
  139. showFor (newTip);
  140. }
  141. }
  142. else
  143. {
  144. // if there isn't currently a tip, but one is needed, only let it
  145. // appear after a timeout..
  146. if (newTip.isNotEmpty()
  147. && newTip != tipShowing
  148. && now > lastCompChangeTime + millisecondsBeforeTipAppears)
  149. {
  150. showFor (newTip);
  151. }
  152. }
  153. }