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_ALIGN_LEFT 0x0002
27 #define wxHL_ALIGN_RIGHT 0x0004
28 #define wxHL_ALIGN_CENTRE 0x0008
29 #define wxHL_DEFAULT_STYLE (wxHL_CONTEXTMENU|wxNO_BORDER|wxHL_ALIGN_CENTRE)
31 extern WXDLLIMPEXP_DATA_ADV(const wxChar
) wxHyperlinkCtrlNameStr
[];
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // A static text control that emulates a hyperlink. The link is displayed
39 // in an appropriate text style, derived from the control's normal font.
40 // When the mouse rolls over the link, the cursor changes to a hand and the
41 // link's color changes to the active color.
43 // Clicking on the link does not launch a web browser; instead, a
44 // HyperlinkEvent is fired. The event propagates upward until it is caught,
45 // just like a wxCommandEvent.
47 // Use the EVT_HYPERLINK() to catch link events.
48 class WXDLLIMPEXP_ADV wxHyperlinkCtrl
: public wxControl
51 // Default constructor (for two-step construction).
55 wxHyperlinkCtrl(wxWindow
*parent
,
57 const wxString
& label
, const wxString
& url
,
58 const wxPoint
& pos
= wxDefaultPosition
,
59 const wxSize
& size
= wxDefaultSize
,
60 long style
= wxHL_DEFAULT_STYLE
,
61 const wxString
& name
= wxHyperlinkCtrlNameStr
)
63 (void)Create(parent
, id
, label
, url
, pos
, size
, style
, name
);
66 // Creation function (for two-step construction).
67 bool Create(wxWindow
*parent
,
69 const wxString
& label
, const wxString
& url
,
70 const wxPoint
& pos
= wxDefaultPosition
,
71 const wxSize
& size
= wxDefaultSize
,
72 long style
= wxHL_DEFAULT_STYLE
,
73 const wxString
& name
= wxHyperlinkCtrlNameStr
);
77 wxColour
GetHoverColour() const { return m_hoverColour
; }
78 void SetHoverColour(const wxColour
&colour
) { m_hoverColour
= colour
; }
80 wxColour
GetNormalColour() const { return m_normalColour
; }
81 void SetNormalColour(const wxColour
&colour
);
83 wxColour
GetVisitedColour() const { return m_visitedColour
; }
84 void SetVisitedColour(const wxColour
&colour
);
86 wxString
GetURL() const { return m_url
; }
87 void SetURL (const wxString
&url
) { m_url
=url
; }
89 void SetVisited(bool visited
= true) { m_visited
=visited
; }
90 bool GetVisited() const { return m_visited
; }
92 // NOTE: also wxWindow::Set/GetLabel, wxWindow::Set/GetBackgroundColour,
93 // wxWindow::Get/SetFont, wxWindow::Get/SetCursor are important !
99 // Renders the hyperlink.
100 void OnPaint(wxPaintEvent
& event
);
102 // Returns the wxRect of the label of this hyperlink.
103 // This is different from the clientsize's rectangle when
104 // clientsize != bestsize and this rectangle is influenced
105 // by the alignment of the label (wxHL_ALIGN_*).
106 wxRect
GetLabelRect() const;
108 // If the click originates inside the bounding box of the label,
109 // a flag is set so that an event will be fired when the left
110 // button is released.
111 void OnLeftDown(wxMouseEvent
& event
);
113 // If the click both originated and finished inside the bounding box
114 // of the label, a HyperlinkEvent is fired.
115 void OnLeftUp(wxMouseEvent
& event
);
116 void OnRightUp(wxMouseEvent
& event
);
118 // Changes the cursor to a hand, if the mouse is inside the label's
120 void OnMotion(wxMouseEvent
& event
);
122 // Changes the cursor back to the default, if necessary.
123 void OnLeaveWindow(wxMouseEvent
& event
);
125 // handles "Copy URL" menuitem
126 void OnPopUpCopy(wxCommandEvent
& event
);
128 // Refreshes the control to update label's position if necessary
129 void OnSize(wxSizeEvent
& event
);
132 // overridden base class virtuals
134 // Returns the best size for the window, which is the size needed
135 // to display the text label.
136 virtual wxSize
DoGetBestSize() const;
138 // creates a context menu with "Copy URL" menuitem
139 virtual void DoContextMenu(const wxPoint
&);
142 // URL associated with the link. This is transmitted inside
143 // the HyperlinkEvent fired when the user clicks on the label.
146 // Foreground colours for various link types.
147 // NOTE: wxWindow::m_backgroundColour is used for background,
148 // wxWindow::m_foregroundColour is used to render non-visited links
149 wxColour m_hoverColour
;
150 wxColour m_normalColour
;
151 wxColour m_visitedColour
;
153 // True if the mouse cursor is inside the label's bounding box.
156 // True if the link has been clicked before.
159 // True if a click is in progress (left button down) and the click
160 // originated inside the label's bounding box.
164 DECLARE_DYNAMIC_CLASS(wxHyperlinkCtrl
)
165 DECLARE_EVENT_TABLE()
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 // Declare an event identifier.
174 BEGIN_DECLARE_EVENT_TYPES()
175 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV
, wxEVT_COMMAND_HYPERLINK
, 3700)
176 END_DECLARE_EVENT_TYPES()
179 // An event fired when the user clicks on the label in a hyperlink control.
180 // See HyperlinkControl for details.
182 class WXDLLIMPEXP_ADV wxHyperlinkEvent
: public wxCommandEvent
185 wxHyperlinkEvent() {}
186 wxHyperlinkEvent(wxObject
*generator
, wxWindowID id
, const wxString
& url
)
187 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK
, id
),
190 SetEventObject(generator
);
193 // Returns the URL associated with the hyperlink control
194 // that the user clicked on.
195 wxString
GetURL() const { return m_url
; }
196 void SetURL(const wxString
&url
) { m_url
=url
; }
198 // default copy ctor, assignment operator and dtor are ok
199 virtual wxEvent
*Clone() const { return new wxHyperlinkEvent(*this); }
203 // URL associated with the hyperlink control that the used clicked on.
206 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHyperlinkEvent
)
210 // ----------------------------------------------------------------------------
211 // event types and macros
212 // ----------------------------------------------------------------------------
214 typedef void (wxEvtHandler::*wxHyperlinkEventFunction
)(wxHyperlinkEvent
&);
216 #define wxHyperlinkEventHandler(func) \
217 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
219 #define EVT_HYPERLINK(id, fn) \
220 wx__DECLARE_EVT1(wxEVT_COMMAND_HYPERLINK, id, wxHyperlinkEventHandler(fn))
222 #ifdef _WX_DEFINE_DATE_EVENTS_
223 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK
)
225 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkEvent
, wxCommandEvent
)
229 #endif // wxUSE_HYPERLINKCTRL
231 #endif // _WX_HYPERLINK_H__