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.0KB

  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. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  26. }
  27. void BubbleMessageComponent::showAt (int x, int y,
  28. const AttributedString& text,
  29. const int numMillisecondsBeforeRemoving,
  30. const bool removeWhenMouseClicked,
  31. const bool deleteSelfAfterUse)
  32. {
  33. createLayout (text);
  34. setPosition (x, y);
  35. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  36. }
  37. void BubbleMessageComponent::showAt (Component* const component,
  38. const AttributedString& text,
  39. const int numMillisecondsBeforeRemoving,
  40. const bool removeWhenMouseClicked,
  41. const bool deleteSelfAfterUse)
  42. {
  43. createLayout (text);
  44. setPosition (component);
  45. init (numMillisecondsBeforeRemoving, removeWhenMouseClicked, deleteSelfAfterUse);
  46. }
  47. void BubbleMessageComponent::createLayout (const AttributedString& text)
  48. {
  49. textLayout.createLayoutWithBalancedLineLengths (text, 256);
  50. }
  51. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  52. const bool removeWhenMouseClicked,
  53. const bool deleteSelfAfterUse)
  54. {
  55. setVisible (true);
  56. deleteAfterUse = deleteSelfAfterUse;
  57. if (numMillisecondsBeforeRemoving > 0)
  58. expiryTime = Time::getMillisecondCounter() + numMillisecondsBeforeRemoving;
  59. else
  60. expiryTime = 0;
  61. startTimer (77);
  62. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  63. if (! (removeWhenMouseClicked && isShowing()))
  64. mouseClickCounter += 0xfffff;
  65. repaint();
  66. }
  67. void BubbleMessageComponent::getContentSize (int& w, int& h)
  68. {
  69. w = 20 + (int) textLayout.getWidth();
  70. h = 20 + (int) 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> (6.0f, 6.0f, w - 12.0f, h - 12.0f));
  76. }
  77. void BubbleMessageComponent::timerCallback()
  78. {
  79. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  80. {
  81. stopTimer();
  82. setVisible (false);
  83. if (deleteAfterUse)
  84. delete this;
  85. }
  86. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  87. {
  88. stopTimer();
  89. if (deleteAfterUse)
  90. delete this;
  91. else
  92. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  93. }
  94. }