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.

120 lines
4.2KB

  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. BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
  19. : fadeOutLength (fadeOutLengthMs),
  20. deleteAfterUse (false)
  21. {
  22. }
  23. BubbleMessageComponent::~BubbleMessageComponent()
  24. {
  25. }
  26. void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
  27. const AttributedString& text,
  28. const int numMillisecondsBeforeRemoving,
  29. const bool removeWhenMouseClicked,
  30. const bool deleteSelfAfterUse)
  31. {
  32. createLayout (text);
  33. setPosition (pos);
  34. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  35. }
  36. void BubbleMessageComponent::showAt (Component* const component,
  37. const AttributedString& text,
  38. const int numMillisecondsBeforeRemoving,
  39. const bool removeWhenMouseClicked,
  40. const bool deleteSelfAfterUse)
  41. {
  42. createLayout (text);
  43. setPosition (component);
  44. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  45. }
  46. void BubbleMessageComponent::createLayout (const AttributedString& text)
  47. {
  48. textLayout.createLayoutWithBalancedLineLengths (text, 256);
  49. }
  50. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  51. const bool removeWhenMouseClicked,
  52. const bool deleteSelfAfterUse)
  53. {
  54. setAlpha (1.0f);
  55. setVisible (true);
  56. deleteAfterUse = deleteSelfAfterUse;
  57. expiryTime = numMillisecondsBeforeRemoving > 0
  58. ? (Time::getMillisecondCounter() + numMillisecondsBeforeRemoving) : 0;
  59. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  60. if (! (removeWhenMouseClicked && isShowing()))
  61. mouseClickCounter += 0xfffff;
  62. startTimer (77);
  63. repaint();
  64. }
  65. const float bubblePaddingX = 20.0f;
  66. const float bubblePaddingY = 14.0f;
  67. void BubbleMessageComponent::getContentSize (int& w, int& h)
  68. {
  69. w = (int) (bubblePaddingX + textLayout.getWidth());
  70. h = (int) (bubblePaddingY + textLayout.getHeight());
  71. }
  72. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  73. {
  74. g.setColour (findColour (TooltipWindow::textColourId));
  75. textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
  76. w - bubblePaddingX, h - bubblePaddingY));
  77. }
  78. void BubbleMessageComponent::timerCallback()
  79. {
  80. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  81. hide (false);
  82. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  83. hide (true);
  84. }
  85. void BubbleMessageComponent::hide (const bool fadeOut)
  86. {
  87. stopTimer();
  88. if (fadeOut)
  89. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  90. else
  91. setVisible (false);
  92. if (deleteAfterUse)
  93. delete this;
  94. }