]> git.saurik.com Git - wxWidgets.git/blame - src/generic/hyperlink.cpp
Source cleaning for new features.
[wxWidgets.git] / src / generic / hyperlink.cpp
CommitLineData
17e91437
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: hyperlink.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#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21#pragma implementation "hyperlink.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
31//---------------------------------------------------------------------------
32// Includes
33//---------------------------------------------------------------------------
34
35#include "wx/hyperlink.h"
36#include "wx/utils.h" // for wxLaunchDefaultBrowser
37#include "wx/clipbrd.h"
38
39
40#if wxUSE_HYPERLINKCTRL
41
42// ============================================================================
43// implementation
44// ============================================================================
45
46IMPLEMENT_DYNAMIC_CLASS(wxHyperlinkCtrl, wxControl)
47DEFINE_EVENT_TYPE(wxEVT_COMMAND_HYPERLINK)
48
49// reserved for internal use only
50#define wxHYPERLINKCTRL_POPUP_COPY_ID 16384
51
52// ----------------------------------------------------------------------------
53// wxHyperlinkCtrl
54// ----------------------------------------------------------------------------
55
56BEGIN_EVENT_TABLE(wxHyperlinkCtrl, wxControl)
57 EVT_PAINT(wxHyperlinkCtrl::OnPaint)
58 EVT_LEFT_DOWN(wxHyperlinkCtrl::OnLeftDown)
59 EVT_LEFT_UP(wxHyperlinkCtrl::OnLeftUp)
60 EVT_RIGHT_UP(wxHyperlinkCtrl::OnRightUp)
61 EVT_ENTER_WINDOW(wxHyperlinkCtrl::OnEnterWindow)
62 EVT_LEAVE_WINDOW(wxHyperlinkCtrl::OnLeaveWindow)
63
64 // for the context menu
65 EVT_MENU(wxHYPERLINKCTRL_POPUP_COPY_ID, wxHyperlinkCtrl::OnPopUpCopy)
66END_EVENT_TABLE()
67
68bool wxHyperlinkCtrl::Create(wxWindow *parent, wxWindowID id,
69 const wxString& label, const wxString& url, const wxPoint& pos,
70 const wxSize& size, long style, const wxString& name)
71{
72 wxASSERT_MSG(!url.IsEmpty() || !label.IsEmpty(),
73 wxT("Both URL and label are empty ?"));
74
75 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
76 return false;
77
78 // set to non empty strings both the url and the label
79 SetURL(url.IsEmpty() ? label : url);
80 SetLabel(label.IsEmpty() ? url : label);
81
82 // by default the cursor to use in this window is wxCURSOR_HAND
83 SetCursor(wxCursor(wxCURSOR_HAND));
84
85 m_rollover = false;
86 m_clicking = false;
87 m_visited = false;
88
89 // colours
90 m_normalColour = *wxBLUE;
91 m_hoverColour = *wxRED;
92 SetForegroundColour(m_normalColour);
93
94 // by default the font of an hyperlink control is underlined
95 wxFont f = GetFont();
96 f.SetUnderlined(true);
97 SetFont(f);
98
99 CacheBestSize(DoGetBestSize());
100 SetMinSize(GetBestSize());
101 SetSize (DoGetBestSize());
102
103 return true;
104}
105
106wxSize wxHyperlinkCtrl::DoGetBestSize() const
107{
108 int w, h;
109
110 wxClientDC dc((wxWindow *)this);
111 dc.SetFont(GetFont());
112 dc.GetTextExtent(GetLabel(), &w, &h);
113
114 return wxSize(w, h);
115}
116
117void wxHyperlinkCtrl::DoGetSize(int *width, int *height) const
118{
119 if (width) *width = GetBestSize().GetWidth();
120 if (height) *height = GetBestSize().GetHeight();
121}
122
123void wxHyperlinkCtrl::SetNormalColour(const wxColour &colour)
124{
125 m_normalColour = colour;
126 if (!m_visited)
127 {
128 SetForegroundColour(m_normalColour);
129 Refresh();
130 }
131}
132
133void wxHyperlinkCtrl::SetVisitedColour(const wxColour &colour)
134{
135 m_visitedColour = colour;
136 if (m_visited)
137 {
138 SetForegroundColour(m_visitedColour);
139 Refresh();
140 }
141}
142
143void wxHyperlinkCtrl::DoContextMenu(const wxPoint &pos)
144{
145 wxMenu *menuPopUp = new wxMenu(wxEmptyString, wxMENU_TEAROFF);
146 menuPopUp->Append(wxHYPERLINKCTRL_POPUP_COPY_ID, wxT("Copy URL"));
147 PopupMenu( menuPopUp, pos );
148 delete menuPopUp;
149}
150
151
152
153// ----------------------------------------------------------------------------
154// wxHyperlinkCtrl - event handlers
155// ----------------------------------------------------------------------------
156
157void wxHyperlinkCtrl::OnPaint(wxPaintEvent& WXUNUSED(event))
158{
159 wxPaintDC dc(this);
160 dc.SetFont(GetFont());
161 dc.SetTextForeground(GetForegroundColour());
162 dc.SetTextBackground(GetBackgroundColour());
163 dc.DrawText(GetLabel(), 0, 0);
164}
165
166void wxHyperlinkCtrl::OnLeftDown(wxMouseEvent& WXUNUSED(event))
167{
168 m_clicking = true;
169}
170
171void wxHyperlinkCtrl::OnLeftUp(wxMouseEvent& WXUNUSED(event))
172{
173 if (!m_clicking) return;
174
175 SetForegroundColour(m_visitedColour);
176 m_visited = true;
177 m_clicking = false;
178
179 // send the event
180 wxHyperlinkEvent linkEvent(this, GetId(), m_url);
181 if (!GetEventHandler()->ProcessEvent(linkEvent)) // was the event skipped ?
182 if (!wxLaunchDefaultBrowser(m_url))
183 wxLogWarning(wxT("Could not launch the default browser with url '%s' !"), m_url.c_str());
184}
185
186void wxHyperlinkCtrl::OnRightUp(wxMouseEvent& event)
187{
188 if( GetWindowStyle() & wxHL_CONTEXTMENU )
189 DoContextMenu(wxPoint(event.m_x, event.m_y));
190}
191
192void wxHyperlinkCtrl::OnEnterWindow(wxMouseEvent& WXUNUSED(event))
193{
194 SetForegroundColour(m_hoverColour);
195 m_rollover = true;
196 Refresh();
197}
198
199void wxHyperlinkCtrl::OnLeaveWindow(wxMouseEvent& WXUNUSED(event))
200{
201 if (m_rollover)
202 {
203 SetForegroundColour(!m_visited ? m_normalColour : m_visitedColour);
204 m_rollover = false;
205 Refresh();
206 }
207}
208
209void wxHyperlinkCtrl::OnPopUpCopy( wxCommandEvent& WXUNUSED(event) )
210{
211 if (!wxTheClipboard->Open())
212 return;
213
214 wxTextDataObject *data = new wxTextDataObject( m_url );
215 wxTheClipboard->SetData( data );
216 wxTheClipboard->Close();
217}
218
219#endif // wxUSE_HYPERLINKCTRL