1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/hyperlinkg.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"
40 #include "wx/dataobj.h"
43 #include "wx/clipbrd.h"
45 // ============================================================================
47 // ============================================================================
49 // reserved for internal use only
50 #define wxHYPERLINK_POPUP_COPY_ID 16384
52 // ----------------------------------------------------------------------------
53 // wxGenericHyperlinkCtrl
54 // ----------------------------------------------------------------------------
56 bool wxGenericHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
57 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
58 const wxSize
& size
, long style
, const wxString
& name
)
60 // do validation checks:
61 CheckParams(label
, url
, style
);
63 if ((style
& wxHL_ALIGN_LEFT
) == 0)
64 style
|= wxFULL_REPAINT_ON_RESIZE
;
66 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
69 // set to non empty strings both the url and the label
70 SetURL(url
.empty() ? label
: url
);
71 SetLabel(label
.empty() ? url
: label
);
74 SetForegroundColour(m_normalColour
);
76 // by default the font of an hyperlink control is underlined
78 f
.SetUnderlined(true);
84 // connect our event handlers:
85 // NOTE: since this class is the base class of the GTK+'s native implementation
86 // of wxHyperlinkCtrl, we cannot use the static macros in BEGIN/END_EVENT_TABLE
87 // blocks, otherwise the GTK+'s native impl of wxHyperlinkCtrl would not
88 // behave correctly (as we intercept events doing things which interfere
89 // with GTK+'s native handling):
91 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxGenericHyperlinkCtrl::OnPaint
) );
92 Connect( wxEVT_LEAVE_WINDOW
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeaveWindow
) );
94 Connect( wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftDown
) );
95 Connect( wxEVT_LEFT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftUp
) );
96 Connect( wxEVT_MOTION
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnMotion
) );
98 ConnectMenuHandlers();
103 void wxGenericHyperlinkCtrl::Init()
110 m_normalColour
= *wxBLUE
;
111 m_hoverColour
= *wxRED
;
112 m_visitedColour
= wxColour("#551a8b");
115 void wxGenericHyperlinkCtrl::ConnectMenuHandlers()
117 // Connect the event handlers for the context menu.
118 Connect( wxEVT_RIGHT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnRightUp
) );
119 Connect( wxHYPERLINK_POPUP_COPY_ID
, wxEVT_COMMAND_MENU_SELECTED
,
120 wxCommandEventHandler(wxGenericHyperlinkCtrl::OnPopUpCopy
) );
123 wxSize
wxGenericHyperlinkCtrl::DoGetBestClientSize() const
125 wxClientDC
dc((wxWindow
*)this);
126 return dc
.GetTextExtent(GetLabel());
130 void wxGenericHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
132 m_normalColour
= colour
;
135 SetForegroundColour(m_normalColour
);
140 void wxGenericHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
142 m_visitedColour
= colour
;
145 SetForegroundColour(m_visitedColour
);
150 void wxGenericHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
152 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
153 menuPopUp
->Append(wxHYPERLINK_POPUP_COPY_ID
, _("&Copy URL"));
154 PopupMenu( menuPopUp
, pos
);
158 wxRect
wxGenericHyperlinkCtrl::GetLabelRect() const
160 // our best size is always the size of the label without borders
161 wxSize
c(GetClientSize()), b(GetBestSize());
164 // the label is always centered vertically
165 offset
.y
= (c
.GetHeight()-b
.GetHeight())/2;
167 if (HasFlag(wxHL_ALIGN_CENTRE
))
168 offset
.x
= (c
.GetWidth()-b
.GetWidth())/2;
169 else if (HasFlag(wxHL_ALIGN_RIGHT
))
170 offset
.x
= c
.GetWidth()-b
.GetWidth();
171 else if (HasFlag(wxHL_ALIGN_LEFT
))
173 return wxRect(offset
, b
);
178 // ----------------------------------------------------------------------------
179 // wxGenericHyperlinkCtrl - event handlers
180 // ----------------------------------------------------------------------------
182 void wxGenericHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
185 dc
.SetFont(GetFont());
186 dc
.SetTextForeground(GetForegroundColour());
187 dc
.SetTextBackground(GetBackgroundColour());
189 dc
.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
192 void wxGenericHyperlinkCtrl::OnLeftDown(wxMouseEvent
& event
)
194 // the left click must start from the hyperlink rect
195 m_clicking
= GetLabelRect().Contains(event
.GetPosition());
198 void wxGenericHyperlinkCtrl::OnLeftUp(wxMouseEvent
& event
)
200 // the click must be started and ended in the hyperlink rect
201 if (!m_clicking
|| !GetLabelRect().Contains(event
.GetPosition()))
204 SetForegroundColour(m_visitedColour
);
212 void wxGenericHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
214 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
215 if ( GetLabelRect().Contains(event
.GetPosition()) )
216 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
219 void wxGenericHyperlinkCtrl::OnMotion(wxMouseEvent
& event
)
221 wxRect textrc
= GetLabelRect();
223 if (textrc
.Contains(event
.GetPosition()))
225 SetCursor(wxCursor(wxCURSOR_HAND
));
226 SetForegroundColour(m_hoverColour
);
232 SetCursor(*wxSTANDARD_CURSOR
);
233 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
239 void wxGenericHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
) )
241 // NB: when the label rect and the client size rect have the same
242 // height this function is indispensable to remove the "rollover"
243 // effect as the OnMotion() event handler could not be called
244 // in that case moving the mouse out of the label vertically...
248 SetCursor(*wxSTANDARD_CURSOR
);
249 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
255 void wxGenericHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
258 if (!wxTheClipboard
->Open())
261 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
262 wxTheClipboard
->SetData( data
);
263 wxTheClipboard
->Close();
264 #endif // wxUSE_CLIPBOARD
267 #endif // wxUSE_HYPERLINKCTRL