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 #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 #if wxUSE_HYPERLINKCTRL
33 //---------------------------------------------------------------------------
35 //---------------------------------------------------------------------------
37 #include "wx/hyperlink.h"
40 #include "wx/utils.h" // for wxLaunchDefaultBrowser
43 #include "wx/clipbrd.h"
45 // ============================================================================
47 // ============================================================================
49 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl
, wxControl
)
50 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
52 // reserved for internal use only
53 #define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 BEGIN_EVENT_TABLE(wxHyperlinkCtrl
, wxControl
)
60 EVT_PAINT(wxHyperlinkCtrl::OnPaint
)
61 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown
)
62 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp
)
63 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp
)
64 EVT_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow
)
65 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow
)
67 // for the context menu
68 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxHyperlinkCtrl::OnPopUpCopy
)
71 bool wxHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
72 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
73 const wxSize
& size
, long style
, const wxString
& name
)
75 wxASSERT_MSG(!url
.empty() || !label
.empty(),
76 wxT("Both URL and label are empty ?"));
78 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
81 // set to non empty strings both the url and the label
92 // by default the cursor to use in this window is wxCURSOR_HAND
93 SetCursor(wxCursor(wxCURSOR_HAND
));
100 m_normalColour
= *wxBLUE
;
101 m_hoverColour
= *wxRED
;
102 SetForegroundColour(m_normalColour
);
104 // by default the font of an hyperlink control is underlined
105 wxFont f
= GetFont();
106 f
.SetUnderlined(true);
109 CacheBestSize(DoGetBestSize());
110 SetMinSize(GetBestSize());
111 SetSize (DoGetBestSize());
116 wxSize
wxHyperlinkCtrl::DoGetBestSize() const
120 wxClientDC
dc((wxWindow
*)this);
121 dc
.SetFont(GetFont());
122 dc
.GetTextExtent(GetLabel(), &w
, &h
);
127 void wxHyperlinkCtrl::DoGetSize(int *width
, int *height
) const
129 if (width
) *width
= GetBestSize().GetWidth();
130 if (height
) *height
= GetBestSize().GetHeight();
133 void wxHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
135 m_normalColour
= colour
;
138 SetForegroundColour(m_normalColour
);
143 void wxHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
145 m_visitedColour
= colour
;
148 SetForegroundColour(m_visitedColour
);
153 void wxHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
155 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
156 menuPopUp
->Append(wxHYPERLINKCTRL_POPUP_COPY_ID
, wxT("Copy URL"));
157 PopupMenu( menuPopUp
, pos
);
163 // ----------------------------------------------------------------------------
164 // wxHyperlinkCtrl - event handlers
165 // ----------------------------------------------------------------------------
167 void wxHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
170 dc
.SetFont(GetFont());
171 dc
.SetTextForeground(GetForegroundColour());
172 dc
.SetTextBackground(GetBackgroundColour());
173 dc
.DrawText(GetLabel(), 0, 0);
176 void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent
& WXUNUSED(event
))
181 void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent
& WXUNUSED(event
))
183 if (!m_clicking
) return;
185 SetForegroundColour(m_visitedColour
);
190 wxHyperlinkEvent
linkEvent(this, GetId(), m_url
);
191 if (!GetEventHandler()->ProcessEvent(linkEvent
)) // was the event skipped ?
192 if (!wxLaunchDefaultBrowser(m_url
))
193 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url
.c_str());
196 void wxHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
198 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
199 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
202 void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent
& WXUNUSED(event
))
204 SetForegroundColour(m_hoverColour
);
209 void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
))
213 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
219 void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
221 if (!wxTheClipboard
->Open())
224 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
225 wxTheClipboard
->SetData( data
);
226 wxTheClipboard
->Close();
229 #endif // wxUSE_HYPERLINKCTRL