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"
43 #include "wx/dataobj.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_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow
)
66 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
68 // for the context menu
69 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
72 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
73 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
74 const wxSize
& size
, long style
, const wxString
& name
)
76 wxASSERT_MSG(!url
.empty() || !label
.empty(),
77 wxT("Both URL and label are empty ?"));
79 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
82 // set to non empty strings both the url and the label
93 // by default the cursor to use in this window is wxCURSOR_HAND
94 SetCursor(wxCursor(wxCURSOR_HAND
));
101 m_normalColour
= *wxBLUE
;
102 m_hoverColour
= *wxRED
;
103 SetForegroundColour(m_normalColour
);
105 // by default the font of an hyperlink control is underlined
106 wxFont f
= GetFont();
107 f
.SetUnderlined(true);
110 CacheBestSize(DoGetBestSize());
111 SetMinSize(GetBestSize());
112 SetSize (DoGetBestSize());
117 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
121 wxClientDC
dc((wxWindow
*)this);
122 dc
.SetFont(GetFont());
123 dc
.GetTextExtent(GetLabel(), &w
, &h
);
128 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
130 if (width
) *width
= GetBestSize().GetWidth();
131 if (height
) *height
= GetBestSize().GetHeight();
134 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
136 m_normalColour
= colour
;
139 SetForegroundColour(m_normalColour
);
144 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
146 m_visitedColour
= colour
;
149 SetForegroundColour(m_visitedColour
);
154 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
156 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
157 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
158 PopupMenu( menuPopUp
, pos
);
164 // ----------------------------------------------------------------------------
165 // wxHyperlinkCtrl - event handlers
166 // ----------------------------------------------------------------------------
168 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
171 dc
.SetFont(GetFont());
172 dc
.SetTextForeground(GetForegroundColour());
173 dc
.SetTextBackground(GetBackgroundColour());
174 dc
.DrawText(GetLabel(), 0, 0);
177 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& WXUNUSED(event
))
182 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
184 if (!m_clicking
) return;
186 SetForegroundColour(m_visitedColour
);
191 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
192 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
193 if (!wxLaunchDefaultBrowser(m_url
))
194 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
197 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
199 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
200 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
203 void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent
& WXUNUSED(event
))
205 SetForegroundColour(m_hoverColour
);
210 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
214 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
220 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
223 if (!wxTheClipboard
->Open())
226 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
227 wxTheClipboard
->SetData( data
);
228 wxTheClipboard
->Close();
229 #endif // wxUSE_CLIPBOARD
232 #endif // wxUSE_HYPERLINKCTRL