1 /////////////////////////////////////////////////////////////////////////////
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 #ifndef _WX_HYPERLINK_H__
13 #define _WX_HYPERLINK_H__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "hyperlink.h"
21 #if wxUSE_HYPERLINKCTRL
23 #include "wx/control.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 #define wxHL_CONTEXTMENU 0x0001
30 #define wxHL_DEFAULT_STYLE wxHL_CONTEXTMENU|wxNO_BORDER
32 extern WXDLLEXPORT_DATA(const wxChar
) wxHyperlinkCtrlNameStr
[];
35 // ----------------------------------------------------------------------------
37 // ----------------------------------------------------------------------------
39 // A static text control that emulates a hyperlink. The link is displayed
40 // in an appropriate text style, derived from the control's normal font.
41 // When the mouse rolls over the link, the cursor changes to a hand and the
42 // link's color changes to the active color.
44 // Clicking on the link does not launch a web browser; instead, a
45 // HyperlinkEvent is fired. The event propagates upward until it is caught,
46 // just like a wxCommandEvent.
48 // Use the EVT_HYPERLINK() to catch link events.
49 class WXDLLIMPEXP_CORE wxHyperlinkCtrl
: public wxControl
52 // Default constructor (for two-step construction).
56 wxHyperlinkCtrl(wxWindow
*parent
,
58 const wxString
& label
, const wxString
& url
,
59 const wxPoint
& pos
= wxDefaultPosition
,
60 const wxSize
& size
= wxDefaultSize
,
61 long style
= wxHL_DEFAULT_STYLE
,
62 const wxString
& name
= wxHyperlinkCtrlNameStr
)
64 (void)Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
67 // Creation function (for two-step construction).
68 bool Create(wxWindow
*parent
,
70 const wxString
& label
, const wxString
& url
,
71 const wxPoint
& pos
= wxDefaultPosition
,
72 const wxSize
& size
= wxDefaultSize
,
73 long style
= wxHL_DEFAULT_STYLE
,
74 const wxString
& name
= wxHyperlinkCtrlNameStr
);
78 wxColour
GetHoverColour() const { return m_hoverColour
; }
79 void SetHoverColour(const wxColour
&colour
) { m_hoverColour
= colour
; }
81 wxColour
GetNormalColour() const { return m_normalColour
; }
82 void SetNormalColour(const wxColour
&colour
);
84 wxColour
GetVisitedColour() const { return m_visitedColour
; }
85 void SetVisitedColour(const wxColour
&colour
);
87 wxString
GetURL() const { return m_url
; }
88 void SetURL (const wxString
&url
) { m_url
=url
; }
90 void SetVisited(bool visited
= true) { m_visited
=visited
; }
91 bool GetVisited() const { return m_visited
; }
93 // NOTE: also wxWindow::Set/GetLabel, wxWindow::Set/GetBackgroundColour,
94 // wxWindow::Get/SetFont, wxWindow::Get/SetCursor are important !
100 // Renders the hyperlink.
101 void OnPaint(wxPaintEvent
& event
);
103 // If the click originates inside the bounding box of the label,
104 // a flag is set so that an event will be fired when the left
105 // button is released.
106 void OnLeftDown(wxMouseEvent
& event
);
108 // If the click both originated and finished inside the bounding box
109 // of the label, a HyperlinkEvent is fired.
110 void OnLeftUp(wxMouseEvent
& event
);
111 void OnRightUp(wxMouseEvent
& event
);
113 // Changes the cursor to a hand, if the mouse is inside the label's
115 void OnEnterWindow(wxMouseEvent
& event
);
117 // Changes the cursor back to the default, if necessary.
118 void OnLeaveWindow(wxMouseEvent
& event
);
120 // handles "Copy URL" menuitem
121 void OnPopUpCopy(wxCommandEvent
& event
);
124 // overridden base class virtuals
126 // Returns the best size for the window, which is the size needed
127 // to display the text label.
128 virtual void DoGetSize(int *width
, int *height
) const;
129 virtual wxSize
DoGetBestSize() const;
131 // creates a context menu with "Copy URL" menuitem
132 virtual void DoContextMenu(const wxPoint
&);
135 // URL associated with the link. This is transmitted inside
136 // the HyperlinkEvent fired when the user clicks on the label.
139 // Foreground colours for various link types.
140 // NOTE: wxWindow::m_backgroundColour is used for background,
141 // wxWindow::m_foregroundColour is used to render non-visited links
142 wxColour m_hoverColour
;
143 wxColour m_normalColour
;
144 wxColour m_visitedColour
;
146 // True if the mouse cursor is inside the label's bounding box.
149 // True if the link has been clicked before.
152 // True if a click is in progress (left button down) and the click
153 // originated inside the label's bounding box.
157 DECLARE_DYNAMIC_CLASS(wxHyperlinkCtrl
)
158 DECLARE_EVENT_TABLE()
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
166 // Declare an event identifier.
167 BEGIN_DECLARE_EVENT_TYPES()
168 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_CORE
, wxEVT_COMMAND_HYPERLINK
, 3700)
169 END_DECLARE_EVENT_TYPES()
172 // An event fired when the user clicks on the label in a hyperlink control.
173 // See HyperlinkControl for details.
175 class WXDLLIMPEXP_CORE wxHyperlinkEvent
: public wxCommandEvent
179 wxHyperlinkEvent() {}
180 wxHyperlinkEvent(wxObject
*generator
, wxWindowID id
, const wxString
& url
)
181 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK
, id
), m_url(url
)
182 { SetEventObject(generator
); }
184 // Returns the URL associated with the hyperlink control
185 // that the user clicked on.
186 wxString
GetURL() const { return m_url
; }
187 void SetURL(const wxString
&url
) { m_url
=url
; }
191 // URL associated with the hyperlink control that the used clicked on.
195 // Define a typedef for event handler functions.
196 typedef void (wxEvtHandler::*wxHyperlinkEventFunction
)(wxHyperlinkEvent
&);
198 // Define an event table macro.
199 #define EVT_HYPERLINK(id, fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_COMMAND_HYPERLINK, \
200 id, id, (wxObjectEventFunction) (wxEventFunction) \
201 wxStaticCastEvent(wxHyperlinkEventFunction, &fn), (wxObject *) NULL),
204 #define wxHyperlinkEventHandler(func) \
205 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
207 #endif // wxUSE_HYPERLINKCTRL
209 #endif // _WX_HYPERLINK_H__