Applied Patch #1502801: Missing Clone() functions from some events
[wxWidgets.git] / include / wx / hyperlink.h
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
6 // Created: 04/02/2005
7 // RCS-ID: $Id$
8 // Copyright: (c) 2005 David Norris
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_HYPERLINK_H__
13 #define _WX_HYPERLINK_H__
14
15 #include "wx/defs.h"
16
17 #if wxUSE_HYPERLINKCTRL
18
19 #include "wx/control.h"
20
21 // ----------------------------------------------------------------------------
22 // constants
23 // ----------------------------------------------------------------------------
24
25 #define wxHL_CONTEXTMENU 0x0001
26 #define wxHL_DEFAULT_STYLE wxHL_CONTEXTMENU|wxNO_BORDER
27
28 extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxHyperlinkCtrlNameStr[];
29
30
31 // ----------------------------------------------------------------------------
32 // wxHyperlinkCtrl
33 // ----------------------------------------------------------------------------
34
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.
39 //
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.
43 //
44 // Use the EVT_HYPERLINK() to catch link events.
45 class WXDLLIMPEXP_ADV wxHyperlinkCtrl : public wxControl
46 {
47 public:
48 // Default constructor (for two-step construction).
49 wxHyperlinkCtrl() { }
50
51 // Constructor.
52 wxHyperlinkCtrl(wxWindow *parent,
53 wxWindowID id,
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)
59 {
60 (void)Create(parent, id, label, url, pos, size, style, name);
61 }
62
63 // Creation function (for two-step construction).
64 bool Create(wxWindow *parent,
65 wxWindowID id,
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);
71
72
73 // get/set
74 wxColour GetHoverColour() const { return m_hoverColour; }
75 void SetHoverColour(const wxColour &colour) { m_hoverColour = colour; }
76
77 wxColour GetNormalColour() const { return m_normalColour; }
78 void SetNormalColour(const wxColour &colour);
79
80 wxColour GetVisitedColour() const { return m_visitedColour; }
81 void SetVisitedColour(const wxColour &colour);
82
83 wxString GetURL() const { return m_url; }
84 void SetURL (const wxString &url) { m_url=url; }
85
86 void SetVisited(bool visited = true) { m_visited=visited; }
87 bool GetVisited() const { return m_visited; }
88
89 // NOTE: also wxWindow::Set/GetLabel, wxWindow::Set/GetBackgroundColour,
90 // wxWindow::Get/SetFont, wxWindow::Get/SetCursor are important !
91
92
93 protected:
94 // event handlers
95
96 // Renders the hyperlink.
97 void OnPaint(wxPaintEvent& event);
98
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);
103
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);
108
109 // Changes the cursor to a hand, if the mouse is inside the label's
110 // bounding box.
111 void OnEnterWindow(wxMouseEvent& event);
112
113 // Changes the cursor back to the default, if necessary.
114 void OnLeaveWindow(wxMouseEvent& event);
115
116 // handles "Copy URL" menuitem
117 void OnPopUpCopy(wxCommandEvent& event);
118
119
120 // overridden base class virtuals
121
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;
126
127 // creates a context menu with "Copy URL" menuitem
128 virtual void DoContextMenu(const wxPoint &);
129
130 private:
131 // URL associated with the link. This is transmitted inside
132 // the HyperlinkEvent fired when the user clicks on the label.
133 wxString m_url;
134
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;
141
142 // True if the mouse cursor is inside the label's bounding box.
143 bool m_rollover;
144
145 // True if the link has been clicked before.
146 bool m_visited;
147
148 // True if a click is in progress (left button down) and the click
149 // originated inside the label's bounding box.
150 bool m_clicking;
151
152 private:
153 DECLARE_DYNAMIC_CLASS(wxHyperlinkCtrl)
154 DECLARE_EVENT_TABLE()
155 };
156
157
158 // ----------------------------------------------------------------------------
159 // wxHyperlinkEvent
160 // ----------------------------------------------------------------------------
161
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()
166
167 //
168 // An event fired when the user clicks on the label in a hyperlink control.
169 // See HyperlinkControl for details.
170 //
171 class WXDLLIMPEXP_ADV wxHyperlinkEvent : public wxCommandEvent
172 {
173 public:
174 wxHyperlinkEvent() {}
175 wxHyperlinkEvent(wxObject *generator, wxWindowID id, const wxString& url)
176 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK, id),
177 m_url(url)
178 {
179 SetEventObject(generator);
180 }
181
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; }
186
187 // default copy ctor, assignment operator and dtor are ok
188 virtual wxEvent *Clone() const { return new wxHyperlinkEvent(*this); }
189
190 private:
191
192 // URL associated with the hyperlink control that the used clicked on.
193 wxString m_url;
194
195 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHyperlinkEvent)
196 };
197
198
199 // ----------------------------------------------------------------------------
200 // event types and macros
201 // ----------------------------------------------------------------------------
202
203 typedef void (wxEvtHandler::*wxHyperlinkEventFunction)(wxHyperlinkEvent&);
204
205 #define wxHyperlinkEventHandler(func) \
206 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
207
208 #define EVT_HYPERLINK(id, fn) \
209 wx__DECLARE_EVT1(wxEVT_COMMAND_HYPERLINK, id, wxHyperlinkEventHandler(fn))
210
211 #ifdef _WX_DEFINE_DATE_EVENTS_
212 DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK)
213
214 IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkEvent, wxCommandEvent)
215 #endif
216
217
218 #endif // wxUSE_HYPERLINKCTRL
219
220 #endif // _WX_HYPERLINK_H__