]>
Commit | Line | Data |
---|---|---|
17e91437 | 1 | ///////////////////////////////////////////////////////////////////////////// |
98159dd8 | 2 | // Name: wx/hyperlink.h |
17e91437 VZ |
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 | ||
17e91437 VZ |
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 WXDLLEXPORT_DATA(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_CORE 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_CORE, 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_CORE wxHyperlinkEvent : public wxCommandEvent | |
172 | { | |
173 | public: | |
174 | ||
175 | wxHyperlinkEvent() {} | |
176 | wxHyperlinkEvent(wxObject *generator, wxWindowID id, const wxString& url) | |
177 | : wxCommandEvent(wxEVT_COMMAND_HYPERLINK, id), m_url(url) | |
178 | { SetEventObject(generator); } | |
179 | ||
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; } | |
184 | ||
185 | private: | |
186 | ||
187 | // URL associated with the hyperlink control that the used clicked on. | |
188 | wxString m_url; | |
189 | }; | |
190 | ||
191 | // Define a typedef for event handler functions. | |
192 | typedef void (wxEvtHandler::*wxHyperlinkEventFunction)(wxHyperlinkEvent&); | |
193 | ||
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), | |
198 | ||
199 | // Newer event macro | |
200 | #define wxHyperlinkEventHandler(func) \ | |
201 | (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func) | |
202 | ||
203 | #endif // wxUSE_HYPERLINKCTRL | |
204 | ||
205 | #endif // _WX_HYPERLINK_H__ |