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