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