1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
HyperlinkButton(const String & linkText,const URL & linkURL)29 HyperlinkButton::HyperlinkButton (const String& linkText,
30                                   const URL& linkURL)
31    : Button (linkText),
32      url (linkURL),
33      font (14.0f, Font::underlined),
34      resizeFont (true),
35      justification (Justification::centred)
36 {
37     setMouseCursor (MouseCursor::PointingHandCursor);
38     setTooltip (linkURL.toString (false));
39 }
40 
HyperlinkButton()41 HyperlinkButton::HyperlinkButton()
42    : Button (String()),
43      font (14.0f, Font::underlined),
44      resizeFont (true),
45      justification (Justification::centred)
46 {
47     setMouseCursor (MouseCursor::PointingHandCursor);
48 }
49 
~HyperlinkButton()50 HyperlinkButton::~HyperlinkButton()
51 {
52 }
53 
54 //==============================================================================
setFont(const Font & newFont,const bool resizeToMatchComponentHeight,Justification justificationType)55 void HyperlinkButton::setFont (const Font& newFont,
56                                const bool resizeToMatchComponentHeight,
57                                Justification justificationType)
58 {
59     font = newFont;
60     resizeFont = resizeToMatchComponentHeight;
61     justification = justificationType;
62     repaint();
63 }
64 
setURL(const URL & newURL)65 void HyperlinkButton::setURL (const URL& newURL) noexcept
66 {
67     url = newURL;
68     setTooltip (newURL.toString (false));
69 }
70 
getFontToUse() const71 Font HyperlinkButton::getFontToUse() const
72 {
73     if (resizeFont)
74         return font.withHeight ((float) getHeight() * 0.7f);
75 
76     return font;
77 }
78 
changeWidthToFitText()79 void HyperlinkButton::changeWidthToFitText()
80 {
81     setSize (getFontToUse().getStringWidth (getButtonText()) + 6, getHeight());
82 }
83 
setJustificationType(Justification newJustification)84 void HyperlinkButton::setJustificationType (Justification newJustification)
85 {
86     if (justification != newJustification)
87     {
88         justification = newJustification;
89         repaint();
90     }
91 }
92 
colourChanged()93 void HyperlinkButton::colourChanged()
94 {
95     repaint();
96 }
97 
98 //==============================================================================
clicked()99 void HyperlinkButton::clicked()
100 {
101     if (url.isWellFormed())
102         url.launchInDefaultBrowser();
103 }
104 
paintButton(Graphics & g,bool shouldDrawButtonAsHighlighted,bool shouldDrawButtonAsDown)105 void HyperlinkButton::paintButton (Graphics& g,
106                                    bool shouldDrawButtonAsHighlighted,
107                                    bool shouldDrawButtonAsDown)
108 {
109     const Colour textColour (findColour (textColourId));
110 
111     if (isEnabled())
112         g.setColour ((shouldDrawButtonAsHighlighted) ? textColour.darker ((shouldDrawButtonAsDown) ? 1.3f : 0.4f)
113                                          : textColour);
114     else
115         g.setColour (textColour.withMultipliedAlpha (0.4f));
116 
117     g.setFont (getFontToUse());
118 
119     g.drawText (getButtonText(), getLocalBounds().reduced (1, 0),
120                 justification.getOnlyHorizontalFlags() | Justification::verticallyCentred,
121                 true);
122 }
123 
124 } // namespace juce
125