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