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.

121 lines
4.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. BubbleMessageComponent::BubbleMessageComponent (int fadeOutLengthMs)
  20. : fadeOutLength (fadeOutLengthMs), mouseClickCounter (0),
  21. expiryTime (0), deleteAfterUse (false)
  22. {
  23. }
  24. BubbleMessageComponent::~BubbleMessageComponent()
  25. {
  26. }
  27. void BubbleMessageComponent::showAt (const Rectangle<int>& pos,
  28. const AttributedString& text,
  29. const int numMillisecondsBeforeRemoving,
  30. const bool removeWhenMouseClicked,
  31. const bool deleteSelfAfterUse)
  32. {
  33. createLayout (text);
  34. setPosition (pos);
  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. setAlpha (1.0f);
  56. setVisible (true);
  57. deleteAfterUse = deleteSelfAfterUse;
  58. expiryTime = numMillisecondsBeforeRemoving > 0
  59. ? (Time::getMillisecondCounter() + (uint32) numMillisecondsBeforeRemoving) : 0;
  60. mouseClickCounter = Desktop::getInstance().getMouseButtonClickCounter();
  61. if (! (removeWhenMouseClicked && isShowing()))
  62. mouseClickCounter += 0xfffff;
  63. startTimer (77);
  64. repaint();
  65. }
  66. const float bubblePaddingX = 20.0f;
  67. const float bubblePaddingY = 14.0f;
  68. void BubbleMessageComponent::getContentSize (int& w, int& h)
  69. {
  70. w = (int) (bubblePaddingX + textLayout.getWidth());
  71. h = (int) (bubblePaddingY + textLayout.getHeight());
  72. }
  73. void BubbleMessageComponent::paintContent (Graphics& g, int w, int h)
  74. {
  75. g.setColour (findColour (TooltipWindow::textColourId));
  76. textLayout.draw (g, Rectangle<float> (bubblePaddingX / 2.0f, bubblePaddingY / 2.0f,
  77. w - bubblePaddingX, h - bubblePaddingY));
  78. }
  79. void BubbleMessageComponent::timerCallback()
  80. {
  81. if (Desktop::getInstance().getMouseButtonClickCounter() > mouseClickCounter)
  82. hide (false);
  83. else if (expiryTime != 0 && Time::getMillisecondCounter() > expiryTime)
  84. hide (true);
  85. }
  86. void BubbleMessageComponent::hide (const bool fadeOut)
  87. {
  88. stopTimer();
  89. if (fadeOut)
  90. Desktop::getInstance().getAnimator().fadeOut (this, fadeOutLength);
  91. else
  92. setVisible (false);
  93. if (deleteAfterUse)
  94. delete this;
  95. }