]> git.saurik.com Git - wxWidgets.git/blob - samples/html/test/test.cpp
cleanup
[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
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 virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type),
54 const wxString& WXUNUSED(url),
55 wxString *WXUNUSED(redirect)) const;
56
57 private:
58 void OnClipboardEvent(wxClipboardTextEvent& event);
59
60 #if wxUSE_CLIPBOARD
61 DECLARE_EVENT_TABLE()
62 #endif // wxUSE_CLIPBOARD
63 DECLARE_NO_COPY_CLASS(MyHtmlWindow)
64 };
65
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame : public wxFrame
68 {
69 public:
70 // ctor(s)
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent& event);
75 void OnPageOpen(wxCommandEvent& event);
76 void OnDefaultLocalBrowser(wxCommandEvent& event);
77 void OnDefaultWebBrowser(wxCommandEvent& event);
78 void OnBack(wxCommandEvent& event);
79 void OnForward(wxCommandEvent& event);
80 void OnProcessor(wxCommandEvent& event);
81
82 void OnHtmlLinkClicked(wxHtmlLinkEvent& event);
83 void OnHtmlCellHover(wxHtmlCellEvent &event);
84 void OnHtmlCellClicked(wxHtmlCellEvent &event);
85
86 private:
87 MyHtmlWindow *m_Html;
88 wxHtmlProcessor *m_Processor;
89
90 // Any class wishing to process wxWidgets events must use this macro
91 DECLARE_EVENT_TABLE()
92 };
93
94
95 class BoldProcessor : public wxHtmlProcessor
96 {
97 public:
98 virtual wxString Process(const wxString& s) const
99 {
100 wxString r(s);
101 r.Replace(wxT("<b>"), wxEmptyString);
102 r.Replace(wxT("<B>"), wxEmptyString);
103 r.Replace(wxT("</b>"), wxEmptyString);
104 r.Replace(wxT("</B>"), wxEmptyString);
105
106 return r;
107 }
108 };
109
110 // ----------------------------------------------------------------------------
111 // constants
112 // ----------------------------------------------------------------------------
113
114 // IDs for the controls and the menu commands
115 enum
116 {
117 // menu items
118 ID_PageOpen = wxID_HIGHEST,
119 ID_DefaultLocalBrowser,
120 ID_DefaultWebBrowser,
121 ID_Back,
122 ID_Forward,
123 ID_Processor
124 };
125
126 // ----------------------------------------------------------------------------
127 // event tables and other macros for wxWidgets
128 // ----------------------------------------------------------------------------
129
130 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
131 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
132 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
133 EVT_MENU(ID_DefaultLocalBrowser, MyFrame::OnDefaultLocalBrowser)
134 EVT_MENU(ID_DefaultWebBrowser, MyFrame::OnDefaultWebBrowser)
135 EVT_MENU(ID_Back, MyFrame::OnBack)
136 EVT_MENU(ID_Forward, MyFrame::OnForward)
137 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
138
139 EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked)
140 EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover)
141 EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked)
142 END_EVENT_TABLE()
143
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // `Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 if ( !wxApp::OnInit() )
158 return false;
159
160 #if wxUSE_SYSTEM_OPTIONS
161 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
162 #endif
163
164 wxInitAllImageHandlers();
165 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
166 wxFileSystem::AddHandler(new wxInternetFSHandler);
167 #endif
168
169 SetVendorName(wxT("wxWidgets"));
170 SetAppName(wxT("wxHtmlTest"));
171 // the following call to wxConfig::Get will use it to create an object...
172
173 // Create the main application window
174 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
175 wxDefaultPosition, wxSize(640, 480));
176
177 frame->Show();
178
179 return true /* continue running */;
180 }
181
182 // ----------------------------------------------------------------------------
183 // main frame
184 // ----------------------------------------------------------------------------
185
186 // frame constructor
187 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
188 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
189 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
190 {
191 // create a menu bar
192 wxMenu *menuFile = new wxMenu;
193 wxMenu *menuNav = new wxMenu;
194
195 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
196 menuFile->Append(ID_DefaultLocalBrowser, _("&Open current page with default browser"));
197 menuFile->Append(ID_DefaultWebBrowser, _("Open a &web page with default browser"));
198 menuFile->AppendSeparator();
199 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
200 wxEmptyString, wxITEM_CHECK);
201
202 menuFile->AppendSeparator();
203 menuFile->Append(wxID_EXIT, _("&Close frame"));
204 menuNav->Append(ID_Back, _("Go &BACK"));
205 menuNav->Append(ID_Forward, _("Go &FORWARD"));
206
207 // now append the freshly created menu to the menu bar...
208 wxMenuBar *menuBar = new wxMenuBar;
209 menuBar->Append(menuFile, _("&File"));
210 menuBar->Append(menuNav, _("&Navigate"));
211
212 // ... and attach this menu bar to the frame
213 SetMenuBar(menuBar);
214
215 SetIcon(wxIcon(sample_xpm));
216
217 #if wxUSE_ACCEL
218 // Create convenient accelerators for Back and Forward navigation
219 wxAcceleratorEntry entries[2];
220 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
221 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
222
223 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
224 SetAcceleratorTable(accel);
225 #endif // wxUSE_ACCEL
226
227 #if wxUSE_STATUSBAR
228 CreateStatusBar(2);
229 #endif // wxUSE_STATUSBAR
230
231 m_Processor = new BoldProcessor;
232 m_Processor->Enable(false);
233 m_Html = new MyHtmlWindow(this);
234 m_Html->SetRelatedFrame(this, _("HTML : %s"));
235 #if wxUSE_STATUSBAR
236 m_Html->SetRelatedStatusBar(0);
237 #endif // wxUSE_STATUSBAR
238 m_Html->ReadCustomization(wxConfig::Get());
239 m_Html->LoadFile(wxFileName(wxT("test.htm")));
240 m_Html->AddProcessor(m_Processor);
241
242 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
243 wxDefaultPosition, wxDefaultSize,
244 wxTE_MULTILINE);
245
246 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
247
248 wxSizer *sz = new wxBoxSizer(wxVERTICAL);
249 sz->Add(m_Html, 3, wxGROW);
250 sz->Add(text, 1, wxGROW);
251 SetSizer(sz);
252 }
253
254
255 // event handlers
256
257 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
258 {
259 m_Html->WriteCustomization(wxConfig::Get());
260 delete wxConfig::Set(NULL);
261
262 // true is to force the frame to close
263 Close(true);
264 }
265
266 void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
267 {
268 #if wxUSE_FILEDLG
269 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
270 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html"));
271
272 if (!p.empty())
273 m_Html->LoadFile(wxFileName(p));
274 #endif // wxUSE_FILEDLG
275 }
276
277 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent& WXUNUSED(event))
278 {
279 wxString page = m_Html->GetOpenedPage();
280 if (!page.empty())
281 {
282 wxLaunchDefaultBrowser(page);
283 }
284 }
285
286 void MyFrame::OnDefaultWebBrowser(wxCommandEvent& WXUNUSED(event))
287 {
288 wxString page = m_Html->GetOpenedPage();
289 if (!page.empty())
290 {
291 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
292 }
293 }
294
295 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
296 {
297 if (!m_Html->HistoryBack())
298 {
299 wxMessageBox(_("You reached prehistory era!"));
300 }
301 }
302
303 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
304 {
305 if (!m_Html->HistoryForward())
306 {
307 wxMessageBox(_("No more items in history!"));
308 }
309 }
310
311 void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
312 {
313 m_Processor->Enable(!m_Processor->IsEnabled());
314 m_Html->LoadPage(m_Html->GetOpenedPage());
315 }
316
317 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event)
318 {
319 wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str());
320
321 // skipping this event the default behaviour (load the clicked URL)
322 // will happen...
323 event.Skip();
324 }
325
326 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event)
327 {
328 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
329 event.GetCell(), event.GetPoint().x, event.GetPoint().y);
330 }
331
332 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event)
333 {
334 wxLogMessage(wxT("Click over cell %p at %d;%d"),
335 event.GetCell(), event.GetPoint().x, event.GetPoint().y);
336
337 // if we don't skip the event, OnHtmlLinkClicked won't be called!
338 event.Skip();
339 }
340
341 wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
342 const wxString& url,
343 wxString *WXUNUSED(redirect)) const
344 {
345 GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1);
346 return wxHTML_OPEN;
347 }
348
349 #if wxUSE_CLIPBOARD
350 BEGIN_EVENT_TABLE(MyHtmlWindow, wxHtmlWindow)
351 EVT_TEXT_COPY(wxID_ANY, MyHtmlWindow::OnClipboardEvent)
352 END_EVENT_TABLE()
353
354 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event))
355 {
356 // explicitly call wxHtmlWindow::CopySelection() method
357 // and show the first 100 characters of the text copied in the status bar
358 if ( CopySelection() )
359 {
360 wxTextDataObject data;
361 if ( wxTheClipboard && wxTheClipboard->GetData(data) )
362 {
363 const wxString text = data.GetText();
364 const size_t maxTextLength = 100;
365
366 wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"),
367 wxString(text, maxTextLength).c_str(),
368 (text.length() > maxTextLength) ? _T("...")
369 : _T("")));
370 return;
371 }
372 }
373
374 wxLogStatus(_T("Clipboard: nothing"));
375 }
376 #endif // wxUSE_CLIPBOARD