]> git.saurik.com Git - wxWidgets.git/blob - samples/html/test/test.cpp
56dabce153de90750e73e164897dd90edbd1a8f2
[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
13 #pragma interface
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 #include "wx/utils.h"
36
37 #include "../../sample.xpm"
38
39 // ----------------------------------------------------------------------------
40 // private classes
41 // ----------------------------------------------------------------------------
42
43 // Define a new application type, each program should derive a class from wxApp
44 class MyApp : public wxApp
45 {
46 public:
47 virtual bool OnInit();
48 };
49
50 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
51 class MyHtmlWindow : public wxHtmlWindow
52 {
53 public:
54 MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { }
55
56 virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type),
57 const wxString& WXUNUSED(url),
58 wxString *WXUNUSED(redirect)) const;
59
60 private:
61 DECLARE_NO_COPY_CLASS(MyHtmlWindow)
62 };
63
64 // Define a new frame type: this is going to be our main frame
65 class MyFrame : public wxFrame
66 {
67 public:
68 // ctor(s)
69 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
70
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent& event);
73 void OnPageOpen(wxCommandEvent& event);
74 void OnDefaultBrowser(wxCommandEvent& event);
75 void OnBack(wxCommandEvent& event);
76 void OnForward(wxCommandEvent& event);
77 void OnProcessor(wxCommandEvent& event);
78
79 private:
80 MyHtmlWindow *m_Html;
81 wxHtmlProcessor *m_Processor;
82
83 // Any class wishing to process wxWidgets events must use this macro
84 DECLARE_EVENT_TABLE()
85 };
86
87
88 class BoldProcessor : public wxHtmlProcessor
89 {
90 public:
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 }
101 };
102
103 // ----------------------------------------------------------------------------
104 // constants
105 // ----------------------------------------------------------------------------
106
107 // IDs for the controls and the menu commands
108 enum
109 {
110 // menu items
111 ID_PageOpen = wxID_HIGHEST,
112 ID_DefaultBrowser,
113 ID_Back,
114 ID_Forward,
115 ID_Processor
116 };
117
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
121
122 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
123 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
124 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
125 EVT_MENU(ID_DefaultBrowser, MyFrame::OnDefaultBrowser)
126 EVT_MENU(ID_Back, MyFrame::OnBack)
127 EVT_MENU(ID_Forward, MyFrame::OnForward)
128 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
129 END_EVENT_TABLE()
130
131 IMPLEMENT_APP(MyApp)
132
133 // ============================================================================
134 // implementation
135 // ============================================================================
136
137 // ----------------------------------------------------------------------------
138 // the application class
139 // ----------------------------------------------------------------------------
140
141 // `Main program' equivalent: the program execution "starts" here
142 bool MyApp::OnInit()
143 {
144 #if wxUSE_SYSTEM_OPTIONS
145 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
146 #endif
147
148 wxInitAllImageHandlers();
149 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
150 wxFileSystem::AddHandler(new wxInternetFSHandler);
151 #endif
152
153 SetVendorName(wxT("wxWidgets"));
154 SetAppName(wxT("wxHtmlTest"));
155 // the following call to wxConfig::Get will use it to create an object...
156
157 // Create the main application window
158 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
159 wxDefaultPosition, wxSize(640, 480));
160
161 frame->Show();
162
163 return true /* continue running */;
164 }
165
166 // ----------------------------------------------------------------------------
167 // main frame
168 // ----------------------------------------------------------------------------
169
170 // frame constructor
171 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
172 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
173 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
174 {
175 // create a menu bar
176 wxMenu *menuFile = new wxMenu;
177 wxMenu *menuNav = new wxMenu;
178
179 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
180 menuFile->Append(ID_DefaultBrowser, _("&Open current page with default browser"));
181 menuFile->AppendSeparator();
182 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
183 wxEmptyString, wxITEM_CHECK);
184
185 menuFile->AppendSeparator();
186 menuFile->Append(wxID_EXIT, _("&Close frame"));
187 menuNav->Append(ID_Back, _("Go &BACK"));
188 menuNav->Append(ID_Forward, _("Go &FORWARD"));
189
190 // now append the freshly created menu to the menu bar...
191 wxMenuBar *menuBar = new wxMenuBar;
192 menuBar->Append(menuFile, _("&File"));
193 menuBar->Append(menuNav, _("&Navigate"));
194
195 // ... and attach this menu bar to the frame
196 SetMenuBar(menuBar);
197
198 SetIcon(wxIcon(sample_xpm));
199
200 #if wxUSE_ACCEL
201 // Create convenient accelerators for Back and Forward navigation
202 wxAcceleratorEntry entries[2];
203 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
204 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
205
206 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
207 SetAcceleratorTable(accel);
208 #endif // wxUSE_ACCEL
209
210 #if wxUSE_STATUSBAR
211 CreateStatusBar(2);
212 #endif // wxUSE_STATUSBAR
213
214 m_Processor = new BoldProcessor;
215 m_Processor->Enable(false);
216 m_Html = new MyHtmlWindow(this);
217 m_Html->SetRelatedFrame(this, _("HTML : %s"));
218 #if wxUSE_STATUSBAR
219 m_Html->SetRelatedStatusBar(0);
220 #endif // wxUSE_STATUSBAR
221 m_Html->ReadCustomization(wxConfig::Get());
222 m_Html->LoadFile(wxFileName(wxT("test.htm")));
223 m_Html->AddProcessor(m_Processor);
224 }
225
226
227 // event handlers
228
229 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
230 {
231 m_Html->WriteCustomization(wxConfig::Get());
232 delete wxConfig::Set(NULL);
233
234 // true is to force the frame to close
235 Close(true);
236 }
237
238 void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
239 {
240 #if wxUSE_FILEDLG
241 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
242 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm"));
243
244 if (!p.empty())
245 m_Html->LoadPage(p);
246 #endif // wxUSE_FILEDLG
247 }
248
249 void MyFrame::OnDefaultBrowser(wxCommandEvent& WXUNUSED(event))
250 {
251 wxString page = m_Html->GetOpenedPage();
252 if (!page.empty())
253 {
254 wxLaunchDefaultBrowser(page);
255 }
256 }
257
258 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
259 {
260 if (!m_Html->HistoryBack())
261 {
262 wxMessageBox(_("You reached prehistory era!"));
263 }
264 }
265
266 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
267 {
268 if (!m_Html->HistoryForward())
269 {
270 wxMessageBox(_("No more items in history!"));
271 }
272 }
273
274 void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
275 {
276 m_Processor->Enable(!m_Processor->IsEnabled());
277 m_Html->LoadPage(m_Html->GetOpenedPage());
278 }
279
280 wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
281 const wxString& url,
282 wxString *WXUNUSED(redirect)) const
283 {
284 GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1);
285 return wxHTML_OPEN;
286 }