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
39 #include "wx/clipbrd.h"
41 // ============================================================================
43 // ============================================================================
45 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxControl
)
46 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
48 // reserved for internal use only
49 #define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
56 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
57 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
58 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
59 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
60 EVT_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow
)
61 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
63 // for the context menu
64 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
67 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
68 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
69 const wxSize
& size
, long style
, const wxString
& name
)
71 wxASSERT_MSG(!url
.empty() || !label
.empty(),
72 wxT("Both URL and label are empty ?"));
74 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
77 // set to non empty strings both the url and the label
88 // by default the cursor to use in this window is wxCURSOR_HAND
89 SetCursor(wxCursor(wxCURSOR_HAND
));
96 m_normalColour
= *wxBLUE
;
97 m_hoverColour
= *wxRED
;
98 SetForegroundColour(m_normalColour
);
100 // by default the font of an hyperlink control is underlined
101 wxFont f
= GetFont();
102 f
.SetUnderlined(true);
105 CacheBestSize(DoGetBestSize());
106 SetMinSize(GetBestSize());
107 SetSize (DoGetBestSize());
112 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
116 wxClientDC
dc((wxWindow
*)this);
117 dc
.SetFont(GetFont());
118 dc
.GetTextExtent(GetLabel(), &w
, &h
);
123 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
125 if (width
) *width
= GetBestSize().GetWidth();
126 if (height
) *height
= GetBestSize().GetHeight();
129 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
131 m_normalColour
= colour
;
134 SetForegroundColour(m_normalColour
);
139 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
141 m_visitedColour
= colour
;
144 SetForegroundColour(m_visitedColour
);
149 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
151 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
152 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
153 PopupMenu( menuPopUp
, pos
);
159 // ----------------------------------------------------------------------------
160 // wxHyperlinkCtrl - event handlers
161 // ----------------------------------------------------------------------------
163 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
166 dc
.SetFont(GetFont());
167 dc
.SetTextForeground(GetForegroundColour());
168 dc
.SetTextBackground(GetBackgroundColour());
169 dc
.DrawText(GetLabel(), 0, 0);
172 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& WXUNUSED(event
))
177 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
179 if (!m_clicking
) return;
181 SetForegroundColour(m_visitedColour
);
186 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
187 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
188 if (!wxLaunchDefaultBrowser(m_url
))
189 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
192 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
194 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
195 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
198 void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent
& WXUNUSED(event
))
200 SetForegroundColour(m_hoverColour
);
205 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
209 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
215 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
218 if (!wxTheClipboard
->Open())
221 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
222 wxTheClipboard
->SetData( data
);
223 wxTheClipboard
->Close();
224 #endif // wxUSE_CLIPBOARD
227 #endif // wxUSE_HYPERLINKCTRL