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