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.

126 lines
3.5KB

  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. namespace juce
  20. {
  21. HyperlinkButton::HyperlinkButton (const String& linkText,
  22. const URL& linkURL)
  23. : Button (linkText),
  24. url (linkURL),
  25. font (14.0f, Font::underlined),
  26. resizeFont (true),
  27. justification (Justification::centred)
  28. {
  29. setMouseCursor (MouseCursor::PointingHandCursor);
  30. setTooltip (linkURL.toString (false));
  31. }
  32. HyperlinkButton::HyperlinkButton()
  33. : Button (String()),
  34. font (14.0f, Font::underlined),
  35. resizeFont (true),
  36. justification (Justification::centred)
  37. {
  38. setMouseCursor (MouseCursor::PointingHandCursor);
  39. }
  40. HyperlinkButton::~HyperlinkButton()
  41. {
  42. }
  43. //==============================================================================
  44. void HyperlinkButton::setFont (const Font& newFont,
  45. const bool resizeToMatchComponentHeight,
  46. Justification justificationType)
  47. {
  48. font = newFont;
  49. resizeFont = resizeToMatchComponentHeight;
  50. justification = justificationType;
  51. repaint();
  52. }
  53. void HyperlinkButton::setURL (const URL& newURL) noexcept
  54. {
  55. url = newURL;
  56. setTooltip (newURL.toString (false));
  57. }
  58. Font HyperlinkButton::getFontToUse() const
  59. {
  60. if (resizeFont)
  61. return font.withHeight (getHeight() * 0.7f);
  62. return font;
  63. }
  64. void HyperlinkButton::changeWidthToFitText()
  65. {
  66. setSize (getFontToUse().getStringWidth (getButtonText()) + 6, getHeight());
  67. }
  68. void HyperlinkButton::setJustificationType (Justification newJustification)
  69. {
  70. if (justification != newJustification)
  71. {
  72. justification = newJustification;
  73. repaint();
  74. }
  75. }
  76. void HyperlinkButton::colourChanged()
  77. {
  78. repaint();
  79. }
  80. //==============================================================================
  81. void HyperlinkButton::clicked()
  82. {
  83. if (url.isWellFormed())
  84. url.launchInDefaultBrowser();
  85. }
  86. void HyperlinkButton::paintButton (Graphics& g,
  87. bool isMouseOverButton,
  88. bool isButtonDown)
  89. {
  90. const Colour textColour (findColour (textColourId));
  91. if (isEnabled())
  92. g.setColour ((isMouseOverButton) ? textColour.darker ((isButtonDown) ? 1.3f : 0.4f)
  93. : textColour);
  94. else
  95. g.setColour (textColour.withMultipliedAlpha (0.4f));
  96. g.setFont (getFontToUse());
  97. g.drawText (getButtonText(), getLocalBounds().reduced (1, 0),
  98. justification.getOnlyHorizontalFlags() | Justification::verticallyCentred,
  99. true);
  100. }
  101. } // namespace juce