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"
42 #include "wx/clipbrd.h"
44 // ============================================================================
46 // ============================================================================
48 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxControl
)
49 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
51 // reserved for internal use only
52 #define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
59 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
60 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
61 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
62 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
63 EVT_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow
)
64 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
66 // for the context menu
67 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
70 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
71 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
72 const wxSize
& size
, long style
, const wxString
& name
)
74 wxASSERT_MSG(!url
.empty() || !label
.empty(),
75 wxT("Both URL and label are empty ?"));
77 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
80 // set to non empty strings both the url and the label
91 // by default the cursor to use in this window is wxCURSOR_HAND
92 SetCursor(wxCursor(wxCURSOR_HAND
));
99 m_normalColour
= *wxBLUE
;
100 m_hoverColour
= *wxRED
;
101 SetForegroundColour(m_normalColour
);
103 // by default the font of an hyperlink control is underlined
104 wxFont f
= GetFont();
105 f
.SetUnderlined(true);
108 CacheBestSize(DoGetBestSize());
109 SetMinSize(GetBestSize());
110 SetSize (DoGetBestSize());
115 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
119 wxClientDC
dc((wxWindow
*)this);
120 dc
.SetFont(GetFont());
121 dc
.GetTextExtent(GetLabel(), &w
, &h
);
126 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
128 if (width
) *width
= GetBestSize().GetWidth();
129 if (height
) *height
= GetBestSize().GetHeight();
132 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
134 m_normalColour
= colour
;
137 SetForegroundColour(m_normalColour
);
142 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
144 m_visitedColour
= colour
;
147 SetForegroundColour(m_visitedColour
);
152 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
154 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
155 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
156 PopupMenu( menuPopUp
, pos
);
162 // ----------------------------------------------------------------------------
163 // wxHyperlinkCtrl - event handlers
164 // ----------------------------------------------------------------------------
166 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
169 dc
.SetFont(GetFont());
170 dc
.SetTextForeground(GetForegroundColour());
171 dc
.SetTextBackground(GetBackgroundColour());
172 dc
.DrawText(GetLabel(), 0, 0);
175 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& WXUNUSED(event
))
180 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
182 if (!m_clicking
) return;
184 SetForegroundColour(m_visitedColour
);
189 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
190 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
191 if (!wxLaunchDefaultBrowser(m_url
))
192 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
195 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
197 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
198 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
201 void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent
& WXUNUSED(event
))
203 SetForegroundColour(m_hoverColour
);
208 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
212 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
218 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
221 if (!wxTheClipboard
->Open())
224 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
225 wxTheClipboard
->SetData( data
);
226 wxTheClipboard
->Close();
227 #endif // wxUSE_CLIPBOARD
230 #endif // wxUSE_HYPERLINKCTRL