1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/hyperlink.h
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__
17 #if wxUSE_HYPERLINKCTRL
19 #include "wx/control.h"
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #define wxHL_CONTEXTMENU 0x0001
26 #define wxHL_DEFAULT_STYLE wxHL_CONTEXTMENU|wxNO_BORDER
28 extern WXDLLIMPEXP_DATA_ADV(const wxChar
) wxHyperlinkCtrlNameStr
[];
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 // A static text control that emulates a hyperlink. The link is displayed
36 // in an appropriate text style, derived from the control's normal font.
37 // When the mouse rolls over the link, the cursor changes to a hand and the
38 // link's color changes to the active color.
40 // Clicking on the link does not launch a web browser; instead, a
41 // HyperlinkEvent is fired. The event propagates upward until it is caught,
42 // just like a wxCommandEvent.
44 // Use the EVT_HYPERLINK() to catch link events.
45 class WXDLLIMPEXP_ADV wxHyperlinkCtrl
: public wxControl
48 // Default constructor (for two-step construction).
52 wxHyperlinkCtrl(wxWindow
*parent
,
54 const wxString
& label
, const wxString
& url
,
55 const wxPoint
& pos
= wxDefaultPosition
,
56 const wxSize
& size
= wxDefaultSize
,
57 long style
= wxHL_DEFAULT_STYLE
,
58 const wxString
& name
= wxHyperlinkCtrlNameStr
)
60 (void)Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
63 // Creation function (for two-step construction).
64 bool Create(wxWindow
*parent
,
66 const wxString
& label
, const wxString
& url
,
67 const wxPoint
& pos
= wxDefaultPosition
,
68 const wxSize
& size
= wxDefaultSize
,
69 long style
= wxHL_DEFAULT_STYLE
,
70 const wxString
& name
= wxHyperlinkCtrlNameStr
);
74 wxColour
GetHoverColour() const { return m_hoverColour
; }
75 void SetHoverColour(const wxColour
&colour
) { m_hoverColour
= colour
; }
77 wxColour
GetNormalColour() const { return m_normalColour
; }
78 void SetNormalColour(const wxColour
&colour
);
80 wxColour
GetVisitedColour() const { return m_visitedColour
; }
81 void SetVisitedColour(const wxColour
&colour
);
83 wxString
GetURL() const { return m_url
; }
84 void SetURL (const wxString
&url
) { m_url
=url
; }
86 void SetVisited(bool visited
= true) { m_visited
=visited
; }
87 bool GetVisited() const { return m_visited
; }
89 // NOTE: also wxWindow::Set/GetLabel, wxWindow::Set/GetBackgroundColour,
90 // wxWindow::Get/SetFont, wxWindow::Get/SetCursor are important !
96 // Renders the hyperlink.
97 void OnPaint(wxPaintEvent
& event
);
99 // If the click originates inside the bounding box of the label,
100 // a flag is set so that an event will be fired when the left
101 // button is released.
102 void OnLeftDown(wxMouseEvent
& event
);
104 // If the click both originated and finished inside the bounding box
105 // of the label, a HyperlinkEvent is fired.
106 void OnLeftUp(wxMouseEvent
& event
);
107 void OnRightUp(wxMouseEvent
& event
);
109 // Changes the cursor to a hand, if the mouse is inside the label's
111 void OnEnterWindow(wxMouseEvent
& event
);
113 // Changes the cursor back to the default, if necessary.
114 void OnLeaveWindow(wxMouseEvent
& event
);
116 // handles "Copy URL" menuitem
117 void OnPopUpCopy(wxCommandEvent
& event
);
120 // overridden base class virtuals
122 // Returns the best size for the window, which is the size needed
123 // to display the text label.
124 virtual void DoGetSize(int *width
, int *height
) const;
125 virtual wxSize
DoGetBestSize() const;
127 // creates a context menu with "Copy URL" menuitem
128 virtual void DoContextMenu(const wxPoint
&);
131 // URL associated with the link. This is transmitted inside
132 // the HyperlinkEvent fired when the user clicks on the label.
135 // Foreground colours for various link types.
136 // NOTE: wxWindow::m_backgroundColour is used for background,
137 // wxWindow::m_foregroundColour is used to render non-visited links
138 wxColour m_hoverColour
;
139 wxColour m_normalColour
;
140 wxColour m_visitedColour
;
142 // True if the mouse cursor is inside the label's bounding box.
145 // True if the link has been clicked before.
148 // True if a click is in progress (left button down) and the click
149 // originated inside the label's bounding box.
153 DECLARE_DYNAMIC_CLASS(wxHyperlinkCtrl
)
154 DECLARE_EVENT_TABLE()
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 // Declare an event identifier.
163 BEGIN_DECLARE_EVENT_TYPES()
164 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_HYPERLINK
, 3700)
165 END_DECLARE_EVENT_TYPES()
168 // An event fired when the user clicks on the label in a hyperlink control.
169 // See HyperlinkControl for details.
171 class WXDLLIMPEXP_ADV wxHyperlinkEvent
: public wxCommandEvent
174 wxHyperlinkEvent() {}
175 wxHyperlinkEvent(wxObject
*generator
, wxWindowID id
, const wxString
& url
)
176 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK
, id
),
179 SetEventObject(generator
);
182 // Returns the URL associated with the hyperlink control
183 // that the user clicked on.
184 wxString
GetURL() const { return m_url
; }
185 void SetURL(const wxString
&url
) { m_url
=url
; }
187 // default copy ctor, assignment operator and dtor are ok
188 virtual wxEvent
*Clone() const { return new wxHyperlinkEvent(*this); }
192 // URL associated with the hyperlink control that the used clicked on.
195 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHyperlinkEvent
)
199 // ----------------------------------------------------------------------------
200 // event types and macros
201 // ----------------------------------------------------------------------------
203 typedef void (wxEvtHandler::*wxHyperlinkEventFunction
)(wxHyperlinkEvent
&);
205 #define wxHyperlinkEventHandler(func) \
206 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
208 #define EVT_HYPERLINK(id, fn) \
209 wx__DECLARE_EVT1(wxEVT_COMMAND_HYPERLINK, id, wxHyperlinkEventHandler(fn))
211 #ifdef _WX_DEFINE_DATE_EVENTS_
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
214 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkEvent
, wxCommandEvent
)
218 #endif // wxUSE_HYPERLINKCTRL
220 #endif // _WX_HYPERLINK_H__