Send full url to wxHtmlWindow::OnOpeningURL validation (and therefore use wxURL to...
[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 #if defined(__GNUG__) && !defined(__APPLE__)
12 #pragma implementation "test.cpp"
13 #pragma interface "test.cpp"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 // For all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWidgets headers
25 #ifndef WX_PRECOMP
26 #include "wx/wx.h"
27 #endif
28
29 #include "wx/image.h"
30 #include "wx/sysopt.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/html/htmlproc.h"
33 #include "wx/fs_inet.h"
34 #include "wx/filedlg.h"
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 DECLARE_NO_COPY_CLASS(MyHtmlWindow)
59 };
60
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame : public wxFrame
63 {
64 public:
65 // ctor(s)
66 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
67
68 // event handlers (these functions should _not_ be virtual)
69 void OnQuit(wxCommandEvent& event);
70 void OnPageOpen(wxCommandEvent& event);
71 void OnBack(wxCommandEvent& event);
72 void OnForward(wxCommandEvent& event);
73 void OnProcessor(wxCommandEvent& event);
74
75 private:
76 MyHtmlWindow *m_Html;
77 wxHtmlProcessor *m_Processor;
78
79 // Any class wishing to process wxWidgets events must use this macro
80 DECLARE_EVENT_TABLE()
81 };
82
83
84 class BoldProcessor : public wxHtmlProcessor
85 {
86 public:
87 virtual wxString Process(const wxString& s) const
88 {
89 wxString r(s);
90 r.Replace(wxT("<b>"), wxEmptyString);
91 r.Replace(wxT("<B>"), wxEmptyString);
92 r.Replace(wxT("</b>"), wxEmptyString);
93 r.Replace(wxT("</B>"), wxEmptyString);
94
95 return r;
96 }
97 };
98
99 // ----------------------------------------------------------------------------
100 // constants
101 // ----------------------------------------------------------------------------
102
103 // IDs for the controls and the menu commands
104 enum
105 {
106 // menu items
107 ID_PageOpen = wxID_HIGHEST,
108 ID_Back,
109 ID_Forward,
110 ID_Processor
111 };
112
113 // ----------------------------------------------------------------------------
114 // event tables and other macros for wxWidgets
115 // ----------------------------------------------------------------------------
116
117 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
118 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
119 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
120 EVT_MENU(ID_Back, MyFrame::OnBack)
121 EVT_MENU(ID_Forward, MyFrame::OnForward)
122 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
123 END_EVENT_TABLE()
124
125 IMPLEMENT_APP(MyApp)
126
127 // ============================================================================
128 // implementation
129 // ============================================================================
130
131 // ----------------------------------------------------------------------------
132 // the application class
133 // ----------------------------------------------------------------------------
134
135 // `Main program' equivalent: the program execution "starts" here
136 bool MyApp::OnInit()
137 {
138 #if wxUSE_SYSTEM_OPTIONS
139 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
140 #endif
141
142 wxInitAllImageHandlers();
143 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
144 wxFileSystem::AddHandler(new wxInternetFSHandler);
145 #endif
146
147 SetVendorName(wxT("wxWidgets"));
148 SetAppName(wxT("wxHtmlTest"));
149 // the following call to wxConfig::Get will use it to create an object...
150
151 // Create the main application window
152 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
153 wxDefaultPosition, wxSize(640, 480));
154
155 frame->Show();
156
157 return true /* continue running */;
158 }
159
160 // ----------------------------------------------------------------------------
161 // main frame
162 // ----------------------------------------------------------------------------
163
164 // frame constructor
165 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
166 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
167 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
168 {
169 // create a menu bar
170 wxMenu *menuFile = new wxMenu;
171 wxMenu *menuNav = new wxMenu;
172
173 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
174 menuFile->AppendSeparator();
175 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
176 wxEmptyString, wxITEM_CHECK);
177
178 menuFile->AppendSeparator();
179 menuFile->Append(wxID_EXIT, _("&Close frame"));
180 menuNav->Append(ID_Back, _("Go &BACK"));
181 menuNav->Append(ID_Forward, _("Go &FORWARD"));
182
183 // now append the freshly created menu to the menu bar...
184 wxMenuBar *menuBar = new wxMenuBar;
185 menuBar->Append(menuFile, _("&File"));
186 menuBar->Append(menuNav, _("&Navigate"));
187
188 // ... and attach this menu bar to the frame
189 SetMenuBar(menuBar);
190
191 #if wxUSE_ACCEL
192 // Create convenient accelerators for Back and Forward navigation
193 wxAcceleratorEntry entries[2];
194 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
195 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
196
197 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
198 SetAcceleratorTable(accel);
199 #endif // wxUSE_ACCEL
200
201 #if wxUSE_STATUSBAR
202 CreateStatusBar(2);
203 #endif // wxUSE_STATUSBAR
204
205 m_Processor = new BoldProcessor;
206 m_Processor->Enable(false);
207 m_Html = new MyHtmlWindow(this);
208 m_Html->SetRelatedFrame(this, _("HTML : %s"));
209 #if wxUSE_STATUSBAR
210 m_Html->SetRelatedStatusBar(0);
211 #endif // wxUSE_STATUSBAR
212 m_Html->ReadCustomization(wxConfig::Get());
213 m_Html->LoadFile(wxFileName(wxT("test.htm")));
214 m_Html->AddProcessor(m_Processor);
215 }
216
217
218 // event handlers
219
220 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
221 {
222 m_Html->WriteCustomization(wxConfig::Get());
223 delete wxConfig::Set(NULL);
224
225 // true is to force the frame to close
226 Close(true);
227 }
228
229 void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
230 {
231 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
232 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm"));
233
234 if (p != wxEmptyString)
235 m_Html->LoadPage(p);
236 }
237
238 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
239 {
240 if (!m_Html->HistoryBack())
241 {
242 wxMessageBox(_("You reached prehistory era!"));
243 }
244 }
245
246 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
247 {
248 if (!m_Html->HistoryForward())
249 {
250 wxMessageBox(_("No more items in history!"));
251 }
252 }
253
254 void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
255 {
256 m_Processor->Enable(!m_Processor->IsEnabled());
257 m_Html->LoadPage(m_Html->GetOpenedPage());
258 }
259
260 wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
261 const wxString& url,
262 wxString *WXUNUSED(redirect)) const
263 {
264 GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1);
265 return wxHTML_OPEN;
266 }