]> git.saurik.com Git - wxWidgets.git/blob - src/generic/hyperlinkg.cpp
Remove duplicate item about Trac changes from release tech note.
[wxWidgets.git] / src / generic / hyperlinkg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/hyperlinkg.cpp
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 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_HYPERLINKCTRL
28
29 //---------------------------------------------------------------------------
30 // Includes
31 //---------------------------------------------------------------------------
32
33 #include "wx/hyperlink.h"
34
35 #ifndef WX_PRECOMP
36 #include "wx/utils.h" // for wxLaunchDefaultBrowser
37 #include "wx/dcclient.h"
38 #include "wx/menu.h"
39 #include "wx/log.h"
40 #include "wx/dataobj.h"
41 #endif
42
43 #include "wx/clipbrd.h"
44 #include "wx/renderer.h"
45
46 // ============================================================================
47 // implementation
48 // ============================================================================
49
50 // reserved for internal use only
51 #define wxHYPERLINK_POPUP_COPY_ID 16384
52
53 // ----------------------------------------------------------------------------
54 // wxGenericHyperlinkCtrl
55 // ----------------------------------------------------------------------------
56
57 bool wxGenericHyperlinkCtrl::Create(wxWindow *parent, wxWindowID id,
58 const wxString& label, const wxString& url, const wxPoint& pos,
59 const wxSize& size, long style, const wxString& name)
60 {
61 // do validation checks:
62 CheckParams(label, url, style);
63
64 if ((style & wxHL_ALIGN_LEFT) == 0)
65 style |= wxFULL_REPAINT_ON_RESIZE;
66
67 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
68 return false;
69
70 // set to non empty strings both the url and the label
71 SetURL(url.empty() ? label : url);
72 SetLabel(label.empty() ? url : label);
73
74 Init();
75 SetForegroundColour(m_normalColour);
76
77 // by default the font of an hyperlink control is underlined
78 wxFont f = GetFont();
79 f.SetUnderlined(true);
80 SetFont(f);
81
82 SetInitialSize(size);
83
84
85 // connect our event handlers:
86 // NOTE: since this class is the base class of the GTK+'s native implementation
87 // of wxHyperlinkCtrl, we cannot use the static macros in BEGIN/END_EVENT_TABLE
88 // blocks, otherwise the GTK+'s native impl of wxHyperlinkCtrl would not
89 // behave correctly (as we intercept events doing things which interfere
90 // with GTK+'s native handling):
91
92 Connect( wxEVT_PAINT, wxPaintEventHandler(wxGenericHyperlinkCtrl::OnPaint) );
93 Connect( wxEVT_SET_FOCUS, wxFocusEventHandler(wxGenericHyperlinkCtrl::OnFocus) );
94 Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler(wxGenericHyperlinkCtrl::OnFocus) );
95 Connect( wxEVT_CHAR, wxKeyEventHandler(wxGenericHyperlinkCtrl::OnChar) );
96 Connect( wxEVT_LEAVE_WINDOW, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeaveWindow) );
97
98 Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftDown) );
99 Connect( wxEVT_LEFT_UP, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnLeftUp) );
100 Connect( wxEVT_MOTION, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnMotion) );
101
102 ConnectMenuHandlers();
103
104 return true;
105 }
106
107 void wxGenericHyperlinkCtrl::Init()
108 {
109 m_rollover = false;
110 m_clicking = false;
111 m_visited = false;
112
113 // colours
114 m_normalColour = *wxBLUE;
115 m_hoverColour = *wxRED;
116 m_visitedColour = wxColour("#551a8b");
117 }
118
119 void wxGenericHyperlinkCtrl::ConnectMenuHandlers()
120 {
121 // Connect the event handlers for the context menu.
122 Connect( wxEVT_RIGHT_UP, wxMouseEventHandler(wxGenericHyperlinkCtrl::OnRightUp) );
123 Connect( wxHYPERLINK_POPUP_COPY_ID, wxEVT_COMMAND_MENU_SELECTED,
124 wxCommandEventHandler(wxGenericHyperlinkCtrl::OnPopUpCopy) );
125 }
126
127 wxSize wxGenericHyperlinkCtrl::DoGetBestClientSize() const
128 {
129 wxClientDC dc((wxWindow *)this);
130 return dc.GetTextExtent(GetLabel());
131 }
132
133
134 void wxGenericHyperlinkCtrl::SetNormalColour(const wxColour &colour)
135 {
136 m_normalColour = colour;
137 if (!m_visited)
138 {
139 SetForegroundColour(m_normalColour);
140 Refresh();
141 }
142 }
143
144 void wxGenericHyperlinkCtrl::SetVisitedColour(const wxColour &colour)
145 {
146 m_visitedColour = colour;
147 if (m_visited)
148 {
149 SetForegroundColour(m_visitedColour);
150 Refresh();
151 }
152 }
153
154 void wxGenericHyperlinkCtrl::DoContextMenu(const wxPoint &pos)
155 {
156 wxMenu *menuPopUp = new wxMenu(wxEmptyString, wxMENU_TEAROFF);
157 menuPopUp->Append(wxHYPERLINK_POPUP_COPY_ID, _("&Copy URL"));
158 PopupMenu( menuPopUp, pos );
159 delete menuPopUp;
160 }
161
162 wxRect wxGenericHyperlinkCtrl::GetLabelRect() const
163 {
164 // our best size is always the size of the label without borders
165 wxSize c(GetClientSize()), b(GetBestSize());
166 wxPoint offset;
167
168 // the label is always centered vertically
169 offset.y = (c.GetHeight()-b.GetHeight())/2;
170
171 if (HasFlag(wxHL_ALIGN_CENTRE))
172 offset.x = (c.GetWidth()-b.GetWidth())/2;
173 else if (HasFlag(wxHL_ALIGN_RIGHT))
174 offset.x = c.GetWidth()-b.GetWidth();
175 else if (HasFlag(wxHL_ALIGN_LEFT))
176 offset.x = 0;
177 return wxRect(offset, b);
178 }
179
180
181
182 // ----------------------------------------------------------------------------
183 // wxGenericHyperlinkCtrl - event handlers
184 // ----------------------------------------------------------------------------
185
186 void wxGenericHyperlinkCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
187 {
188 wxPaintDC dc(this);
189 dc.SetFont(GetFont());
190 dc.SetTextForeground(GetForegroundColour());
191 dc.SetTextBackground(GetBackgroundColour());
192
193 dc.DrawText(GetLabel(), GetLabelRect().GetTopLeft());
194 if (HasFocus())
195 {
196 wxRendererNative::Get().DrawFocusRect(this, dc, GetClientRect(), wxCONTROL_SELECTED);
197 }
198 }
199
200 void wxGenericHyperlinkCtrl::OnFocus(wxFocusEvent& event)
201 {
202 Refresh();
203 event.Skip();
204 }
205
206 void wxGenericHyperlinkCtrl::OnChar(wxKeyEvent& event)
207 {
208 switch (event.m_keyCode)
209 {
210 default:
211 event.Skip();
212 break;
213 case WXK_SPACE:
214 case WXK_NUMPAD_SPACE:
215 SetForegroundColour(m_visitedColour);
216 m_visited = true;
217 SendEvent();
218 break;
219 }
220 }
221
222 void wxGenericHyperlinkCtrl::OnLeftDown(wxMouseEvent& event)
223 {
224 // the left click must start from the hyperlink rect
225 m_clicking = GetLabelRect().Contains(event.GetPosition());
226 }
227
228 void wxGenericHyperlinkCtrl::OnLeftUp(wxMouseEvent& event)
229 {
230 // the click must be started and ended in the hyperlink rect
231 if (!m_clicking || !GetLabelRect().Contains(event.GetPosition()))
232 return;
233
234 SetForegroundColour(m_visitedColour);
235 m_visited = true;
236 m_clicking = false;
237
238 // send the event
239 SendEvent();
240 }
241
242 void wxGenericHyperlinkCtrl::OnRightUp(wxMouseEvent& event)
243 {
244 if( GetWindowStyle() & wxHL_CONTEXTMENU )
245 if ( GetLabelRect().Contains(event.GetPosition()) )
246 DoContextMenu(wxPoint(event.m_x, event.m_y));
247 }
248
249 void wxGenericHyperlinkCtrl::OnMotion(wxMouseEvent& event)
250 {
251 wxRect textrc = GetLabelRect();
252
253 if (textrc.Contains(event.GetPosition()))
254 {
255 SetCursor(wxCursor(wxCURSOR_HAND));
256 SetForegroundColour(m_hoverColour);
257 m_rollover = true;
258 Refresh();
259 }
260 else if (m_rollover)
261 {
262 SetCursor(*wxSTANDARD_CURSOR);
263 SetForegroundColour(!m_visited ? m_normalColour : m_visitedColour);
264 m_rollover = false;
265 Refresh();
266 }
267 }
268
269 void wxGenericHyperlinkCtrl::OnLeaveWindow(wxMouseEvent& WXUNUSED(event) )
270 {
271 // NB: when the label rect and the client size rect have the same
272 // height this function is indispensable to remove the "rollover"
273 // effect as the OnMotion() event handler could not be called
274 // in that case moving the mouse out of the label vertically...
275
276 if (m_rollover)
277 {
278 SetCursor(*wxSTANDARD_CURSOR);
279 SetForegroundColour(!m_visited ? m_normalColour : m_visitedColour);
280 m_rollover = false;
281 Refresh();
282 }
283 }
284
285 void wxGenericHyperlinkCtrl::OnPopUpCopy( wxCommandEvent& WXUNUSED(event) )
286 {
287 #if wxUSE_CLIPBOARD
288 if (!wxTheClipboard->Open())
289 return;
290
291 wxTextDataObject *data = new wxTextDataObject( m_url );
292 wxTheClipboard->SetData( data );
293 wxTheClipboard->Close();
294 #endif // wxUSE_CLIPBOARD
295 }
296
297 #endif // wxUSE_HYPERLINKCTRL