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"
32 #include "../../sample.xpm"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // Define a new application type, each program should derive a class from wxApp
39 class MyApp
: public wxApp
42 virtual bool OnInit();
45 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
46 class MyHtmlWindow
: public wxHtmlWindow
49 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
51 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
52 const wxString
& WXUNUSED(url
),
53 wxString
*WXUNUSED(redirect
)) const;
56 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
64 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
66 // event handlers (these functions should _not_ be virtual)
67 void OnQuit(wxCommandEvent
& event
);
68 void OnPageOpen(wxCommandEvent
& event
);
69 void OnDefaultBrowser(wxCommandEvent
& event
);
70 void OnBack(wxCommandEvent
& event
);
71 void OnForward(wxCommandEvent
& event
);
72 void OnProcessor(wxCommandEvent
& event
);
76 wxHtmlProcessor
*m_Processor
;
78 // Any class wishing to process wxWidgets events must use this macro
83 class BoldProcessor
: public wxHtmlProcessor
86 virtual wxString
Process(const wxString
& s
) const
89 r
.Replace(wxT("<b>"), wxEmptyString
);
90 r
.Replace(wxT("<B>"), wxEmptyString
);
91 r
.Replace(wxT("</b>"), wxEmptyString
);
92 r
.Replace(wxT("</B>"), wxEmptyString
);
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 // IDs for the controls and the menu commands
106 ID_PageOpen
= wxID_HIGHEST
,
113 // ----------------------------------------------------------------------------
114 // event tables and other macros for wxWidgets
115 // ----------------------------------------------------------------------------
117 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
118 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
119 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
120 EVT_MENU(ID_DefaultBrowser
, MyFrame::OnDefaultBrowser
)
121 EVT_MENU(ID_Back
, MyFrame::OnBack
)
122 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
123 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
128 // ============================================================================
130 // ============================================================================
132 // ----------------------------------------------------------------------------
133 // the application class
134 // ----------------------------------------------------------------------------
136 // `Main program' equivalent: the program execution "starts" here
139 #if wxUSE_SYSTEM_OPTIONS
140 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
143 wxInitAllImageHandlers();
144 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
145 wxFileSystem::AddHandler(new wxInternetFSHandler
);
148 SetVendorName(wxT("wxWidgets"));
149 SetAppName(wxT("wxHtmlTest"));
150 // the following call to wxConfig::Get will use it to create an object...
152 // Create the main application window
153 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
154 wxDefaultPosition
, wxSize(640, 480));
158 return true /* continue running */;
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
166 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
167 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
168 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
171 wxMenu
*menuFile
= new wxMenu
;
172 wxMenu
*menuNav
= new wxMenu
;
174 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
175 menuFile
->Append(ID_DefaultBrowser
, _("&Open current page with default browser"));
176 menuFile
->AppendSeparator();
177 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
178 wxEmptyString
, wxITEM_CHECK
);
180 menuFile
->AppendSeparator();
181 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
182 menuNav
->Append(ID_Back
, _("Go &BACK"));
183 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
185 // now append the freshly created menu to the menu bar...
186 wxMenuBar
*menuBar
= new wxMenuBar
;
187 menuBar
->Append(menuFile
, _("&File"));
188 menuBar
->Append(menuNav
, _("&Navigate"));
190 // ... and attach this menu bar to the frame
193 SetIcon(wxIcon(sample_xpm
));
196 // Create convenient accelerators for Back and Forward navigation
197 wxAcceleratorEntry entries
[2];
198 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
199 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
201 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
202 SetAcceleratorTable(accel
);
203 #endif // wxUSE_ACCEL
207 #endif // wxUSE_STATUSBAR
209 m_Processor
= new BoldProcessor
;
210 m_Processor
->Enable(false);
211 m_Html
= new MyHtmlWindow(this);
212 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
214 m_Html
->SetRelatedStatusBar(0);
215 #endif // wxUSE_STATUSBAR
216 m_Html
->ReadCustomization(wxConfig::Get());
217 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
218 m_Html
->AddProcessor(m_Processor
);
224 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
226 m_Html
->WriteCustomization(wxConfig::Get());
227 delete wxConfig::Set(NULL
);
229 // true is to force the frame to close
233 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
236 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
237 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
240 m_Html
->LoadFile(wxFileName(p
));
241 #endif // wxUSE_FILEDLG
244 void MyFrame::OnDefaultBrowser(wxCommandEvent
& WXUNUSED(event
))
246 wxString page
= m_Html
->GetOpenedPage();
249 wxLaunchDefaultBrowser(page
);
253 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
255 if (!m_Html
->HistoryBack())
257 wxMessageBox(_("You reached prehistory era!"));
261 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
263 if (!m_Html
->HistoryForward())
265 wxMessageBox(_("No more items in history!"));
269 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
271 m_Processor
->Enable(!m_Processor
->IsEnabled());
272 m_Html
->LoadPage(m_Html
->GetOpenedPage());
275 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
277 wxString
*WXUNUSED(redirect
)) const
279 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);