]> git.saurik.com Git - wxWidgets.git/blame - samples/html/test/test.cpp
generate #line references to source .xrc files in wxrc --gettext (patch #1803492...
[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"
0f11c233
VZ
31#include "wx/clipbrd.h"
32#include "wx/dataobj.h"
5526e819 33
18922659
JS
34#include "../../sample.xpm"
35
5526e819
VS
36// ----------------------------------------------------------------------------
37// private classes
38// ----------------------------------------------------------------------------
39
40// Define a new application type, each program should derive a class from wxApp
3e729cbe
VS
41class MyApp : public wxApp
42{
43public:
3f6bd7c1 44 virtual bool OnInit();
3e729cbe 45};
5526e819 46
4bfa3189
WS
47// Define a new html window type: this is a wrapper for handling wxHtmlWindow events
48class MyHtmlWindow : public wxHtmlWindow
49{
50public:
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
57private:
0f11c233
VZ
58 void OnClipboardEvent(wxClipboardTextEvent& event);
59
60#if wxUSE_CLIPBOARD
61 DECLARE_EVENT_TABLE()
62#endif // wxUSE_CLIPBOARD
4bfa3189
WS
63 DECLARE_NO_COPY_CLASS(MyHtmlWindow)
64};
65
5526e819 66// Define a new frame type: this is going to be our main frame
3e729cbe
VS
67class MyFrame : public wxFrame
68{
69public:
3f6bd7c1 70 // ctor(s)
3e729cbe
VS
71 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
72
3f6bd7c1 73 // event handlers (these functions should _not_ be virtual)
3e729cbe
VS
74 void OnQuit(wxCommandEvent& event);
75 void OnPageOpen(wxCommandEvent& event);
17ede0b1
RR
76 void OnDefaultLocalBrowser(wxCommandEvent& event);
77 void OnDefaultWebBrowser(wxCommandEvent& event);
3e729cbe
VS
78 void OnBack(wxCommandEvent& event);
79 void OnForward(wxCommandEvent& event);
80 void OnProcessor(wxCommandEvent& event);
81
a1c3cdc4
VS
82 void OnHtmlLinkClicked(wxHtmlLinkEvent& event);
83 void OnHtmlCellHover(wxHtmlCellEvent &event);
84 void OnHtmlCellClicked(wxHtmlCellEvent &event);
85
3e729cbe 86private:
4bfa3189 87 MyHtmlWindow *m_Html;
3f6bd7c1
DS
88 wxHtmlProcessor *m_Processor;
89
be5a51fb 90 // Any class wishing to process wxWidgets events must use this macro
3f6bd7c1 91 DECLARE_EVENT_TABLE()
3e729cbe
VS
92};
93
94
95class BoldProcessor : public wxHtmlProcessor
96{
3f6bd7c1
DS
97public:
98 virtual wxString Process(const wxString& s) const
99 {
100 wxString r(s);
101 r.Replace(wxT("<b>"), wxEmptyString);
102 r.Replace(wxT("<B>"), wxEmptyString);
103 r.Replace(wxT("</b>"), wxEmptyString);
104 r.Replace(wxT("</B>"), wxEmptyString);
105
106 return r;
107 }
3e729cbe 108};
5526e819
VS
109
110// ----------------------------------------------------------------------------
111// constants
112// ----------------------------------------------------------------------------
113
114// IDs for the controls and the menu commands
3f6bd7c1
DS
115enum
116{
5526e819 117 // menu items
3f6bd7c1 118 ID_PageOpen = wxID_HIGHEST,
17ede0b1
RR
119 ID_DefaultLocalBrowser,
120 ID_DefaultWebBrowser,
3f6bd7c1
DS
121 ID_Back,
122 ID_Forward,
123 ID_Processor
124};
5526e819
VS
125
126// ----------------------------------------------------------------------------
be5a51fb 127// event tables and other macros for wxWidgets
5526e819
VS
128// ----------------------------------------------------------------------------
129
3f6bd7c1
DS
130BEGIN_EVENT_TABLE(MyFrame, wxFrame)
131 EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
132 EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen)
17ede0b1
RR
133 EVT_MENU(ID_DefaultLocalBrowser, MyFrame::OnDefaultLocalBrowser)
134 EVT_MENU(ID_DefaultWebBrowser, MyFrame::OnDefaultWebBrowser)
3f6bd7c1
DS
135 EVT_MENU(ID_Back, MyFrame::OnBack)
136 EVT_MENU(ID_Forward, MyFrame::OnForward)
137 EVT_MENU(ID_Processor, MyFrame::OnProcessor)
a1c3cdc4
VS
138
139 EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked)
140 EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover)
141 EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked)
3f6bd7c1
DS
142END_EVENT_TABLE()
143
144IMPLEMENT_APP(MyApp)
145
146// ============================================================================
147// implementation
148// ============================================================================
149
150// ----------------------------------------------------------------------------
151// the application class
152// ----------------------------------------------------------------------------
153
154// `Main program' equivalent: the program execution "starts" here
155bool MyApp::OnInit()
156{
45e6e6f8
VZ
157 if ( !wxApp::OnInit() )
158 return false;
159
954cc8d2 160#if wxUSE_SYSTEM_OPTIONS
3f6bd7c1 161 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
954cc8d2 162#endif
bbb8f29b 163
3f6bd7c1 164 wxInitAllImageHandlers();
954cc8d2 165#if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
3f6bd7c1 166 wxFileSystem::AddHandler(new wxInternetFSHandler);
954cc8d2 167#endif
5612e524 168
be5a51fb 169 SetVendorName(wxT("wxWidgets"));
3f6bd7c1
DS
170 SetAppName(wxT("wxHtmlTest"));
171 // the following call to wxConfig::Get will use it to create an object...
5612e524 172
5526e819 173 // Create the main application window
3f6bd7c1
DS
174 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
175 wxDefaultPosition, wxSize(640, 480));
176
177 frame->Show();
178
179 return true /* continue running */;
180}
5526e819
VS
181
182// ----------------------------------------------------------------------------
183// main frame
184// ----------------------------------------------------------------------------
185
5526e819 186// frame constructor
3f6bd7c1
DS
187MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
188 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size,
189 wxDEFAULT_FRAME_STYLE, wxT("html_test_app"))
190{
5526e819 191 // create a menu bar
3f6bd7c1
DS
192 wxMenu *menuFile = new wxMenu;
193 wxMenu *menuNav = new wxMenu;
194
195 menuFile->Append(ID_PageOpen, _("&Open HTML page..."));
17ede0b1
RR
196 menuFile->Append(ID_DefaultLocalBrowser, _("&Open current page with default browser"));
197 menuFile->Append(ID_DefaultWebBrowser, _("Open a &web page with default browser"));
3f6bd7c1
DS
198 menuFile->AppendSeparator();
199 menuFile->Append(ID_Processor, _("&Remove bold attribute"),
200 wxEmptyString, wxITEM_CHECK);
201
202 menuFile->AppendSeparator();
203 menuFile->Append(wxID_EXIT, _("&Close frame"));
204 menuNav->Append(ID_Back, _("Go &BACK"));
205 menuNav->Append(ID_Forward, _("Go &FORWARD"));
5526e819
VS
206
207 // now append the freshly created menu to the menu bar...
3f6bd7c1
DS
208 wxMenuBar *menuBar = new wxMenuBar;
209 menuBar->Append(menuFile, _("&File"));
210 menuBar->Append(menuNav, _("&Navigate"));
5526e819
VS
211
212 // ... and attach this menu bar to the frame
3f6bd7c1
DS
213 SetMenuBar(menuBar);
214
18922659 215 SetIcon(wxIcon(sample_xpm));
11fdee42 216
3f6bd7c1
DS
217#if wxUSE_ACCEL
218 // Create convenient accelerators for Back and Forward navigation
219 wxAcceleratorEntry entries[2];
220 entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back);
221 entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward);
222
223 wxAcceleratorTable accel(WXSIZEOF(entries), entries);
224 SetAcceleratorTable(accel);
225#endif // wxUSE_ACCEL
226
8520f137 227#if wxUSE_STATUSBAR
4bfa3189 228 CreateStatusBar(2);
8520f137 229#endif // wxUSE_STATUSBAR
3f6bd7c1
DS
230
231 m_Processor = new BoldProcessor;
232 m_Processor->Enable(false);
4bfa3189 233 m_Html = new MyHtmlWindow(this);
3f6bd7c1 234 m_Html->SetRelatedFrame(this, _("HTML : %s"));
8520f137 235#if wxUSE_STATUSBAR
3f6bd7c1 236 m_Html->SetRelatedStatusBar(0);
8520f137 237#endif // wxUSE_STATUSBAR
3f6bd7c1
DS
238 m_Html->ReadCustomization(wxConfig::Get());
239 m_Html->LoadFile(wxFileName(wxT("test.htm")));
240 m_Html->AddProcessor(m_Processor);
a1c3cdc4
VS
241
242 wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""),
243 wxDefaultPosition, wxDefaultSize,
244 wxTE_MULTILINE);
245
246 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text));
247
248 wxSizer *sz = new wxBoxSizer(wxVERTICAL);
249 sz->Add(m_Html, 3, wxGROW);
250 sz->Add(text, 1, wxGROW);
251 SetSizer(sz);
3f6bd7c1 252}
5526e819
VS
253
254
255// event handlers
256
3e729cbe
VS
257void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
258{
3f6bd7c1
DS
259 m_Html->WriteCustomization(wxConfig::Get());
260 delete wxConfig::Set(NULL);
261
262 // true is to force the frame to close
263 Close(true);
3e729cbe
VS
264}
265
266void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event))
267{
71307412 268#if wxUSE_FILEDLG
3f6bd7c1 269 wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString,
43056e6e 270 wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html"));
3f6bd7c1 271
71307412 272 if (!p.empty())
9dbadebe 273 m_Html->LoadFile(wxFileName(p));
71307412 274#endif // wxUSE_FILEDLG
3e729cbe
VS
275}
276
17ede0b1 277void MyFrame::OnDefaultLocalBrowser(wxCommandEvent& WXUNUSED(event))
d83c04e6
WS
278{
279 wxString page = m_Html->GetOpenedPage();
280 if (!page.empty())
281 {
282 wxLaunchDefaultBrowser(page);
283 }
284}
285
17ede0b1
RR
286void MyFrame::OnDefaultWebBrowser(wxCommandEvent& WXUNUSED(event))
287{
288 wxString page = m_Html->GetOpenedPage();
289 if (!page.empty())
290 {
291 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
292 }
293}
294
3e729cbe
VS
295void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
296{
3f6bd7c1
DS
297 if (!m_Html->HistoryBack())
298 {
299 wxMessageBox(_("You reached prehistory era!"));
300 }
3e729cbe
VS
301}
302
303void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
304{
3f6bd7c1
DS
305 if (!m_Html->HistoryForward())
306 {
307 wxMessageBox(_("No more items in history!"));
308 }
3e729cbe
VS
309}
310
311void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event))
312{
313 m_Processor->Enable(!m_Processor->IsEnabled());
314 m_Html->LoadPage(m_Html->GetOpenedPage());
3e729cbe 315}
4bfa3189 316
a1c3cdc4
VS
317void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event)
318{
319 wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str());
320
321 // skipping this event the default behaviour (load the clicked URL)
322 // will happen...
323 event.Skip();
324}
325
326void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event)
327{
34e20200 328 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
a1c3cdc4
VS
329 event.GetCell(), event.GetPoint().x, event.GetPoint().y);
330}
331
332void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event)
333{
34e20200 334 wxLogMessage(wxT("Click over cell %p at %d;%d"),
a1c3cdc4
VS
335 event.GetCell(), event.GetPoint().x, event.GetPoint().y);
336
337 // if we don't skip the event, OnHtmlLinkClicked won't be called!
338 event.Skip();
339}
340
4bfa3189
WS
341wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type),
342 const wxString& url,
343 wxString *WXUNUSED(redirect)) const
344{
345 GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1);
346 return wxHTML_OPEN;
347}
0f11c233
VZ
348
349#if wxUSE_CLIPBOARD
350BEGIN_EVENT_TABLE(MyHtmlWindow, wxHtmlWindow)
351 EVT_TEXT_COPY(wxID_ANY, MyHtmlWindow::OnClipboardEvent)
352END_EVENT_TABLE()
353
354void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event))
355{
356 // explicitly call wxHtmlWindow::CopySelection() method
357 // and show the first 100 characters of the text copied in the status bar
358 if ( CopySelection() )
359 {
360 wxTextDataObject data;
361 if ( wxTheClipboard && wxTheClipboard->GetData(data) )
362 {
363 const wxString text = data.GetText();
364 const size_t maxTextLength = 100;
365
366 wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"),
367 wxString(text, maxTextLength).c_str(),
368 (text.length() > maxTextLength) ? _T("...")
369 : _T("")));
370 return;
371 }
372 }
373
374 wxLogStatus(_T("Clipboard: nothing"));
375}
376#endif // wxUSE_CLIPBOARD