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 frame type: this is going to be our main frame
48 class MyFrame
: public wxFrame
52 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
54 // event handlers (these functions should _not_ be virtual)
55 void OnQuit(wxCommandEvent
& event
);
56 void OnPageOpen(wxCommandEvent
& event
);
57 void OnBack(wxCommandEvent
& event
);
58 void OnForward(wxCommandEvent
& event
);
59 void OnProcessor(wxCommandEvent
& event
);
63 wxHtmlProcessor
*m_Processor
;
65 // Any class wishing to process wxWidgets events must use this macro
70 class BoldProcessor
: public wxHtmlProcessor
73 virtual wxString
Process(const wxString
& s
) const
76 r
.Replace(wxT("<b>"), wxEmptyString
);
77 r
.Replace(wxT("<B>"), wxEmptyString
);
78 r
.Replace(wxT("</b>"), wxEmptyString
);
79 r
.Replace(wxT("</B>"), wxEmptyString
);
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 // IDs for the controls and the menu commands
93 ID_PageOpen
= wxID_HIGHEST
,
99 // ----------------------------------------------------------------------------
100 // event tables and other macros for wxWidgets
101 // ----------------------------------------------------------------------------
103 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
104 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
105 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
106 EVT_MENU(ID_Back
, MyFrame::OnBack
)
107 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
108 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
113 // ============================================================================
115 // ============================================================================
117 // ----------------------------------------------------------------------------
118 // the application class
119 // ----------------------------------------------------------------------------
121 // `Main program' equivalent: the program execution "starts" here
124 #if wxUSE_SYSTEM_OPTIONS
125 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
128 wxInitAllImageHandlers();
129 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
130 wxFileSystem::AddHandler(new wxInternetFSHandler
);
133 SetVendorName(wxT("wxWidgets"));
134 SetAppName(wxT("wxHtmlTest"));
135 // the following call to wxConfig::Get will use it to create an object...
137 // Create the main application window
138 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
139 wxDefaultPosition
, wxSize(640, 480));
143 return true /* continue running */;
146 // ----------------------------------------------------------------------------
148 // ----------------------------------------------------------------------------
151 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
152 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
153 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
156 wxMenu
*menuFile
= new wxMenu
;
157 wxMenu
*menuNav
= new wxMenu
;
159 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
160 menuFile
->AppendSeparator();
161 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
162 wxEmptyString
, wxITEM_CHECK
);
164 menuFile
->AppendSeparator();
165 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
166 menuNav
->Append(ID_Back
, _("Go &BACK"));
167 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
169 // now append the freshly created menu to the menu bar...
170 wxMenuBar
*menuBar
= new wxMenuBar
;
171 menuBar
->Append(menuFile
, _("&File"));
172 menuBar
->Append(menuNav
, _("&Navigate"));
174 // ... and attach this menu bar to the frame
178 // Create convenient accelerators for Back and Forward navigation
179 wxAcceleratorEntry entries
[2];
180 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
181 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
183 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
184 SetAcceleratorTable(accel
);
185 #endif // wxUSE_ACCEL
189 #endif // wxUSE_STATUSBAR
191 m_Processor
= new BoldProcessor
;
192 m_Processor
->Enable(false);
193 m_Html
= new wxHtmlWindow(this);
194 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
196 m_Html
->SetRelatedStatusBar(0);
197 #endif // wxUSE_STATUSBAR
198 m_Html
->ReadCustomization(wxConfig::Get());
199 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
200 m_Html
->AddProcessor(m_Processor
);
206 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
208 m_Html
->WriteCustomization(wxConfig::Get());
209 delete wxConfig::Set(NULL
);
211 // true is to force the frame to close
215 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
217 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
218 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm"));
220 if (p
!= wxEmptyString
)
224 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
226 if (!m_Html
->HistoryBack())
228 wxMessageBox(_("You reached prehistory era!"));
232 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
234 if (!m_Html
->HistoryForward())
236 wxMessageBox(_("No more items in history!"));
240 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
242 m_Processor
->Enable(!m_Processor
->IsEnabled());
243 m_Html
->LoadPage(m_Html
->GetOpenedPage());