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 IMPLEMENT_DYNAMIC_CLASS(wxGenericHyperlinkCtrl
, wxControl
)
51 // reserved for internal use only
52 #define wxHYPERLINK_POPUP_COPY_ID 16384
54 // ----------------------------------------------------------------------------
55 // wxGenericHyperlinkCtrl
56 // ----------------------------------------------------------------------------
58 bool wxGenericHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
59 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
60 const wxSize
& size
, long style
, const wxString
& name
)
62 // do validation checks:
63 CheckParams(label
, url
, style
);
65 if ((style
& wxHL_ALIGN_LEFT
) == 0)
66 style
|= wxFULL_REPAINT_ON_RESIZE
;
68 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
71 // set to non empty strings both the url and the label
72 SetURL(url
.empty() ? label
: url
);
73 SetLabel(label
.empty() ? url
: label
);
80 m_normalColour
= *wxBLUE
;
81 m_hoverColour
= *wxRED
;
82 SetForegroundColour(m_normalColour
);
84 // by default the font of an hyperlink control is underlined
86 f
.SetUnderlined(true);
92 // connect our event handlers:
93 // NOTE: since this class is the base class of the GTK+'s native implementation
94 // of wxHyperlinkCtrl, we cannot use the static macros in BEGIN/END_EVENT_TABLE
95 // blocks, otherwise the GTK+'s native impl of wxHyperlinkCtrl would not
96 // behave correctly (as we intercept events doing things which interfere
97 // with GTK+'s native handling):
99 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxGenericHyperlinkCtrl::OnPaint
) );
100 Connect( wxEVT_LEAVE_WINDOW
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeaveWindow
) );
102 Connect( wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftDown
) );
103 Connect( wxEVT_LEFT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftUp
) );
104 Connect( wxEVT_RIGHT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnRightUp
) );
105 Connect( wxEVT_MOTION
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnMotion
) );
107 Connect( wxHYPERLINK_POPUP_COPY_ID
, wxEVT_COMMAND_MENU_SELECTED
,
108 wxCommandEventHandler(wxGenericHyperlinkCtrl::OnPopUpCopy
) );
113 wxSize
wxGenericHyperlinkCtrl::DoGetBestSize() const
117 wxClientDC
dc((wxWindow
*)this);
118 dc
.SetFont(GetFont());
119 dc
.GetTextExtent(GetLabel(), &w
, &h
);
127 void wxGenericHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
129 m_normalColour
= colour
;
132 SetForegroundColour(m_normalColour
);
137 void wxGenericHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
139 m_visitedColour
= colour
;
142 SetForegroundColour(m_visitedColour
);
147 void wxGenericHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
149 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
150 menuPopUp
->Append(wxHYPERLINK_POPUP_COPY_ID
, wxT("Copy URL"));
151 PopupMenu( menuPopUp
, pos
);
155 wxRect
wxGenericHyperlinkCtrl::GetLabelRect() const
157 // our best size is always the size of the label without borders
158 wxSize
c(GetClientSize()), b(GetBestSize());
161 // the label is always centered vertically
162 offset
.y
= (c
.GetHeight()-b
.GetHeight())/2;
164 if (HasFlag(wxHL_ALIGN_CENTRE
))
165 offset
.x
= (c
.GetWidth()-b
.GetWidth())/2;
166 else if (HasFlag(wxHL_ALIGN_RIGHT
))
167 offset
.x
= c
.GetWidth()-b
.GetWidth();
168 else if (HasFlag(wxHL_ALIGN_LEFT
))
170 return wxRect(offset
, b
);
175 // ----------------------------------------------------------------------------
176 // wxGenericHyperlinkCtrl - event handlers
177 // ----------------------------------------------------------------------------
179 void wxGenericHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
182 dc
.SetFont(GetFont());
183 dc
.SetTextForeground(GetForegroundColour());
184 dc
.SetTextBackground(GetBackgroundColour());
186 dc
.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
189 void wxGenericHyperlinkCtrl::OnLeftDown(wxMouseEvent
& event
)
191 // the left click must start from the hyperlink rect
192 m_clicking
= GetLabelRect().Contains(event
.GetPosition());
195 void wxGenericHyperlinkCtrl::OnLeftUp(wxMouseEvent
& event
)
197 // the click must be started and ended in the hyperlink rect
198 if (!m_clicking
|| !GetLabelRect().Contains(event
.GetPosition()))
201 SetForegroundColour(m_visitedColour
);
209 void wxGenericHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
211 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
212 if ( GetLabelRect().Contains(event
.GetPosition()) )
213 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
216 void wxGenericHyperlinkCtrl::OnMotion(wxMouseEvent
& event
)
218 wxRect textrc
= GetLabelRect();
220 if (textrc
.Contains(event
.GetPosition()))
222 SetCursor(wxCursor(wxCURSOR_HAND
));
223 SetForegroundColour(m_hoverColour
);
229 SetCursor(*wxSTANDARD_CURSOR
);
230 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
236 void wxGenericHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
) )
238 // NB: when the label rect and the client size rect have the same
239 // height this function is indispensable to remove the "rollover"
240 // effect as the OnMotion() event handler could not be called
241 // in that case moving the mouse out of the label vertically...
245 SetCursor(*wxSTANDARD_CURSOR
);
246 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
252 void wxGenericHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
255 if (!wxTheClipboard
->Open())
258 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
259 wxTheClipboard
->SetData( data
);
260 wxTheClipboard
->Close();
261 #endif // wxUSE_CLIPBOARD
264 #endif // wxUSE_HYPERLINKCTRL