juce_HyperlinkButton.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. HyperlinkButton::HyperlinkButton (const String& linkText,
  18. const URL& linkURL)
  19. : Button (linkText),
  20. url (linkURL),
  21. font (14.0f, Font::underlined),
  22. resizeFont (true),
  23. justification (Justification::centred)
  24. {
  25. setMouseCursor (MouseCursor::PointingHandCursor);
  26. setTooltip (linkURL.toString (false));
  27. }
  28. HyperlinkButton::HyperlinkButton ()
  29. : Button (String::empty),
  30. font (14.0f, Font::underlined),
  31. resizeFont (true),
  32. justification (Justification::centred)
  33. {
  34. setMouseCursor (MouseCursor::PointingHandCursor);
  35. }
  36. HyperlinkButton::~HyperlinkButton()
  37. {
  38. }
  39. //==============================================================================
  40. void HyperlinkButton::setFont (const Font& newFont,
  41. const bool resizeToMatchComponentHeight,
  42. Justification justificationType)
  43. {
  44. font = newFont;
  45. resizeFont = resizeToMatchComponentHeight;
  46. justification = justificationType;
  47. repaint();
  48. }
  49. void HyperlinkButton::setURL (const URL& newURL) noexcept
  50. {
  51. url = newURL;
  52. setTooltip (newURL.toString (false));
  53. }
  54. Font HyperlinkButton::getFontToUse() const
  55. {
  56. if (resizeFont)
  57. return font.withHeight (getHeight() * 0.7f);
  58. return font;
  59. }
  60. void HyperlinkButton::changeWidthToFitText()
  61. {
  62. setSize (getFontToUse().getStringWidth (getButtonText()) + 6, getHeight());
  63. }
  64. void HyperlinkButton::colourChanged()
  65. {
  66. repaint();
  67. }
  68. //==============================================================================
  69. void HyperlinkButton::clicked()
  70. {
  71. if (url.isWellFormed())
  72. url.launchInDefaultBrowser();
  73. }
  74. void HyperlinkButton::paintButton (Graphics& g,
  75. bool isMouseOverButton,
  76. bool isButtonDown)
  77. {
  78. const Colour textColour (findColour (textColourId));
  79. if (isEnabled())
  80. g.setColour ((isMouseOverButton) ? textColour.darker ((isButtonDown) ? 1.3f : 0.4f)
  81. : textColour);
  82. else
  83. g.setColour (textColour.withMultipliedAlpha (0.4f));
  84. g.setFont (getFontToUse());
  85. g.drawText (getButtonText(), getLocalBounds().reduced (1, 0),
  86. justification.getOnlyHorizontalFlags() | Justification::verticallyCentred,
  87. true);
  88. }