]> git.saurik.com Git - wxWidgets.git/blame - include/wx/hyperlink.h
always let GtkTextView have mouse release events to avoid assertion failures in gtk_t...
[wxWidgets.git] / include / wx / hyperlink.h
CommitLineData
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
870597ef 28extern WXDLLIMPEXP_DATA_ADV(const wxChar) wxHyperlinkCtrlNameStr[];
17e91437
VZ
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.
870597ef 45class WXDLLIMPEXP_ADV wxHyperlinkCtrl : public wxControl
17e91437
VZ
46{
47public:
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
93protected:
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
130private:
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
152private:
153 DECLARE_DYNAMIC_CLASS(wxHyperlinkCtrl)
154 DECLARE_EVENT_TABLE()
155};
156
157
158// ----------------------------------------------------------------------------
159// wxHyperlinkEvent
160// ----------------------------------------------------------------------------
161
162// Declare an event identifier.
163BEGIN_DECLARE_EVENT_TYPES()
870597ef 164 DECLARE_EXPORTED_EVENT_TYPE(WXDLLIMPEXP_ADV, wxEVT_COMMAND_HYPERLINK, 3700)
17e91437
VZ
165END_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//
870597ef 171class WXDLLIMPEXP_ADV wxHyperlinkEvent : public wxCommandEvent
17e91437
VZ
172{
173public:
17e91437
VZ
174 wxHyperlinkEvent() {}
175 wxHyperlinkEvent(wxObject *generator, wxWindowID id, const wxString& url)
258b2ca6
RD
176 : wxCommandEvent(wxEVT_COMMAND_HYPERLINK, id),
177 m_url(url)
178 {
179 SetEventObject(generator);
180 }
17e91437
VZ
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
258b2ca6
RD
187 // default copy ctor, assignment operator and dtor are ok
188 virtual wxEvent *Clone() const { return new wxHyperlinkEvent(*this); }
189
17e91437
VZ
190private:
191
192 // URL associated with the hyperlink control that the used clicked on.
193 wxString m_url;
258b2ca6
RD
194
195 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxHyperlinkEvent)
17e91437
VZ
196};
197
17e91437 198
258b2ca6
RD
199// ----------------------------------------------------------------------------
200// event types and macros
201// ----------------------------------------------------------------------------
202
203typedef void (wxEvtHandler::*wxHyperlinkEventFunction)(wxHyperlinkEvent&);
17e91437 204
17e91437
VZ
205#define wxHyperlinkEventHandler(func) \
206 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxHyperlinkEventFunction, &func)
207
258b2ca6
RD
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
17e91437
VZ
218#endif // wxUSE_HYPERLINKCTRL
219
220#endif // _WX_HYPERLINK_H__