1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx/wx.h".
11 #include "wx/wxprec.h"
17 // For all others, include the necessary headers (this file is usually all you
18 // need because it includes almost all "standard" wxWidgets headers
24 #include "wx/sysopt.h"
25 #include "wx/html/htmlwin.h"
26 #include "wx/html/htmlproc.h"
27 #include "wx/fs_inet.h"
28 #include "wx/filedlg.h"
30 #include "wx/clipbrd.h"
31 #include "wx/dataobj.h"
32 #include "wx/stopwatch.h"
34 #include "../../sample.xpm"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // Define a new application type, each program should derive a class from wxApp
41 class MyApp
: public wxApp
44 virtual bool OnInit();
47 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
48 class MyHtmlWindow
: public wxHtmlWindow
51 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
)
53 // no custom background initially to avoid confusing people
54 m_drawCustomBg
= false;
57 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
58 const wxString
& WXUNUSED(url
),
59 wxString
*WXUNUSED(redirect
)) const;
61 // toggle drawing of custom background
62 void DrawCustomBg(bool draw
)
64 m_drawCustomBg
= draw
;
70 void OnClipboardEvent(wxClipboardTextEvent
& event
);
71 #endif // wxUSE_CLIPBOARD
72 void OnEraseBgEvent(wxEraseEvent
& event
);
77 wxDECLARE_NO_COPY_CLASS(MyHtmlWindow
);
80 // Define a new frame type: this is going to be our main frame
81 class MyFrame
: public wxFrame
85 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
87 // event handlers (these functions should _not_ be virtual)
88 void OnQuit(wxCommandEvent
& event
);
89 void OnPageOpen(wxCommandEvent
& event
);
90 void OnDefaultLocalBrowser(wxCommandEvent
& event
);
91 void OnDefaultWebBrowser(wxCommandEvent
& event
);
92 void OnBack(wxCommandEvent
& event
);
93 void OnForward(wxCommandEvent
& event
);
94 void OnProcessor(wxCommandEvent
& event
);
95 void OnDrawCustomBg(wxCommandEvent
& event
);
97 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
98 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
99 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
102 MyHtmlWindow
*m_Html
;
103 wxHtmlProcessor
*m_Processor
;
105 // Any class wishing to process wxWidgets events must use this macro
106 DECLARE_EVENT_TABLE()
110 class BoldProcessor
: public wxHtmlProcessor
113 virtual wxString
Process(const wxString
& s
) const
116 r
.Replace(wxT("<b>"), wxEmptyString
);
117 r
.Replace(wxT("<B>"), wxEmptyString
);
118 r
.Replace(wxT("</b>"), wxEmptyString
);
119 r
.Replace(wxT("</B>"), wxEmptyString
);
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 // IDs for the controls and the menu commands
133 ID_PageOpen
= wxID_HIGHEST
,
134 ID_DefaultLocalBrowser
,
135 ID_DefaultWebBrowser
,
142 // ----------------------------------------------------------------------------
143 // event tables and other macros for wxWidgets
144 // ----------------------------------------------------------------------------
146 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
147 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
148 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
149 EVT_MENU(ID_DefaultLocalBrowser
, MyFrame::OnDefaultLocalBrowser
)
150 EVT_MENU(ID_DefaultWebBrowser
, MyFrame::OnDefaultWebBrowser
)
151 EVT_MENU(ID_Back
, MyFrame::OnBack
)
152 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
153 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
154 EVT_MENU(ID_DrawCustomBg
, MyFrame::OnDrawCustomBg
)
156 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
157 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
158 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
163 // ============================================================================
165 // ============================================================================
167 // ----------------------------------------------------------------------------
168 // the application class
169 // ----------------------------------------------------------------------------
171 // `Main program' equivalent: the program execution "starts" here
174 if ( !wxApp::OnInit() )
177 #if wxUSE_SYSTEM_OPTIONS
178 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
181 wxInitAllImageHandlers();
182 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
183 wxFileSystem::AddHandler(new wxInternetFSHandler
);
186 SetVendorName(wxT("wxWidgets"));
187 SetAppName(wxT("wxHtmlTest"));
188 // the following call to wxConfig::Get will use it to create an object...
190 // Create the main application window
191 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
192 wxDefaultPosition
, wxSize(640, 480));
196 return true /* continue running */;
199 // ----------------------------------------------------------------------------
201 // ----------------------------------------------------------------------------
204 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
205 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
206 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
209 wxMenu
*menuFile
= new wxMenu
;
210 wxMenu
*menuNav
= new wxMenu
;
212 menuFile
->Append(ID_PageOpen
, _("&Open HTML page...\tCtrl-O"));
213 menuFile
->Append(ID_DefaultLocalBrowser
, _("&Open current page with default browser"));
214 menuFile
->Append(ID_DefaultWebBrowser
, _("Open a &web page with default browser"));
215 menuFile
->AppendSeparator();
216 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
217 wxEmptyString
, wxITEM_CHECK
);
218 menuFile
->AppendSeparator();
219 menuFile
->AppendCheckItem(ID_DrawCustomBg
, "&Draw custom background");
220 menuFile
->AppendSeparator();
221 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
222 menuNav
->Append(ID_Back
, _("Go &BACK"));
223 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
225 // now append the freshly created menu to the menu bar...
226 wxMenuBar
*menuBar
= new wxMenuBar
;
227 menuBar
->Append(menuFile
, _("&File"));
228 menuBar
->Append(menuNav
, _("&Navigate"));
230 // ... and attach this menu bar to the frame
233 SetIcon(wxIcon(sample_xpm
));
236 // Create convenient accelerators for Back and Forward navigation
237 wxAcceleratorEntry entries
[2];
238 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
239 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
241 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
242 SetAcceleratorTable(accel
);
243 #endif // wxUSE_ACCEL
247 #endif // wxUSE_STATUSBAR
249 m_Processor
= new BoldProcessor
;
250 m_Processor
->Enable(false);
251 m_Html
= new MyHtmlWindow(this);
252 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
254 m_Html
->SetRelatedStatusBar(1);
255 #endif // wxUSE_STATUSBAR
256 m_Html
->ReadCustomization(wxConfig::Get());
257 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
258 m_Html
->AddProcessor(m_Processor
);
260 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
261 wxDefaultPosition
, wxDefaultSize
,
264 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
266 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
267 sz
->Add(m_Html
, 3, wxGROW
);
268 sz
->Add(text
, 1, wxGROW
);
275 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
277 m_Html
->WriteCustomization(wxConfig::Get());
278 delete wxConfig::Set(NULL
);
280 // true is to force the frame to close
284 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
287 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
288 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
295 m_Html
->LoadFile(wxFileName(p
));
297 wxLogStatus("Loaded \"%s\" in %lums", p
, sw
.Time());
300 #endif // wxUSE_FILEDLG
303 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
305 wxString page
= m_Html
->GetOpenedPage();
308 wxLaunchDefaultBrowser(page
);
312 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
314 wxString page
= m_Html
->GetOpenedPage();
317 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
321 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
323 if (!m_Html
->HistoryBack())
325 wxMessageBox(_("You reached prehistory era!"));
329 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
331 if (!m_Html
->HistoryForward())
333 wxMessageBox(_("No more items in history!"));
337 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
339 m_Processor
->Enable(!m_Processor
->IsEnabled());
340 m_Html
->LoadPage(m_Html
->GetOpenedPage());
343 void MyFrame::OnDrawCustomBg(wxCommandEvent
& event
)
345 m_Html
->DrawCustomBg(event
.IsChecked());
348 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
350 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
352 // skipping this event the default behaviour (load the clicked URL)
357 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
359 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
360 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
363 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
365 wxLogMessage(wxT("Click over cell %p at %d;%d"),
366 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
368 // if we don't skip the event, OnHtmlLinkClicked won't be called!
372 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
374 wxString
*WXUNUSED(redirect
)) const
376 GetRelatedFrame()->SetStatusText(url
+ wxT(" lately opened"),1);
380 BEGIN_EVENT_TABLE(MyHtmlWindow
, wxHtmlWindow
)
382 EVT_TEXT_COPY(wxID_ANY
, MyHtmlWindow::OnClipboardEvent
)
383 #endif // wxUSE_CLIPBOARD
384 EVT_ERASE_BACKGROUND(MyHtmlWindow::OnEraseBgEvent
)
388 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
390 // explicitly call wxHtmlWindow::CopySelection() method
391 // and show the first 100 characters of the text copied in the status bar
392 if ( CopySelection() )
394 wxTextDataObject data
;
395 if ( wxTheClipboard
&& wxTheClipboard
->Open() && wxTheClipboard
->GetData(data
) )
397 const wxString text
= data
.GetText();
398 const size_t maxTextLength
= 100;
400 wxLogStatus(wxString::Format(wxT("Clipboard: '%s%s'"),
401 wxString(text
, maxTextLength
).c_str(),
402 (text
.length() > maxTextLength
) ? wxT("...")
404 wxTheClipboard
->Close();
410 wxLogStatus(wxT("Clipboard: nothing"));
412 #endif // wxUSE_CLIPBOARD
414 void MyHtmlWindow::OnEraseBgEvent(wxEraseEvent
& event
)
416 if ( !m_drawCustomBg
)
422 // draw a background grid to show that this handler is indeed executed
424 wxDC
& dc
= *event
.GetDC();
425 dc
.SetPen(*wxBLUE_PEN
);
428 const wxSize size
= GetVirtualSize();
429 for ( int x
= 0; x
< size
.x
; x
+= 15 )
431 dc
.DrawLine(x
, 0, x
, size
.y
);
434 for ( int y
= 0; y
< size
.y
; y
+= 15 )
436 dc
.DrawLine(0, y
, size
.x
, y
);