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