More name changes
[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 frame type: this is going to be our main frame
48 class MyFrame : public wxFrame
49 {
50 public:
51 // ctor(s)
52 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
53
54 // event handlers (these functions should _not_ be virtual)
55 void OnQuit(wxCommandEvent& event);
56 void OnPageOpen(wxCommandEvent& event);
57 void OnBack(wxCommandEvent& event);
58 void OnForward(wxCommandEvent& event);
59 void OnProcessor(wxCommandEvent& event);
60
61 private:
62 wxHtmlWindow *m_Html;
63 wxHtmlProcessor *m_Processor;
64
65 // Any class wishing to process wxWidgets events must use this macro
66 DECLARE_EVENT_TABLE()
67 };
68
69
70 class BoldProcessor : public wxHtmlProcessor
71 {
72 public:
73 virtual wxString Process(const wxString& s) const
74 {
75 wxString r(s);
76 r.Replace(wxT("<b>"), wxEmptyString);
77 r.Replace(wxT("<B>"), wxEmptyString);
78 r.Replace(wxT("</b>"), wxEmptyString);
79 r.Replace(wxT("</B>"), wxEmptyString);
80
81 return r;
82 }
83 };
84
85 // ----------------------------------------------------------------------------
86 // constants
87 // ----------------------------------------------------------------------------
88
89 // IDs for the controls and the menu commands
90 enum
91 {
92 // menu items
93 ID_PageOpen = wxID_HIGHEST,
94 ID_Back,
95 ID_Forward,
96 ID_Processor
97 };
98
99 // ----------------------------------------------------------------------------
100 // event tables and other macros for wxWidgets
101 // ----------------------------------------------------------------------------
102
103 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
104 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
105 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
106 EVT_MENU(ID_Back, MyFrame::OnBack)
107 EVT_MENU(ID_Forward, MyFrame::OnForward)
108 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
109 END_EVENT_TABLE()
110
111 IMPLEMENT_APP(MyApp)
112
113 // ============================================================================
114 // implementation
115 // ============================================================================
116
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
120
121 // `Main program' equivalent: the program execution "starts" here
122 bool MyApp::OnInit()
123 {
124 #if wxUSE_SYSTEM_OPTIONS
125 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
126 #endif
127
128 wxInitAllImageHandlers();
129 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
130 wxFileSystem::AddHandler(new wxInternetFSHandler);
131 #endif
132
133 SetVendorName(wxT("wxWidgets"));
134 SetAppName(wxT("wxHtmlTest"));
135 // the following call to wxConfig::Get will use it to create an object...
136
137 // Create the main application window
138 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
139 wxDefaultPosition, wxSize(640, 480));
140
141 frame->Show();
142
143 return true /* continue running */;
144 }
145
146 // ----------------------------------------------------------------------------
147 // main frame
148 // ----------------------------------------------------------------------------
149
150 // frame constructor
151 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
152 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
153 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
154 {
155 // create a menu bar
156 wxMenu *menuFile = new wxMenu;
157 wxMenu *menuNav = new wxMenu;
158
159 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
160 menuFile->AppendSeparator();
161 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
162 wxEmptyString, wxITEM_CHECK);
163
164 menuFile->AppendSeparator();
165 menuFile->Append(wxID_EXIT, _("&Close frame"));
166 menuNav->Append(ID_Back, _("Go &BACK"));
167 menuNav->Append(ID_Forward, _("Go &FORWARD"));
168
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar *menuBar = new wxMenuBar;
171 menuBar->Append(menuFile, _("&File"));
172 menuBar->Append(menuNav, _("&Navigate"));
173
174 // ... and attach this menu bar to the frame
175 SetMenuBar(menuBar);
176
177 #if wxUSE_ACCEL
178 // Create convenient accelerators for Back and Forward navigation
179 wxAcceleratorEntry entries[2];
180 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
181 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
182
183 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
184 SetAcceleratorTable(accel);
185 #endif // wxUSE_ACCEL
186
187 CreateStatusBar(1);
188
189 m_Processor = new BoldProcessor;
190 m_Processor->Enable(false);
191 m_Html = new wxHtmlWindow(this);
192 m_Html->SetRelatedFrame(this, _("HTML : %s"));
193 m_Html->SetRelatedStatusBar(0);
194 m_Html->ReadCustomization(wxConfig::Get());
195 m_Html->LoadFile(wxFileName(wxT("test.htm")));
196 m_Html->AddProcessor(m_Processor);
197 }
198
199
200 // event handlers
201
202 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
203 {
204 m_Html->WriteCustomization(wxConfig::Get());
205 delete wxConfig::Set(NULL);
206
207 // true is to force the frame to close
208 Close(true);
209 }
210
211 void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
212 {
213 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
214 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm"));
215
216 if (p != wxEmptyString)
217 m_Html->LoadPage(p);
218 }
219
220 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
221 {
222 if (!m_Html->HistoryBack())
223 {
224 wxMessageBox(_("You reached prehistory era!"));
225 }
226 }
227
228 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
229 {
230 if (!m_Html->HistoryForward())
231 {
232 wxMessageBox(_("No more items in history!"));
233 }
234 }
235
236 void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
237 {
238 m_Processor->Enable(!m_Processor->IsEnabled());
239 m_Html->LoadPage(m_Html->GetOpenedPage());
240 }