1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "hyperlink.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 //---------------------------------------------------------------------------
33 //---------------------------------------------------------------------------
35 #include "wx/hyperlink.h"
36 #include "wx/utils.h" // for wxLaunchDefaultBrowser
37 #include "wx/clipbrd.h"
40 #if wxUSE_HYPERLINKCTRL
42 // ============================================================================
44 // ============================================================================
46 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxControl
)
47 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
49 // reserved for internal use only
50 #define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
57 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
58 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
59 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
60 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
61 EVT_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow
)
62 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
64 // for the context menu
65 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
68 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
69 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
70 const wxSize
& size
, long style
, const wxString
& name
)
72 wxASSERT_MSG(!url
.IsEmpty() || !label
.IsEmpty(),
73 wxT("Both URL and label are empty ?"));
75 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
78 // set to non empty strings both the url and the label
79 SetURL(url
.IsEmpty() ? label
: url
);
80 SetLabel(label
.IsEmpty() ? url
: label
);
82 // by default the cursor to use in this window is wxCURSOR_HAND
83 SetCursor(wxCursor(wxCURSOR_HAND
));
90 m_normalColour
= *wxBLUE
;
91 m_hoverColour
= *wxRED
;
92 SetForegroundColour(m_normalColour
);
94 // by default the font of an hyperlink control is underlined
96 f
.SetUnderlined(true);
99 CacheBestSize(DoGetBestSize());
100 SetMinSize(GetBestSize());
101 SetSize (DoGetBestSize());
106 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
110 wxClientDC
dc((wxWindow
*)this);
111 dc
.SetFont(GetFont());
112 dc
.GetTextExtent(GetLabel(), &w
, &h
);
117 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
119 if (width
) *width
= GetBestSize().GetWidth();
120 if (height
) *height
= GetBestSize().GetHeight();
123 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
125 m_normalColour
= colour
;
128 SetForegroundColour(m_normalColour
);
133 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
135 m_visitedColour
= colour
;
138 SetForegroundColour(m_visitedColour
);
143 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
145 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
146 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
147 PopupMenu( menuPopUp
, pos
);
153 // ----------------------------------------------------------------------------
154 // wxHyperlinkCtrl - event handlers
155 // ----------------------------------------------------------------------------
157 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
160 dc
.SetFont(GetFont());
161 dc
.SetTextForeground(GetForegroundColour());
162 dc
.SetTextBackground(GetBackgroundColour());
163 dc
.DrawText(GetLabel(), 0, 0);
166 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& WXUNUSED(event
))
171 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
173 if (!m_clicking
) return;
175 SetForegroundColour(m_visitedColour
);
180 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
181 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
182 if (!wxLaunchDefaultBrowser(m_url
))
183 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
186 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
188 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
189 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
192 void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent
& WXUNUSED(event
))
194 SetForegroundColour(m_hoverColour
);
199 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
203 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
209 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
211 if (!wxTheClipboard
->Open())
214 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
215 wxTheClipboard
->SetData( data
);
216 wxTheClipboard
->Close();
219 #endif // wxUSE_HYPERLINKCTRL