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 "test.cpp"
13 #pragma interface "test.cpp"
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"
36 #include "../../sample.xpm"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // Define a new application type, each program should derive a class from wxApp
43 class MyApp
: public wxApp
46 virtual bool OnInit();
49 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
50 class MyHtmlWindow
: public wxHtmlWindow
53 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
55 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
56 const wxString
& WXUNUSED(url
),
57 wxString
*WXUNUSED(redirect
)) const;
60 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
63 // Define a new frame type: this is going to be our main frame
64 class MyFrame
: public wxFrame
68 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
70 // event handlers (these functions should _not_ be virtual)
71 void OnQuit(wxCommandEvent
& event
);
72 void OnPageOpen(wxCommandEvent
& event
);
73 void OnBack(wxCommandEvent
& event
);
74 void OnForward(wxCommandEvent
& event
);
75 void OnProcessor(wxCommandEvent
& event
);
79 wxHtmlProcessor
*m_Processor
;
81 // Any class wishing to process wxWidgets events must use this macro
86 class BoldProcessor
: public wxHtmlProcessor
89 virtual wxString
Process(const wxString
& s
) const
92 r
.Replace(wxT("<b>"), wxEmptyString
);
93 r
.Replace(wxT("<B>"), wxEmptyString
);
94 r
.Replace(wxT("</b>"), wxEmptyString
);
95 r
.Replace(wxT("</B>"), wxEmptyString
);
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // IDs for the controls and the menu commands
109 ID_PageOpen
= wxID_HIGHEST
,
115 // ----------------------------------------------------------------------------
116 // event tables and other macros for wxWidgets
117 // ----------------------------------------------------------------------------
119 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
120 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
121 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
122 EVT_MENU(ID_Back
, MyFrame::OnBack
)
123 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
124 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
134 // the application class
135 // ----------------------------------------------------------------------------
137 // `Main program' equivalent: the program execution "starts" here
140 #if wxUSE_SYSTEM_OPTIONS
141 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
144 wxInitAllImageHandlers();
145 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
146 wxFileSystem::AddHandler(new wxInternetFSHandler
);
149 SetVendorName(wxT("wxWidgets"));
150 SetAppName(wxT("wxHtmlTest"));
151 // the following call to wxConfig::Get will use it to create an object...
153 // Create the main application window
154 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
155 wxDefaultPosition
, wxSize(640, 480));
159 return true /* continue running */;
162 // ----------------------------------------------------------------------------
164 // ----------------------------------------------------------------------------
167 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
168 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
169 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
172 wxMenu
*menuFile
= new wxMenu
;
173 wxMenu
*menuNav
= new wxMenu
;
175 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
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
))
235 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
236 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm"));
238 if (p
!= wxEmptyString
)
242 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
244 if (!m_Html
->HistoryBack())
246 wxMessageBox(_("You reached prehistory era!"));
250 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
252 if (!m_Html
->HistoryForward())
254 wxMessageBox(_("No more items in history!"));
258 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
260 m_Processor
->Enable(!m_Processor
->IsEnabled());
261 m_Html
->LoadPage(m_Html
->GetOpenedPage());
264 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
266 wxString
*WXUNUSED(redirect
)) const
268 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);