1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(__APPLE__)
12 #pragma implementation
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
23 // For all others, include the necessary headers (this file is usually all you
24 // need because it includes almost all "standard" wxWidgets headers
30 #include "wx/sysopt.h"
31 #include "wx/html/htmlwin.h"
32 #include "wx/html/htmlproc.h"
33 #include "wx/fs_inet.h"
34 #include "wx/filedlg.h"
37 #include "../../sample.xpm"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // Define a new application type, each program should derive a class from wxApp
44 class MyApp
: public wxApp
47 virtual bool OnInit();
50 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
51 class MyHtmlWindow
: public wxHtmlWindow
54 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
56 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
57 const wxString
& WXUNUSED(url
),
58 wxString
*WXUNUSED(redirect
)) const;
61 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
64 // Define a new frame type: this is going to be our main frame
65 class MyFrame
: public wxFrame
69 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
71 // event handlers (these functions should _not_ be virtual)
72 void OnQuit(wxCommandEvent
& event
);
73 void OnPageOpen(wxCommandEvent
& event
);
74 void OnDefaultBrowser(wxCommandEvent
& event
);
75 void OnBack(wxCommandEvent
& event
);
76 void OnForward(wxCommandEvent
& event
);
77 void OnProcessor(wxCommandEvent
& event
);
81 wxHtmlProcessor
*m_Processor
;
83 // Any class wishing to process wxWidgets events must use this macro
88 class BoldProcessor
: public wxHtmlProcessor
91 virtual wxString
Process(const wxString
& s
) const
94 r
.Replace(wxT("<b>"), wxEmptyString
);
95 r
.Replace(wxT("<B>"), wxEmptyString
);
96 r
.Replace(wxT("</b>"), wxEmptyString
);
97 r
.Replace(wxT("</B>"), wxEmptyString
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
111 ID_PageOpen
= wxID_HIGHEST
,
118 // ----------------------------------------------------------------------------
119 // event tables and other macros for wxWidgets
120 // ----------------------------------------------------------------------------
122 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
123 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
124 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
125 EVT_MENU(ID_DefaultBrowser
, MyFrame::OnDefaultBrowser
)
126 EVT_MENU(ID_Back
, MyFrame::OnBack
)
127 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
128 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
138 // the application class
139 // ----------------------------------------------------------------------------
141 // `Main program' equivalent: the program execution "starts" here
144 #if wxUSE_SYSTEM_OPTIONS
145 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
148 wxInitAllImageHandlers();
149 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
150 wxFileSystem::AddHandler(new wxInternetFSHandler
);
153 SetVendorName(wxT("wxWidgets"));
154 SetAppName(wxT("wxHtmlTest"));
155 // the following call to wxConfig::Get will use it to create an object...
157 // Create the main application window
158 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
159 wxDefaultPosition
, wxSize(640, 480));
163 return true /* continue running */;
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
171 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
172 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
173 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
176 wxMenu
*menuFile
= new wxMenu
;
177 wxMenu
*menuNav
= new wxMenu
;
179 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
180 menuFile
->Append(ID_DefaultBrowser
, _("&Open current page with default browser"));
181 menuFile
->AppendSeparator();
182 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
183 wxEmptyString
, wxITEM_CHECK
);
185 menuFile
->AppendSeparator();
186 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
187 menuNav
->Append(ID_Back
, _("Go &BACK"));
188 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
190 // now append the freshly created menu to the menu bar...
191 wxMenuBar
*menuBar
= new wxMenuBar
;
192 menuBar
->Append(menuFile
, _("&File"));
193 menuBar
->Append(menuNav
, _("&Navigate"));
195 // ... and attach this menu bar to the frame
198 SetIcon(wxIcon(sample_xpm
));
201 // Create convenient accelerators for Back and Forward navigation
202 wxAcceleratorEntry entries
[2];
203 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
204 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
206 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
207 SetAcceleratorTable(accel
);
208 #endif // wxUSE_ACCEL
212 #endif // wxUSE_STATUSBAR
214 m_Processor
= new BoldProcessor
;
215 m_Processor
->Enable(false);
216 m_Html
= new MyHtmlWindow(this);
217 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
219 m_Html
->SetRelatedStatusBar(0);
220 #endif // wxUSE_STATUSBAR
221 m_Html
->ReadCustomization(wxConfig::Get());
222 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
223 m_Html
->AddProcessor(m_Processor
);
229 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
231 m_Html
->WriteCustomization(wxConfig::Get());
232 delete wxConfig::Set(NULL
);
234 // true is to force the frame to close
238 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
241 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
242 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm"));
246 #endif // wxUSE_FILEDLG
249 void MyFrame::OnDefaultBrowser(wxCommandEvent
& WXUNUSED(event
))
251 wxString page
= m_Html
->GetOpenedPage();
254 wxLaunchDefaultBrowser(page
);
258 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
260 if (!m_Html
->HistoryBack())
262 wxMessageBox(_("You reached prehistory era!"));
266 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
268 if (!m_Html
->HistoryForward())
270 wxMessageBox(_("No more items in history!"));
274 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
276 m_Processor
->Enable(!m_Processor
->IsEnabled());
277 m_Html
->LoadPage(m_Html
->GetOpenedPage());
280 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
282 wxString
*WXUNUSED(redirect
)) const
284 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);