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