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 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // Define a new application type, each program should derive a class from wxApp
41 class MyApp
: public wxApp
44 virtual bool OnInit();
47 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
48 class MyHtmlWindow
: public wxHtmlWindow
51 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
53 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
54 const wxString
& WXUNUSED(url
),
55 wxString
*WXUNUSED(redirect
)) const;
58 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
61 // Define a new frame type: this is going to be our main frame
62 class MyFrame
: public wxFrame
66 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
68 // event handlers (these functions should _not_ be virtual)
69 void OnQuit(wxCommandEvent
& event
);
70 void OnPageOpen(wxCommandEvent
& event
);
71 void OnBack(wxCommandEvent
& event
);
72 void OnForward(wxCommandEvent
& event
);
73 void OnProcessor(wxCommandEvent
& event
);
77 wxHtmlProcessor
*m_Processor
;
79 // Any class wishing to process wxWidgets events must use this macro
84 class BoldProcessor
: public wxHtmlProcessor
87 virtual wxString
Process(const wxString
& s
) const
90 r
.Replace(wxT("<b>"), wxEmptyString
);
91 r
.Replace(wxT("<B>"), wxEmptyString
);
92 r
.Replace(wxT("</b>"), wxEmptyString
);
93 r
.Replace(wxT("</B>"), wxEmptyString
);
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // IDs for the controls and the menu commands
107 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_Back
, MyFrame::OnBack
)
121 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
122 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
127 // ============================================================================
129 // ============================================================================
131 // ----------------------------------------------------------------------------
132 // the application class
133 // ----------------------------------------------------------------------------
135 // `Main program' equivalent: the program execution "starts" here
138 #if wxUSE_SYSTEM_OPTIONS
139 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
142 wxInitAllImageHandlers();
143 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
144 wxFileSystem::AddHandler(new wxInternetFSHandler
);
147 SetVendorName(wxT("wxWidgets"));
148 SetAppName(wxT("wxHtmlTest"));
149 // the following call to wxConfig::Get will use it to create an object...
151 // Create the main application window
152 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
153 wxDefaultPosition
, wxSize(640, 480));
157 return true /* continue running */;
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
165 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
166 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
167 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
170 wxMenu
*menuFile
= new wxMenu
;
171 wxMenu
*menuNav
= new wxMenu
;
173 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
174 menuFile
->AppendSeparator();
175 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
176 wxEmptyString
, wxITEM_CHECK
);
178 menuFile
->AppendSeparator();
179 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
180 menuNav
->Append(ID_Back
, _("Go &BACK"));
181 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
183 // now append the freshly created menu to the menu bar...
184 wxMenuBar
*menuBar
= new wxMenuBar
;
185 menuBar
->Append(menuFile
, _("&File"));
186 menuBar
->Append(menuNav
, _("&Navigate"));
188 // ... and attach this menu bar to the frame
192 // Create convenient accelerators for Back and Forward navigation
193 wxAcceleratorEntry entries
[2];
194 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
195 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
197 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
198 SetAcceleratorTable(accel
);
199 #endif // wxUSE_ACCEL
203 #endif // wxUSE_STATUSBAR
205 m_Processor
= new BoldProcessor
;
206 m_Processor
->Enable(false);
207 m_Html
= new MyHtmlWindow(this);
208 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
210 m_Html
->SetRelatedStatusBar(0);
211 #endif // wxUSE_STATUSBAR
212 m_Html
->ReadCustomization(wxConfig::Get());
213 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
214 m_Html
->AddProcessor(m_Processor
);
220 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
222 m_Html
->WriteCustomization(wxConfig::Get());
223 delete wxConfig::Set(NULL
);
225 // true is to force the frame to close
229 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
231 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
232 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm"));
234 if (p
!= wxEmptyString
)
238 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
240 if (!m_Html
->HistoryBack())
242 wxMessageBox(_("You reached prehistory era!"));
246 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
248 if (!m_Html
->HistoryForward())
250 wxMessageBox(_("No more items in history!"));
254 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
256 m_Processor
->Enable(!m_Processor
->IsEnabled());
257 m_Html
->LoadPage(m_Html
->GetOpenedPage());
260 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
262 wxString
*WXUNUSED(redirect
)) const
264 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);