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