1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/hyperlink.cpp
3 // Purpose: Hyperlink control
4 // Author: David Norris <danorris@gmail.com>, Otto Wyss
5 // Modified by: Ryan Norton, Francesco Montorsi
8 // Copyright: (c) 2005 David Norris
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_HYPERLINKCTRL
29 //---------------------------------------------------------------------------
31 //---------------------------------------------------------------------------
33 #include "wx/hyperlink.h"
36 #include "wx/utils.h" // for wxLaunchDefaultBrowser
37 #include "wx/dcclient.h"
40 #include "wx/dataobj.h"
43 #include "wx/clipbrd.h"
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxControl
)
50 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkEvent
, wxCommandEvent
)
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
53 // reserved for internal use only
54 #define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
56 const wxChar wxHyperlinkCtrlNameStr
[] = wxT("hyperlink");
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
63 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
64 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
65 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
66 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
67 EVT_MOTION(wxHyperlinkCtrl::OnMotion
)
68 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
69 EVT_SIZE(wxHyperlinkCtrl::OnSize
)
71 // for the context menu
72 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
75 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
76 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
77 const wxSize
& size
, long style
, const wxString
& name
)
79 wxASSERT_MSG(!url
.empty() || !label
.empty(),
80 wxT("Both URL and label are empty ?"));
83 int alignment
= (int)((style
& wxHL_ALIGN_LEFT
) != 0) +
84 (int)((style
& wxHL_ALIGN_CENTRE
) != 0) +
85 (int)((style
& wxHL_ALIGN_RIGHT
) != 0);
86 wxASSERT_MSG(alignment
== 1,
87 wxT("Specify exactly one align flag!"));
90 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
93 // set to non empty strings both the url and the label
109 m_normalColour
= *wxBLUE
;
110 m_hoverColour
= *wxRED
;
111 SetForegroundColour(m_normalColour
);
113 // by default the font of an hyperlink control is underlined
114 wxFont f
= GetFont();
115 f
.SetUnderlined(true);
118 CacheBestSize(DoGetBestSize());
119 SetMinSize(GetBestSize());
120 SetSize (DoGetBestSize());
125 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
129 wxClientDC
dc((wxWindow
*)this);
130 dc
.SetFont(GetFont());
131 dc
.GetTextExtent(GetLabel(), &w
, &h
);
136 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
138 if (width
) *width
= GetBestSize().GetWidth();
139 if (height
) *height
= GetBestSize().GetHeight();
142 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
144 m_normalColour
= colour
;
147 SetForegroundColour(m_normalColour
);
152 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
154 m_visitedColour
= colour
;
157 SetForegroundColour(m_visitedColour
);
162 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
164 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
165 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
166 PopupMenu( menuPopUp
, pos
);
170 wxRect
wxHyperlinkCtrl::GetLabelRect() const
172 // our best size is always the size of the label without borders
173 wxSize
c(GetClientSize()), b(GetBestSize());
176 // the label is always centered vertically
177 offset
.y
= (c
.GetHeight()-b
.GetHeight())/2;
179 if (HasFlag(wxHL_ALIGN_CENTRE
))
180 offset
.x
= (c
.GetWidth()-b
.GetWidth())/2;
181 else if (HasFlag(wxHL_ALIGN_RIGHT
))
182 offset
.x
= c
.GetWidth()-b
.GetWidth();
183 else if (HasFlag(wxHL_ALIGN_LEFT
))
185 return wxRect(offset
, b
);
190 // ----------------------------------------------------------------------------
191 // wxHyperlinkCtrl - event handlers
192 // ----------------------------------------------------------------------------
194 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
197 dc
.SetFont(GetFont());
198 dc
.SetTextForeground(GetForegroundColour());
199 dc
.SetTextBackground(GetBackgroundColour());
201 dc
.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
204 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& event
)
206 // the left click must start from the hyperlink rect
207 m_clicking
= GetLabelRect().Contains(event
.GetPosition());
210 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& event
)
212 // the click must be started and ended in the hyperlink rect
213 if (!m_clicking
|| !GetLabelRect().Contains(event
.GetPosition()))
216 SetForegroundColour(m_visitedColour
);
221 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
222 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
223 if (!wxLaunchDefaultBrowser(m_url
))
224 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
227 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
229 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
230 if ( GetLabelRect().Contains(event
.GetPosition()) )
231 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
234 void wxHyperlinkCtrl::OnMotion(wxMouseEvent
& event
)
236 wxRect textrc
= GetLabelRect();
238 if (textrc
.Contains(event
.GetPosition()))
240 SetCursor(wxCursor(wxCURSOR_HAND
));
241 SetForegroundColour(m_hoverColour
);
247 SetCursor(*wxSTANDARD_CURSOR
);
248 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
254 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
) )
256 // NB: when the label rect and the client size rect have the same
257 // height this function is indispensable to remove the "rollover"
258 // effect as the OnMotion() event handler could not be called
259 // in that case moving the mouse out of the label vertically...
263 SetCursor(*wxSTANDARD_CURSOR
);
264 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
270 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
273 if (!wxTheClipboard
->Open())
276 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
277 wxTheClipboard
->SetData( data
);
278 wxTheClipboard
->Close();
279 #endif // wxUSE_CLIPBOARD
282 void wxHyperlinkCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
284 // update the position of the label in the screen respecting
285 // the selected align flag
289 #endif // wxUSE_HYPERLINKCTRL