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.

129 lines
4.3KB

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