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"
44 #include "wx/renderer.h"
46 // ============================================================================
48 // ============================================================================
50 // reserved for internal use only
51 #define wxHYPERLINK_POPUP_COPY_ID 16384
53 // ----------------------------------------------------------------------------
54 // wxGenericHyperlinkCtrl
55 // ----------------------------------------------------------------------------
57 bool wxGenericHyperlinkCtrl::Create(wxWindow
*parent
, wxWindowID id
,
58 const wxString
& label
, const wxString
& url
, const wxPoint
& pos
,
59 const wxSize
& size
, long style
, const wxString
& name
)
61 // do validation checks:
62 CheckParams(label
, url
, style
);
64 if ((style
& wxHL_ALIGN_LEFT
) == 0)
65 style
|= wxFULL_REPAINT_ON_RESIZE
;
67 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
70 // set to non empty strings both the url and the label
71 SetURL(url
.empty() ? label
: url
);
72 SetLabel(label
.empty() ? url
: label
);
75 SetForegroundColour(m_normalColour
);
77 // by default the font of an hyperlink control is underlined
79 f
.SetUnderlined(true);
85 // connect our event handlers:
86 // NOTE: since this class is the base class of the GTK+'s native implementation
87 // of wxHyperlinkCtrl, we cannot use the static macros in BEGIN/END_EVENT_TABLE
88 // blocks, otherwise the GTK+'s native impl of wxHyperlinkCtrl would not
89 // behave correctly (as we intercept events doing things which interfere
90 // with GTK+'s native handling):
92 Connect( wxEVT_PAINT
, wxPaintEventHandler(wxGenericHyperlinkCtrl::OnPaint
) );
93 Connect( wxEVT_SET_FOCUS
, wxFocusEventHandler(wxGenericHyperlinkCtrl::OnFocus
) );
94 Connect( wxEVT_KILL_FOCUS
, wxFocusEventHandler(wxGenericHyperlinkCtrl::OnFocus
) );
95 Connect( wxEVT_CHAR
, wxKeyEventHandler(wxGenericHyperlinkCtrl::OnChar
) );
96 Connect( wxEVT_LEAVE_WINDOW
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeaveWindow
) );
98 Connect( wxEVT_LEFT_DOWN
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftDown
) );
99 Connect( wxEVT_LEFT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftUp
) );
100 Connect( wxEVT_MOTION
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnMotion
) );
102 ConnectMenuHandlers();
107 void wxGenericHyperlinkCtrl::Init()
114 m_normalColour
= *wxBLUE
;
115 m_hoverColour
= *wxRED
;
116 m_visitedColour
= wxColour("#551a8b");
119 void wxGenericHyperlinkCtrl::ConnectMenuHandlers()
121 // Connect the event handlers for the context menu.
122 Connect( wxEVT_RIGHT_UP
, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnRightUp
) );
123 Connect( wxHYPERLINK_POPUP_COPY_ID
, wxEVT_COMMAND_MENU_SELECTED
,
124 wxCommandEventHandler(wxGenericHyperlinkCtrl::OnPopUpCopy
) );
127 wxSize
wxGenericHyperlinkCtrl::DoGetBestClientSize() const
129 wxClientDC
dc((wxWindow
*)this);
130 return dc
.GetTextExtent(GetLabel());
134 void wxGenericHyperlinkCtrl::SetNormalColour(const wxColour
&colour
)
136 m_normalColour
= colour
;
139 SetForegroundColour(m_normalColour
);
144 void wxGenericHyperlinkCtrl::SetVisitedColour(const wxColour
&colour
)
146 m_visitedColour
= colour
;
149 SetForegroundColour(m_visitedColour
);
154 void wxGenericHyperlinkCtrl::DoContextMenu(const wxPoint
&pos
)
156 wxMenu
*menuPopUp
= new wxMenu(wxEmptyString
, wxMENU_TEAROFF
);
157 menuPopUp
->Append(wxHYPERLINK_POPUP_COPY_ID
, _("&Copy URL"));
158 PopupMenu( menuPopUp
, pos
);
162 wxRect
wxGenericHyperlinkCtrl::GetLabelRect() const
164 // our best size is always the size of the label without borders
165 wxSize
c(GetClientSize()), b(GetBestSize());
168 // the label is always centered vertically
169 offset
.y
= (c
.GetHeight()-b
.GetHeight())/2;
171 if (HasFlag(wxHL_ALIGN_CENTRE
))
172 offset
.x
= (c
.GetWidth()-b
.GetWidth())/2;
173 else if (HasFlag(wxHL_ALIGN_RIGHT
))
174 offset
.x
= c
.GetWidth()-b
.GetWidth();
175 else if (HasFlag(wxHL_ALIGN_LEFT
))
177 return wxRect(offset
, b
);
182 // ----------------------------------------------------------------------------
183 // wxGenericHyperlinkCtrl - event handlers
184 // ----------------------------------------------------------------------------
186 void wxGenericHyperlinkCtrl::OnPaint(wxPaintEvent
& WXUNUSED(event
))
189 dc
.SetFont(GetFont());
190 dc
.SetTextForeground(GetForegroundColour());
191 dc
.SetTextBackground(GetBackgroundColour());
193 dc
.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
196 wxRendererNative::Get().DrawFocusRect(this, dc
, GetClientRect(), wxCONTROL_SELECTED
);
200 void wxGenericHyperlinkCtrl::OnFocus(wxFocusEvent
& event
)
206 void wxGenericHyperlinkCtrl::OnChar(wxKeyEvent
& event
)
208 switch (event
.m_keyCode
)
214 case WXK_NUMPAD_SPACE
:
215 SetForegroundColour(m_visitedColour
);
222 void wxGenericHyperlinkCtrl::OnLeftDown(wxMouseEvent
& event
)
224 // the left click must start from the hyperlink rect
225 m_clicking
= GetLabelRect().Contains(event
.GetPosition());
228 void wxGenericHyperlinkCtrl::OnLeftUp(wxMouseEvent
& event
)
230 // the click must be started and ended in the hyperlink rect
231 if (!m_clicking
|| !GetLabelRect().Contains(event
.GetPosition()))
234 SetForegroundColour(m_visitedColour
);
242 void wxGenericHyperlinkCtrl::OnRightUp(wxMouseEvent
& event
)
244 if( GetWindowStyle() & wxHL_CONTEXTMENU
)
245 if ( GetLabelRect().Contains(event
.GetPosition()) )
246 DoContextMenu(wxPoint(event
.m_x
, event
.m_y
));
249 void wxGenericHyperlinkCtrl::OnMotion(wxMouseEvent
& event
)
251 wxRect textrc
= GetLabelRect();
253 if (textrc
.Contains(event
.GetPosition()))
255 SetCursor(wxCursor(wxCURSOR_HAND
));
256 SetForegroundColour(m_hoverColour
);
262 SetCursor(*wxSTANDARD_CURSOR
);
263 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
269 void wxGenericHyperlinkCtrl::OnLeaveWindow(wxMouseEvent
& WXUNUSED(event
) )
271 // NB: when the label rect and the client size rect have the same
272 // height this function is indispensable to remove the "rollover"
273 // effect as the OnMotion() event handler could not be called
274 // in that case moving the mouse out of the label vertically...
278 SetCursor(*wxSTANDARD_CURSOR
);
279 SetForegroundColour(!m_visited
? m_normalColour
: m_visitedColour
);
285 void wxGenericHyperlinkCtrl::OnPopUpCopy( wxCommandEvent
& WXUNUSED(event
) )
288 if (!wxTheClipboard
->Open())
291 wxTextDataObject
*data
= new wxTextDataObject( m_url
);
292 wxTheClipboard
->SetData( data
);
293 wxTheClipboard
->Close();
294 #endif // wxUSE_CLIPBOARD
297 #endif // wxUSE_HYPERLINKCTRL