]> git.saurik.com Git - wxWidgets.git/blame - samples/html/test/test.cpp
post WM_CLOSE instead of deleting the inplace edit control (second incarnation of...
[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);
d83c04e6 69 void OnDefaultBrowser(wxCommandEvent& event);
3e729cbe
VS
70 void OnBack(wxCommandEvent& event);
71 void OnForward(wxCommandEvent& event);
72 void OnProcessor(wxCommandEvent& event);
73
74private:
4bfa3189 75 MyHtmlWindow *m_Html;
3f6bd7c1
DS
76 wxHtmlProcessor *m_Processor;
77
be5a51fb 78 // Any class wishing to process wxWidgets events must use this macro
3f6bd7c1 79 DECLARE_EVENT_TABLE()
3e729cbe
VS
80};
81
82
83class BoldProcessor : public wxHtmlProcessor
84{
3f6bd7c1
DS
85public:
86 virtual wxString Process(const wxString& s) const
87 {
88 wxString r(s);
89 r.Replace(wxT("<b>"), wxEmptyString);
90 r.Replace(wxT("<B>"), wxEmptyString);
91 r.Replace(wxT("</b>"), wxEmptyString);
92 r.Replace(wxT("</B>"), wxEmptyString);
93
94 return r;
95 }
3e729cbe 96};
5526e819
VS
97
98// ----------------------------------------------------------------------------
99// constants
100// ----------------------------------------------------------------------------
101
102// IDs for the controls and the menu commands
3f6bd7c1
DS
103enum
104{
5526e819 105 // menu items
3f6bd7c1 106 ID_PageOpen = wxID_HIGHEST,
d83c04e6 107 ID_DefaultBrowser,
3f6bd7c1
DS
108 ID_Back,
109 ID_Forward,
110 ID_Processor
111};
5526e819
VS
112
113// ----------------------------------------------------------------------------
be5a51fb 114// event tables and other macros for wxWidgets
5526e819
VS
115// ----------------------------------------------------------------------------
116
3f6bd7c1
DS
117BEGIN_EVENT_TABLE(MyFrame, wxFrame)
118 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
119 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
d83c04e6 120 EVT_MENU(ID_DefaultBrowser, MyFrame::OnDefaultBrowser)
3f6bd7c1
DS
121 EVT_MENU(ID_Back, MyFrame::OnBack)
122 EVT_MENU(ID_Forward, MyFrame::OnForward)
123 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
124END_EVENT_TABLE()
125
126IMPLEMENT_APP(MyApp)
127
128// ============================================================================
129// implementation
130// ============================================================================
131
132// ----------------------------------------------------------------------------
133// the application class
134// ----------------------------------------------------------------------------
135
136// `Main program' equivalent: the program execution "starts" here
137bool MyApp::OnInit()
138{
954cc8d2 139#if wxUSE_SYSTEM_OPTIONS
3f6bd7c1 140 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
954cc8d2 141#endif
bbb8f29b 142
3f6bd7c1 143 wxInitAllImageHandlers();
954cc8d2 144#if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
3f6bd7c1 145 wxFileSystem::AddHandler(new wxInternetFSHandler);
954cc8d2 146#endif
5612e524 147
be5a51fb 148 SetVendorName(wxT("wxWidgets"));
3f6bd7c1
DS
149 SetAppName(wxT("wxHtmlTest"));
150 // the following call to wxConfig::Get will use it to create an object...
5612e524 151
5526e819 152 // Create the main application window
3f6bd7c1
DS
153 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
154 wxDefaultPosition, wxSize(640, 480));
155
156 frame->Show();
157
158 return true /* continue running */;
159}
5526e819
VS
160
161// ----------------------------------------------------------------------------
162// main frame
163// ----------------------------------------------------------------------------
164
5526e819 165// frame constructor
3f6bd7c1
DS
166MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
167 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
168 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
169{
5526e819 170 // create a menu bar
3f6bd7c1
DS
171 wxMenu *menuFile = new wxMenu;
172 wxMenu *menuNav = new wxMenu;
173
174 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
d83c04e6 175 menuFile->Append(ID_DefaultBrowser, _("&Open current page with default browser"));
3f6bd7c1
DS
176 menuFile->AppendSeparator();
177 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
178 wxEmptyString, wxITEM_CHECK);
179
180 menuFile->AppendSeparator();
181 menuFile->Append(wxID_EXIT, _("&Close frame"));
182 menuNav->Append(ID_Back, _("Go &BACK"));
183 menuNav->Append(ID_Forward, _("Go &FORWARD"));
5526e819
VS
184
185 // now append the freshly created menu to the menu bar...
3f6bd7c1
DS
186 wxMenuBar *menuBar = new wxMenuBar;
187 menuBar->Append(menuFile, _("&File"));
188 menuBar->Append(menuNav, _("&Navigate"));
5526e819
VS
189
190 // ... and attach this menu bar to the frame
3f6bd7c1
DS
191 SetMenuBar(menuBar);
192
18922659 193 SetIcon(wxIcon(sample_xpm));
11fdee42 194
3f6bd7c1
DS
195#if wxUSE_ACCEL
196 // Create convenient accelerators for Back and Forward navigation
197 wxAcceleratorEntry entries[2];
198 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
199 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
200
201 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
202 SetAcceleratorTable(accel);
203#endif // wxUSE_ACCEL
204
8520f137 205#if wxUSE_STATUSBAR
4bfa3189 206 CreateStatusBar(2);
8520f137 207#endif // wxUSE_STATUSBAR
3f6bd7c1
DS
208
209 m_Processor = new BoldProcessor;
210 m_Processor->Enable(false);
4bfa3189 211 m_Html = new MyHtmlWindow(this);
3f6bd7c1 212 m_Html->SetRelatedFrame(this, _("HTML : %s"));
8520f137 213#if wxUSE_STATUSBAR
3f6bd7c1 214 m_Html->SetRelatedStatusBar(0);
8520f137 215#endif // wxUSE_STATUSBAR
3f6bd7c1
DS
216 m_Html->ReadCustomization(wxConfig::Get());
217 m_Html->LoadFile(wxFileName(wxT("test.htm")));
218 m_Html->AddProcessor(m_Processor);
219}
5526e819
VS
220
221
222// event handlers
223
3e729cbe
VS
224void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
225{
3f6bd7c1
DS
226 m_Html->WriteCustomization(wxConfig::Get());
227 delete wxConfig::Set(NULL);
228
229 // true is to force the frame to close
230 Close(true);
3e729cbe
VS
231}
232
233void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
234{
71307412 235#if wxUSE_FILEDLG
3f6bd7c1 236 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
43056e6e 237 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html"));
3f6bd7c1 238
71307412 239 if (!p.empty())
9dbadebe 240 m_Html->LoadFile(wxFileName(p));
71307412 241#endif // wxUSE_FILEDLG
3e729cbe
VS
242}
243
d83c04e6
WS
244void MyFrame::OnDefaultBrowser(wxCommandEvent& WXUNUSED(event))
245{
246 wxString page = m_Html->GetOpenedPage();
247 if (!page.empty())
248 {
249 wxLaunchDefaultBrowser(page);
250 }
251}
252
3e729cbe
VS
253void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
254{
3f6bd7c1
DS
255 if (!m_Html->HistoryBack())
256 {
257 wxMessageBox(_("You reached prehistory era!"));
258 }
3e729cbe
VS
259}
260
261void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
262{
3f6bd7c1
DS
263 if (!m_Html->HistoryForward())
264 {
265 wxMessageBox(_("No more items in history!"));
266 }
3e729cbe
VS
267}
268
269void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
270{
271 m_Processor->Enable(!m_Processor->IsEnabled());
272 m_Html->LoadPage(m_Html->GetOpenedPage());
3e729cbe 273}
4bfa3189
WS
274
275wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
276 const wxString& url,
277 wxString *WXUNUSED(redirect)) const
278{
279 GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1);
280 return wxHTML_OPEN;
281}