]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: wxHtml testing example | |
3f6bd7c1 DS |
4 | // Author: Vaclav Slavik |
5 | // Created: 1999-07-07 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
5526e819 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
5526e819 | 11 | // For compilers that support precompilation, includes "wx/wx.h". |
92a19c2e | 12 | #include "wx/wxprec.h" |
5526e819 VS |
13 | |
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
3f6bd7c1 | 18 | // For all others, include the necessary headers (this file is usually all you |
be5a51fb | 19 | // need because it includes almost all "standard" wxWidgets headers |
5526e819 | 20 | #ifndef WX_PRECOMP |
67547666 | 21 | #include "wx/wx.h" |
5526e819 VS |
22 | #endif |
23 | ||
40091824 | 24 | #include "wx/image.h" |
bbb8f29b | 25 | #include "wx/sysopt.h" |
40091824 VS |
26 | #include "wx/html/htmlwin.h" |
27 | #include "wx/html/htmlproc.h" | |
28 | #include "wx/fs_inet.h" | |
29 | #include "wx/filedlg.h" | |
d83c04e6 | 30 | #include "wx/utils.h" |
0f11c233 VZ |
31 | #include "wx/clipbrd.h" |
32 | #include "wx/dataobj.h" | |
63b459df | 33 | #include "wx/stopwatch.h" |
5526e819 | 34 | |
18922659 JS |
35 | #include "../../sample.xpm" |
36 | ||
5526e819 VS |
37 | // ---------------------------------------------------------------------------- |
38 | // private classes | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | // Define a new application type, each program should derive a class from wxApp | |
3e729cbe VS |
42 | class MyApp : public wxApp |
43 | { | |
44 | public: | |
3f6bd7c1 | 45 | virtual bool OnInit(); |
3e729cbe | 46 | }; |
5526e819 | 47 | |
4bfa3189 WS |
48 | // Define a new html window type: this is a wrapper for handling wxHtmlWindow events |
49 | class MyHtmlWindow : public wxHtmlWindow | |
50 | { | |
51 | public: | |
52 | MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { } | |
53 | ||
54 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
55 | const wxString& WXUNUSED(url), | |
56 | wxString *WXUNUSED(redirect)) const; | |
57 | ||
58 | private: | |
0f11c233 VZ |
59 | void OnClipboardEvent(wxClipboardTextEvent& event); |
60 | ||
61 | #if wxUSE_CLIPBOARD | |
62 | DECLARE_EVENT_TABLE() | |
63 | #endif // wxUSE_CLIPBOARD | |
c0c133e1 | 64 | wxDECLARE_NO_COPY_CLASS(MyHtmlWindow); |
4bfa3189 WS |
65 | }; |
66 | ||
5526e819 | 67 | // Define a new frame type: this is going to be our main frame |
3e729cbe VS |
68 | class MyFrame : public wxFrame |
69 | { | |
70 | public: | |
3f6bd7c1 | 71 | // ctor(s) |
3e729cbe VS |
72 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
73 | ||
3f6bd7c1 | 74 | // event handlers (these functions should _not_ be virtual) |
3e729cbe VS |
75 | void OnQuit(wxCommandEvent& event); |
76 | void OnPageOpen(wxCommandEvent& event); | |
17ede0b1 RR |
77 | void OnDefaultLocalBrowser(wxCommandEvent& event); |
78 | void OnDefaultWebBrowser(wxCommandEvent& event); | |
3e729cbe VS |
79 | void OnBack(wxCommandEvent& event); |
80 | void OnForward(wxCommandEvent& event); | |
81 | void OnProcessor(wxCommandEvent& event); | |
82 | ||
a1c3cdc4 VS |
83 | void OnHtmlLinkClicked(wxHtmlLinkEvent& event); |
84 | void OnHtmlCellHover(wxHtmlCellEvent &event); | |
85 | void OnHtmlCellClicked(wxHtmlCellEvent &event); | |
86 | ||
3e729cbe | 87 | private: |
4bfa3189 | 88 | MyHtmlWindow *m_Html; |
3f6bd7c1 DS |
89 | wxHtmlProcessor *m_Processor; |
90 | ||
be5a51fb | 91 | // Any class wishing to process wxWidgets events must use this macro |
3f6bd7c1 | 92 | DECLARE_EVENT_TABLE() |
3e729cbe VS |
93 | }; |
94 | ||
95 | ||
96 | class BoldProcessor : public wxHtmlProcessor | |
97 | { | |
3f6bd7c1 DS |
98 | public: |
99 | virtual wxString Process(const wxString& s) const | |
100 | { | |
101 | wxString r(s); | |
102 | r.Replace(wxT("<b>"), wxEmptyString); | |
103 | r.Replace(wxT("<B>"), wxEmptyString); | |
104 | r.Replace(wxT("</b>"), wxEmptyString); | |
105 | r.Replace(wxT("</B>"), wxEmptyString); | |
106 | ||
107 | return r; | |
108 | } | |
3e729cbe | 109 | }; |
5526e819 VS |
110 | |
111 | // ---------------------------------------------------------------------------- | |
112 | // constants | |
113 | // ---------------------------------------------------------------------------- | |
114 | ||
115 | // IDs for the controls and the menu commands | |
3f6bd7c1 DS |
116 | enum |
117 | { | |
5526e819 | 118 | // menu items |
3f6bd7c1 | 119 | ID_PageOpen = wxID_HIGHEST, |
17ede0b1 RR |
120 | ID_DefaultLocalBrowser, |
121 | ID_DefaultWebBrowser, | |
3f6bd7c1 DS |
122 | ID_Back, |
123 | ID_Forward, | |
124 | ID_Processor | |
125 | }; | |
5526e819 VS |
126 | |
127 | // ---------------------------------------------------------------------------- | |
be5a51fb | 128 | // event tables and other macros for wxWidgets |
5526e819 VS |
129 | // ---------------------------------------------------------------------------- |
130 | ||
3f6bd7c1 DS |
131 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
132 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
133 | EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen) | |
17ede0b1 RR |
134 | EVT_MENU(ID_DefaultLocalBrowser, MyFrame::OnDefaultLocalBrowser) |
135 | EVT_MENU(ID_DefaultWebBrowser, MyFrame::OnDefaultWebBrowser) | |
3f6bd7c1 DS |
136 | EVT_MENU(ID_Back, MyFrame::OnBack) |
137 | EVT_MENU(ID_Forward, MyFrame::OnForward) | |
138 | EVT_MENU(ID_Processor, MyFrame::OnProcessor) | |
a1c3cdc4 VS |
139 | |
140 | EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked) | |
141 | EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover) | |
142 | EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked) | |
3f6bd7c1 DS |
143 | END_EVENT_TABLE() |
144 | ||
145 | IMPLEMENT_APP(MyApp) | |
146 | ||
147 | // ============================================================================ | |
148 | // implementation | |
149 | // ============================================================================ | |
150 | ||
151 | // ---------------------------------------------------------------------------- | |
152 | // the application class | |
153 | // ---------------------------------------------------------------------------- | |
154 | ||
155 | // `Main program' equivalent: the program execution "starts" here | |
156 | bool MyApp::OnInit() | |
157 | { | |
45e6e6f8 VZ |
158 | if ( !wxApp::OnInit() ) |
159 | return false; | |
160 | ||
954cc8d2 | 161 | #if wxUSE_SYSTEM_OPTIONS |
3f6bd7c1 | 162 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 163 | #endif |
bbb8f29b | 164 | |
3f6bd7c1 | 165 | wxInitAllImageHandlers(); |
954cc8d2 | 166 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
3f6bd7c1 | 167 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 168 | #endif |
5612e524 | 169 | |
be5a51fb | 170 | SetVendorName(wxT("wxWidgets")); |
3f6bd7c1 DS |
171 | SetAppName(wxT("wxHtmlTest")); |
172 | // the following call to wxConfig::Get will use it to create an object... | |
5612e524 | 173 | |
5526e819 | 174 | // Create the main application window |
3f6bd7c1 DS |
175 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), |
176 | wxDefaultPosition, wxSize(640, 480)); | |
177 | ||
178 | frame->Show(); | |
179 | ||
180 | return true /* continue running */; | |
181 | } | |
5526e819 VS |
182 | |
183 | // ---------------------------------------------------------------------------- | |
184 | // main frame | |
185 | // ---------------------------------------------------------------------------- | |
186 | ||
5526e819 | 187 | // frame constructor |
3f6bd7c1 DS |
188 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
189 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size, | |
190 | wxDEFAULT_FRAME_STYLE, wxT("html_test_app")) | |
191 | { | |
5526e819 | 192 | // create a menu bar |
3f6bd7c1 DS |
193 | wxMenu *menuFile = new wxMenu; |
194 | wxMenu *menuNav = new wxMenu; | |
195 | ||
63b459df | 196 | menuFile->Append(ID_PageOpen, _("&Open HTML page...\tCtrl-O")); |
17ede0b1 RR |
197 | menuFile->Append(ID_DefaultLocalBrowser, _("&Open current page with default browser")); |
198 | menuFile->Append(ID_DefaultWebBrowser, _("Open a &web page with default browser")); | |
3f6bd7c1 DS |
199 | menuFile->AppendSeparator(); |
200 | menuFile->Append(ID_Processor, _("&Remove bold attribute"), | |
201 | wxEmptyString, wxITEM_CHECK); | |
202 | ||
203 | menuFile->AppendSeparator(); | |
204 | menuFile->Append(wxID_EXIT, _("&Close frame")); | |
205 | menuNav->Append(ID_Back, _("Go &BACK")); | |
206 | menuNav->Append(ID_Forward, _("Go &FORWARD")); | |
5526e819 VS |
207 | |
208 | // now append the freshly created menu to the menu bar... | |
3f6bd7c1 DS |
209 | wxMenuBar *menuBar = new wxMenuBar; |
210 | menuBar->Append(menuFile, _("&File")); | |
211 | menuBar->Append(menuNav, _("&Navigate")); | |
5526e819 VS |
212 | |
213 | // ... and attach this menu bar to the frame | |
3f6bd7c1 DS |
214 | SetMenuBar(menuBar); |
215 | ||
18922659 | 216 | SetIcon(wxIcon(sample_xpm)); |
11fdee42 | 217 | |
3f6bd7c1 DS |
218 | #if wxUSE_ACCEL |
219 | // Create convenient accelerators for Back and Forward navigation | |
220 | wxAcceleratorEntry entries[2]; | |
221 | entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back); | |
222 | entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward); | |
223 | ||
224 | wxAcceleratorTable accel(WXSIZEOF(entries), entries); | |
225 | SetAcceleratorTable(accel); | |
226 | #endif // wxUSE_ACCEL | |
227 | ||
8520f137 | 228 | #if wxUSE_STATUSBAR |
4bfa3189 | 229 | CreateStatusBar(2); |
8520f137 | 230 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
231 | |
232 | m_Processor = new BoldProcessor; | |
233 | m_Processor->Enable(false); | |
4bfa3189 | 234 | m_Html = new MyHtmlWindow(this); |
3f6bd7c1 | 235 | m_Html->SetRelatedFrame(this, _("HTML : %s")); |
8520f137 | 236 | #if wxUSE_STATUSBAR |
63b459df | 237 | m_Html->SetRelatedStatusBar(1); |
8520f137 | 238 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
239 | m_Html->ReadCustomization(wxConfig::Get()); |
240 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
241 | m_Html->AddProcessor(m_Processor); | |
a1c3cdc4 VS |
242 | |
243 | wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""), | |
244 | wxDefaultPosition, wxDefaultSize, | |
245 | wxTE_MULTILINE); | |
246 | ||
247 | delete wxLog::SetActiveTarget(new wxLogTextCtrl(text)); | |
248 | ||
249 | wxSizer *sz = new wxBoxSizer(wxVERTICAL); | |
250 | sz->Add(m_Html, 3, wxGROW); | |
251 | sz->Add(text, 1, wxGROW); | |
252 | SetSizer(sz); | |
3f6bd7c1 | 253 | } |
5526e819 VS |
254 | |
255 | ||
256 | // event handlers | |
257 | ||
3e729cbe VS |
258 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
259 | { | |
3f6bd7c1 DS |
260 | m_Html->WriteCustomization(wxConfig::Get()); |
261 | delete wxConfig::Set(NULL); | |
262 | ||
263 | // true is to force the frame to close | |
264 | Close(true); | |
3e729cbe VS |
265 | } |
266 | ||
267 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
268 | { | |
71307412 | 269 | #if wxUSE_FILEDLG |
3f6bd7c1 | 270 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, |
43056e6e | 271 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html")); |
3f6bd7c1 | 272 | |
71307412 | 273 | if (!p.empty()) |
63b459df VZ |
274 | { |
275 | #if wxUSE_STOPWATCH | |
276 | wxStopWatch sw; | |
277 | #endif | |
9dbadebe | 278 | m_Html->LoadFile(wxFileName(p)); |
63b459df VZ |
279 | #if wxUSE_STOPWATCH |
280 | wxLogStatus("Loaded \"%s\" in %lums", p, sw.Time()); | |
281 | #endif | |
282 | } | |
71307412 | 283 | #endif // wxUSE_FILEDLG |
3e729cbe VS |
284 | } |
285 | ||
17ede0b1 | 286 | void MyFrame::OnDefaultLocalBrowser(wxCommandEvent& WXUNUSED(event)) |
d83c04e6 WS |
287 | { |
288 | wxString page = m_Html->GetOpenedPage(); | |
289 | if (!page.empty()) | |
290 | { | |
291 | wxLaunchDefaultBrowser(page); | |
292 | } | |
293 | } | |
294 | ||
17ede0b1 RR |
295 | void MyFrame::OnDefaultWebBrowser(wxCommandEvent& WXUNUSED(event)) |
296 | { | |
297 | wxString page = m_Html->GetOpenedPage(); | |
298 | if (!page.empty()) | |
299 | { | |
300 | wxLaunchDefaultBrowser(wxT("http://www.google.com")); | |
301 | } | |
302 | } | |
303 | ||
3e729cbe VS |
304 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) |
305 | { | |
3f6bd7c1 DS |
306 | if (!m_Html->HistoryBack()) |
307 | { | |
308 | wxMessageBox(_("You reached prehistory era!")); | |
309 | } | |
3e729cbe VS |
310 | } |
311 | ||
312 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
313 | { | |
3f6bd7c1 DS |
314 | if (!m_Html->HistoryForward()) |
315 | { | |
316 | wxMessageBox(_("No more items in history!")); | |
317 | } | |
3e729cbe VS |
318 | } |
319 | ||
320 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
321 | { | |
322 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
323 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 324 | } |
4bfa3189 | 325 | |
a1c3cdc4 VS |
326 | void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event) |
327 | { | |
328 | wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str()); | |
329 | ||
330 | // skipping this event the default behaviour (load the clicked URL) | |
331 | // will happen... | |
332 | event.Skip(); | |
333 | } | |
334 | ||
335 | void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event) | |
336 | { | |
34e20200 | 337 | wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"), |
a1c3cdc4 VS |
338 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); |
339 | } | |
340 | ||
341 | void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event) | |
342 | { | |
34e20200 | 343 | wxLogMessage(wxT("Click over cell %p at %d;%d"), |
a1c3cdc4 VS |
344 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); |
345 | ||
346 | // if we don't skip the event, OnHtmlLinkClicked won't be called! | |
347 | event.Skip(); | |
348 | } | |
349 | ||
4bfa3189 WS |
350 | wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type), |
351 | const wxString& url, | |
352 | wxString *WXUNUSED(redirect)) const | |
353 | { | |
354 | GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1); | |
355 | return wxHTML_OPEN; | |
356 | } | |
0f11c233 VZ |
357 | |
358 | #if wxUSE_CLIPBOARD | |
359 | BEGIN_EVENT_TABLE(MyHtmlWindow, wxHtmlWindow) | |
360 | EVT_TEXT_COPY(wxID_ANY, MyHtmlWindow::OnClipboardEvent) | |
361 | END_EVENT_TABLE() | |
362 | ||
363 | void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event)) | |
364 | { | |
365 | // explicitly call wxHtmlWindow::CopySelection() method | |
366 | // and show the first 100 characters of the text copied in the status bar | |
367 | if ( CopySelection() ) | |
368 | { | |
369 | wxTextDataObject data; | |
3214ac5a | 370 | if ( wxTheClipboard && wxTheClipboard->Open() && wxTheClipboard->GetData(data) ) |
0f11c233 VZ |
371 | { |
372 | const wxString text = data.GetText(); | |
373 | const size_t maxTextLength = 100; | |
374 | ||
375 | wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"), | |
376 | wxString(text, maxTextLength).c_str(), | |
377 | (text.length() > maxTextLength) ? _T("...") | |
378 | : _T(""))); | |
3214ac5a JS |
379 | wxTheClipboard->Close(); |
380 | ||
0f11c233 VZ |
381 | return; |
382 | } | |
383 | } | |
384 | ||
385 | wxLogStatus(_T("Clipboard: nothing")); | |
386 | } | |
387 | #endif // wxUSE_CLIPBOARD |