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