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 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
61 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
62 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
63 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
64 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
65 EVT_MOTION(wxHyperlinkCtrl::OnMotion
)
66 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
67 EVT_SIZE(wxHyperlinkCtrl::OnSize
)
69 // for the context menu
70 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
73 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
74 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
75 const wxSize
& size
, long style
, const wxString
& name
)
77 wxASSERT_MSG(!url
.empty() || !label
.empty(),
78 wxT("Both URL and label are empty ?"));
81 int alignment
= (int)((style
& wxHL_ALIGN_LEFT
) != 0) +
82 (int)((style
& wxHL_ALIGN_CENTRE
) != 0) +
83 (int)((style
& wxHL_ALIGN_RIGHT
) != 0);
84 wxASSERT_MSG(alignment
== 1,
85 wxT("Specify exactly one align flag!"));
88 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
91 // set to non empty strings both the url and the label
107 m_normalColour
= *wxBLUE
;
108 m_hoverColour
= *wxRED
;
109 SetForegroundColour(m_normalColour
);
111 // by default the font of an hyperlink control is underlined
112 wxFont f
= GetFont();
113 f
.SetUnderlined(true);
116 CacheBestSize(DoGetBestSize());
117 SetMinSize(GetBestSize());
118 SetSize (DoGetBestSize());
123 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
127 wxClientDC
dc((wxWindow
*)this);
128 dc
.SetFont(GetFont());
129 dc
.GetTextExtent(GetLabel(), &w
, &h
);
134 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
136 if (width
) *width
= GetBestSize().GetWidth();
137 if (height
) *height
= GetBestSize().GetHeight();
140 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
142 m_normalColour
= colour
;
145 SetForegroundColour(m_normalColour
);
150 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
152 m_visitedColour
= colour
;
155 SetForegroundColour(m_visitedColour
);
160 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
162 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
163 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
164 PopupMenu( menuPopUp
, pos
);
168 wxRect
wxHyperlinkCtrl::GetLabelRect() const
170 // our best size is always the size of the label without borders
171 wxSize
c(GetClientSize()), b(GetBestSize());
174 // the label is always centered vertically
175 offset
.y
= (c
.GetHeight()-b
.GetHeight())/2;
177 if (HasFlag(wxHL_ALIGN_CENTRE
))
178 offset
.x
= (c
.GetWidth()-b
.GetWidth())/2;
179 else if (HasFlag(wxHL_ALIGN_RIGHT
))
180 offset
.x
= c
.GetWidth()-b
.GetWidth();
181 else if (HasFlag(wxHL_ALIGN_LEFT
))
183 return wxRect(offset
, b
);
188 // ----------------------------------------------------------------------------
189 // wxHyperlinkCtrl - event handlers
190 // ----------------------------------------------------------------------------
192 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
195 dc
.SetFont(GetFont());
196 dc
.SetTextForeground(GetForegroundColour());
197 dc
.SetTextBackground(GetBackgroundColour());
199 dc
.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
202 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& event
)
204 // the left click must start from the hyperlink rect
205 m_clicking
= GetLabelRect().Contains(event
.GetPosition());
208 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& event
)
210 // the click must be started and ended in the hyperlink rect
211 if (!m_clicking
|| !GetLabelRect().Contains(event
.GetPosition()))
214 SetForegroundColour(m_visitedColour
);
219 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
220 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
221 if (!wxLaunchDefaultBrowser(m_url
))
222 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
225 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
227 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
228 if ( GetLabelRect().Contains(event
.GetPosition()) )
229 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
232 void wxHyperlinkCtrl::OnMotion(wxMouseEvent
& event
)
234 wxRect textrc
= GetLabelRect();
236 if (textrc
.Contains(event
.GetPosition()))
238 SetCursor(wxCursor(wxCURSOR_HAND
));
239 SetForegroundColour(m_hoverColour
);
245 SetCursor(*wxSTANDARD_CURSOR
);
246 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
252 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
) )
254 // NB: when the label rect and the client size rect have the same
255 // height this function is indispensable to remove the "rollover"
256 // effect as the OnMotion() event handler could not be called
257 // in that case moving the mouse out of the label vertically...
261 SetCursor(*wxSTANDARD_CURSOR
);
262 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
268 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
271 if (!wxTheClipboard
->Open())
274 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
275 wxTheClipboard
->SetData( data
);
276 wxTheClipboard
->Close();
277 #endif // wxUSE_CLIPBOARD
280 void wxHyperlinkCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
282 // update the position of the label in the screen respecting
283 // the selected align flag
287 #endif // wxUSE_HYPERLINKCTRL