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