1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // For all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers
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"
31 #include "wx/clipbrd.h"
32 #include "wx/dataobj.h"
33 #include "wx/stopwatch.h"
35 #include "../../sample.xpm"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 // Define a new application type, each program should derive a class from wxApp
42 class MyApp
: public wxApp
45 virtual bool OnInit();
48 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
49 class MyHtmlWindow
: public wxHtmlWindow
52 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
)
54 // no custom background initially to avoid confusing people
55 m_drawCustomBg
= false;
58 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
59 const wxString
& WXUNUSED(url
),
60 wxString
*WXUNUSED(redirect
)) const;
62 // toggle drawing of custom background
63 void DrawCustomBg(bool draw
)
65 m_drawCustomBg
= draw
;
71 void OnClipboardEvent(wxClipboardTextEvent
& event
);
72 #endif // wxUSE_CLIPBOARD
73 void OnEraseBgEvent(wxEraseEvent
& event
);
78 wxDECLARE_NO_COPY_CLASS(MyHtmlWindow
);
81 // Define a new frame type: this is going to be our main frame
82 class MyFrame
: public wxFrame
86 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
88 // event handlers (these functions should _not_ be virtual)
89 void OnQuit(wxCommandEvent
& event
);
90 void OnPageOpen(wxCommandEvent
& event
);
91 void OnDefaultLocalBrowser(wxCommandEvent
& event
);
92 void OnDefaultWebBrowser(wxCommandEvent
& event
);
93 void OnBack(wxCommandEvent
& event
);
94 void OnForward(wxCommandEvent
& event
);
95 void OnProcessor(wxCommandEvent
& event
);
96 void OnDrawCustomBg(wxCommandEvent
& event
);
98 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
99 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
100 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
103 MyHtmlWindow
*m_Html
;
104 wxHtmlProcessor
*m_Processor
;
106 // Any class wishing to process wxWidgets events must use this macro
107 DECLARE_EVENT_TABLE()
111 class BoldProcessor
: public wxHtmlProcessor
114 virtual wxString
Process(const wxString
& s
) const
117 r
.Replace(wxT("<b>"), wxEmptyString
);
118 r
.Replace(wxT("<B>"), wxEmptyString
);
119 r
.Replace(wxT("</b>"), wxEmptyString
);
120 r
.Replace(wxT("</B>"), wxEmptyString
);
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 // IDs for the controls and the menu commands
134 ID_PageOpen
= wxID_HIGHEST
,
135 ID_DefaultLocalBrowser
,
136 ID_DefaultWebBrowser
,
143 // ----------------------------------------------------------------------------
144 // event tables and other macros for wxWidgets
145 // ----------------------------------------------------------------------------
147 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
148 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
149 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
150 EVT_MENU(ID_DefaultLocalBrowser
, MyFrame::OnDefaultLocalBrowser
)
151 EVT_MENU(ID_DefaultWebBrowser
, MyFrame::OnDefaultWebBrowser
)
152 EVT_MENU(ID_Back
, MyFrame::OnBack
)
153 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
154 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
155 EVT_MENU(ID_DrawCustomBg
, MyFrame::OnDrawCustomBg
)
157 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
158 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
159 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
164 // ============================================================================
166 // ============================================================================
168 // ----------------------------------------------------------------------------
169 // the application class
170 // ----------------------------------------------------------------------------
172 // `Main program' equivalent: the program execution "starts" here
175 if ( !wxApp::OnInit() )
178 #if wxUSE_SYSTEM_OPTIONS
179 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
182 wxInitAllImageHandlers();
183 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
184 wxFileSystem::AddHandler(new wxInternetFSHandler
);
187 SetVendorName(wxT("wxWidgets"));
188 SetAppName(wxT("wxHtmlTest"));
189 // the following call to wxConfig::Get will use it to create an object...
191 // Create the main application window
192 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
193 wxDefaultPosition
, wxSize(640, 480));
197 return true /* continue running */;
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
205 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
206 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
207 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
210 wxMenu
*menuFile
= new wxMenu
;
211 wxMenu
*menuNav
= new wxMenu
;
213 menuFile
->Append(ID_PageOpen
, _("&Open HTML page...\tCtrl-O"));
214 menuFile
->Append(ID_DefaultLocalBrowser
, _("&Open current page with default browser"));
215 menuFile
->Append(ID_DefaultWebBrowser
, _("Open a &web page with default browser"));
216 menuFile
->AppendSeparator();
217 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
218 wxEmptyString
, wxITEM_CHECK
);
219 menuFile
->AppendSeparator();
220 menuFile
->AppendCheckItem(ID_DrawCustomBg
, "&Draw custom background");
221 menuFile
->AppendSeparator();
222 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
223 menuNav
->Append(ID_Back
, _("Go &BACK"));
224 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
226 // now append the freshly created menu to the menu bar...
227 wxMenuBar
*menuBar
= new wxMenuBar
;
228 menuBar
->Append(menuFile
, _("&File"));
229 menuBar
->Append(menuNav
, _("&Navigate"));
231 // ... and attach this menu bar to the frame
234 SetIcon(wxIcon(sample_xpm
));
237 // Create convenient accelerators for Back and Forward navigation
238 wxAcceleratorEntry entries
[2];
239 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
240 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
242 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
243 SetAcceleratorTable(accel
);
244 #endif // wxUSE_ACCEL
248 #endif // wxUSE_STATUSBAR
250 m_Processor
= new BoldProcessor
;
251 m_Processor
->Enable(false);
252 m_Html
= new MyHtmlWindow(this);
253 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
255 m_Html
->SetRelatedStatusBar(1);
256 #endif // wxUSE_STATUSBAR
257 m_Html
->ReadCustomization(wxConfig::Get());
258 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
259 m_Html
->AddProcessor(m_Processor
);
261 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, wxT(""),
262 wxDefaultPosition
, wxDefaultSize
,
265 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
267 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
268 sz
->Add(m_Html
, 3, wxGROW
);
269 sz
->Add(text
, 1, wxGROW
);
276 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
278 m_Html
->WriteCustomization(wxConfig::Get());
279 delete wxConfig::Set(NULL
);
281 // true is to force the frame to close
285 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
288 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
289 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
296 m_Html
->LoadFile(wxFileName(p
));
298 wxLogStatus("Loaded \"%s\" in %lums", p
, sw
.Time());
301 #endif // wxUSE_FILEDLG
304 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
306 wxString page
= m_Html
->GetOpenedPage();
309 wxLaunchDefaultBrowser(page
);
313 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
315 wxString page
= m_Html
->GetOpenedPage();
318 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
322 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
324 if (!m_Html
->HistoryBack())
326 wxMessageBox(_("You reached prehistory era!"));
330 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
332 if (!m_Html
->HistoryForward())
334 wxMessageBox(_("No more items in history!"));
338 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
340 m_Processor
->Enable(!m_Processor
->IsEnabled());
341 m_Html
->LoadPage(m_Html
->GetOpenedPage());
344 void MyFrame::OnDrawCustomBg(wxCommandEvent
& event
)
346 m_Html
->DrawCustomBg(event
.IsChecked());
349 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
351 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
353 // skipping this event the default behaviour (load the clicked URL)
358 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
360 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
361 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
364 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
366 wxLogMessage(wxT("Click over cell %p at %d;%d"),
367 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
369 // if we don't skip the event, OnHtmlLinkClicked won't be called!
373 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
375 wxString
*WXUNUSED(redirect
)) const
377 GetRelatedFrame()->SetStatusText(url
+ wxT(" lately opened"),1);
381 BEGIN_EVENT_TABLE(MyHtmlWindow
, wxHtmlWindow
)
383 EVT_TEXT_COPY(wxID_ANY
, MyHtmlWindow::OnClipboardEvent
)
384 #endif // wxUSE_CLIPBOARD
385 EVT_ERASE_BACKGROUND(MyHtmlWindow::OnEraseBgEvent
)
389 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
391 // explicitly call wxHtmlWindow::CopySelection() method
392 // and show the first 100 characters of the text copied in the status bar
393 if ( CopySelection() )
395 wxTextDataObject data
;
396 if ( wxTheClipboard
&& wxTheClipboard
->Open() && wxTheClipboard
->GetData(data
) )
398 const wxString text
= data
.GetText();
399 const size_t maxTextLength
= 100;
401 wxLogStatus(wxString::Format(wxT("Clipboard: '%s%s'"),
402 wxString(text
, maxTextLength
).c_str(),
403 (text
.length() > maxTextLength
) ? wxT("...")
405 wxTheClipboard
->Close();
411 wxLogStatus(wxT("Clipboard: nothing"));
413 #endif // wxUSE_CLIPBOARD
415 void MyHtmlWindow::OnEraseBgEvent(wxEraseEvent
& event
)
417 if ( !m_drawCustomBg
)
423 // draw a background grid to show that this handler is indeed executed
425 wxDC
& dc
= *event
.GetDC();
426 dc
.SetPen(*wxBLUE_PEN
);
429 const wxSize size
= GetVirtualSize();
430 for ( int x
= 0; x
< size
.x
; x
+= 15 )
432 dc
.DrawLine(x
, 0, x
, size
.y
);
435 for ( int y
= 0; y
< size
.y
; y
+= 15 )
437 dc
.DrawLine(0, y
, size
.x
, y
);