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