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.

124 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. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  26. }
  27. void BubbleMessageComponent::showAt (int x, int y,
  28. const String& 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 String& 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 String& text)
  48. {
  49. AttributedString attString;
  50. attString.append (text, Font (14.0f));
  51. attString.setJustification (Justification::centred);
  52. textLayout.createLayoutWithBalancedLineLengths (attString, 256);
  53. }
  54. void BubbleMessageComponent::init (const int numMillisecondsBeforeRemoving,
  55. const bool removeWhenMouseClicked,
  56. const bool deleteSelfAfterUse)
  57. {
  58. setVisible (true);
  59. deleteAfterUse = deleteSelfAfterUse;
  60. if (numMillisecondsBeforeRemoving > 0)
  61. expiryTime = Time::getMillisecondCounter() + numMillisecondsBeforeRemoving;
  62. else
  63. expiryTime = 0;
  64. startTimer (77);
  65. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  66. if (! (removeWhenMouseClicked && isShowing()))
  67. mouseClickCounter += 0xfffff;
  68. repaint();
  69. }
  70. void BubbleMessageComponent::getContentSize (int& w, int& h)
  71. {
  72. w = (int) (textLayout.getWidth() + 16.0f);
  73. h = (int) (textLayout.getHeight() + 16.0f);
  74. }
  75. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  76. {
  77. g.setColour (findColour (TooltipWindow::textColourId));
  78. textLayout.draw (g, Rectangle<float> (0.0f, 0.0f, (float) w, (float) h));
  79. }
  80. void BubbleMessageComponent::timerCallback()
  81. {
  82. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  83. {
  84. stopTimer();
  85. setVisible (false);
  86. if (deleteAfterUse)
  87. delete this;
  88. }
  89. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  90. {
  91. stopTimer();
  92. if (deleteAfterUse)
  93. delete this;
  94. else
  95. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  96. }
  97. }