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
175 wxHyperlinkEvent() {}
176 wxHyperlinkEvent(wxObject
*generator
, wxWindowID id
, const wxString
& url
)
177 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK
, id
), m_url(url
)
178 { SetEventObject(generator
); }
180 // Returns the URL associated with the hyperlink control
181 // that the user clicked on.
182 wxString
GetURL() const { return m_url
; }
183 void SetURL(const wxString
&url
) { m_url
=url
; }
187 // URL associated with the hyperlink control that the used clicked on.
191 // Define a typedef for event handler functions.
192 typedef void (wxEvtHandler::*wxHyperlinkEventFunction
)(wxHyperlinkEvent
&);
194 // Define an event table macro.
195 #define EVT_HYPERLINK(id, fn) DECLARE_EVENT_TABLE_ENTRY(wxEVT_COMMAND_HYPERLINK, \
196 id, id, (wxObjectEventFunction) (wxEventFunction) \
197 wxStaticCastEvent(wxHyperlinkEventFunction, &fn), (wxObject *) NULL),
200 #define wxHyperlinkEventHandler(func) \
201 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
203 #endif // wxUSE_HYPERLINKCTRL
205 #endif // _WX_HYPERLINK_H__