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 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
55 const wxString
& WXUNUSED(url
),
56 wxString
*WXUNUSED(redirect
)) const;
59 void OnClipboardEvent(wxClipboardTextEvent
& event
);
63 #endif // wxUSE_CLIPBOARD
64 wxDECLARE_NO_COPY_CLASS(MyHtmlWindow
);
67 // Define a new frame type: this is going to be our main frame
68 class MyFrame
: public wxFrame
72 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
83 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
84 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
85 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
89 wxHtmlProcessor
*m_Processor
;
91 // Any class wishing to process wxWidgets events must use this macro
96 class BoldProcessor
: public wxHtmlProcessor
99 virtual wxString
Process(const wxString
& s
) const
102 r
.Replace(wxT("<b>"), wxEmptyString
);
103 r
.Replace(wxT("<B>"), wxEmptyString
);
104 r
.Replace(wxT("</b>"), wxEmptyString
);
105 r
.Replace(wxT("</B>"), wxEmptyString
);
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 // IDs for the controls and the menu commands
119 ID_PageOpen
= wxID_HIGHEST
,
120 ID_DefaultLocalBrowser
,
121 ID_DefaultWebBrowser
,
127 // ----------------------------------------------------------------------------
128 // event tables and other macros for wxWidgets
129 // ----------------------------------------------------------------------------
131 BEGIN_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
)
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
)
147 // ============================================================================
149 // ============================================================================
151 // ----------------------------------------------------------------------------
152 // the application class
153 // ----------------------------------------------------------------------------
155 // `Main program' equivalent: the program execution "starts" here
158 if ( !wxApp::OnInit() )
161 #if wxUSE_SYSTEM_OPTIONS
162 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
165 wxInitAllImageHandlers();
166 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
167 wxFileSystem::AddHandler(new wxInternetFSHandler
);
170 SetVendorName(wxT("wxWidgets"));
171 SetAppName(wxT("wxHtmlTest"));
172 // the following call to wxConfig::Get will use it to create an object...
174 // Create the main application window
175 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
176 wxDefaultPosition
, wxSize(640, 480));
180 return true /* continue running */;
183 // ----------------------------------------------------------------------------
185 // ----------------------------------------------------------------------------
188 MyFrame::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"))
193 wxMenu
*menuFile
= new wxMenu
;
194 wxMenu
*menuNav
= new wxMenu
;
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
);
203 menuFile
->AppendSeparator();
204 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
205 menuNav
->Append(ID_Back
, _("Go &BACK"));
206 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
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"));
213 // ... and attach this menu bar to the frame
216 SetIcon(wxIcon(sample_xpm
));
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
);
224 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
225 SetAcceleratorTable(accel
);
226 #endif // wxUSE_ACCEL
230 #endif // wxUSE_STATUSBAR
232 m_Processor
= new BoldProcessor
;
233 m_Processor
->Enable(false);
234 m_Html
= new MyHtmlWindow(this);
235 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
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
);
243 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
244 wxDefaultPosition
, wxDefaultSize
,
247 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
249 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
250 sz
->Add(m_Html
, 3, wxGROW
);
251 sz
->Add(text
, 1, wxGROW
);
258 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
260 m_Html
->WriteCustomization(wxConfig::Get());
261 delete wxConfig::Set(NULL
);
263 // true is to force the frame to close
267 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
270 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
271 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
278 m_Html
->LoadFile(wxFileName(p
));
280 wxLogStatus("Loaded \"%s\" in %lums", p
, sw
.Time());
283 #endif // wxUSE_FILEDLG
286 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
288 wxString page
= m_Html
->GetOpenedPage();
291 wxLaunchDefaultBrowser(page
);
295 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
297 wxString page
= m_Html
->GetOpenedPage();
300 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
304 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
306 if (!m_Html
->HistoryBack())
308 wxMessageBox(_("You reached prehistory era!"));
312 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
314 if (!m_Html
->HistoryForward())
316 wxMessageBox(_("No more items in history!"));
320 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
322 m_Processor
->Enable(!m_Processor
->IsEnabled());
323 m_Html
->LoadPage(m_Html
->GetOpenedPage());
326 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
328 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
330 // skipping this event the default behaviour (load the clicked URL)
335 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
337 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
338 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
341 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
343 wxLogMessage(wxT("Click over cell %p at %d;%d"),
344 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
346 // if we don't skip the event, OnHtmlLinkClicked won't be called!
350 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
352 wxString
*WXUNUSED(redirect
)) const
354 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);
359 BEGIN_EVENT_TABLE(MyHtmlWindow
, wxHtmlWindow
)
360 EVT_TEXT_COPY(wxID_ANY
, MyHtmlWindow::OnClipboardEvent
)
363 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
365 // explicitly call wxHtmlWindow::CopySelection() method
366 // and show the first 100 characters of the text copied in the status bar
367 if ( CopySelection() )
369 wxTextDataObject data
;
370 if ( wxTheClipboard
&& wxTheClipboard
->Open() && wxTheClipboard
->GetData(data
) )
372 const wxString text
= data
.GetText();
373 const size_t maxTextLength
= 100;
375 wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"),
376 wxString(text
, maxTextLength
).c_str(),
377 (text
.length() > maxTextLength
) ? _T("...")
379 wxTheClipboard
->Close();
385 wxLogStatus(_T("Clipboard: nothing"));
387 #endif // wxUSE_CLIPBOARD