]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: test.cpp | |
3 | // Purpose: wxHtml testing example | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 1999-07-07 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | // For all others, include the necessary headers (this file is usually all you | |
19 | // need because it includes almost all "standard" wxWidgets headers | |
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/image.h" | |
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" | |
30 | #include "wx/utils.h" | |
31 | ||
32 | #include "../../sample.xpm" | |
33 | ||
34 | // ---------------------------------------------------------------------------- | |
35 | // private classes | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | // Define a new application type, each program should derive a class from wxApp | |
39 | class MyApp : public wxApp | |
40 | { | |
41 | public: | |
42 | virtual bool OnInit(); | |
43 | }; | |
44 | ||
45 | // Define a new html window type: this is a wrapper for handling wxHtmlWindow events | |
46 | class MyHtmlWindow : public wxHtmlWindow | |
47 | { | |
48 | public: | |
49 | MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { } | |
50 | ||
51 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
52 | const wxString& WXUNUSED(url), | |
53 | wxString *WXUNUSED(redirect)) const; | |
54 | ||
55 | private: | |
56 | DECLARE_NO_COPY_CLASS(MyHtmlWindow) | |
57 | }; | |
58 | ||
59 | // Define a new frame type: this is going to be our main frame | |
60 | class MyFrame : public wxFrame | |
61 | { | |
62 | public: | |
63 | // ctor(s) | |
64 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
65 | ||
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); | |
73 | ||
74 | private: | |
75 | MyHtmlWindow *m_Html; | |
76 | wxHtmlProcessor *m_Processor; | |
77 | ||
78 | // Any class wishing to process wxWidgets events must use this macro | |
79 | DECLARE_EVENT_TABLE() | |
80 | }; | |
81 | ||
82 | ||
83 | class BoldProcessor : public wxHtmlProcessor | |
84 | { | |
85 | public: | |
86 | virtual wxString Process(const wxString& s) const | |
87 | { | |
88 | wxString r(s); | |
89 | r.Replace(wxT("<b>"), wxEmptyString); | |
90 | r.Replace(wxT("<B>"), wxEmptyString); | |
91 | r.Replace(wxT("</b>"), wxEmptyString); | |
92 | r.Replace(wxT("</B>"), wxEmptyString); | |
93 | ||
94 | return r; | |
95 | } | |
96 | }; | |
97 | ||
98 | // ---------------------------------------------------------------------------- | |
99 | // constants | |
100 | // ---------------------------------------------------------------------------- | |
101 | ||
102 | // IDs for the controls and the menu commands | |
103 | enum | |
104 | { | |
105 | // menu items | |
106 | ID_PageOpen = wxID_HIGHEST, | |
107 | ID_DefaultBrowser, | |
108 | ID_Back, | |
109 | ID_Forward, | |
110 | ID_Processor | |
111 | }; | |
112 | ||
113 | // ---------------------------------------------------------------------------- | |
114 | // event tables and other macros for wxWidgets | |
115 | // ---------------------------------------------------------------------------- | |
116 | ||
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) | |
124 | END_EVENT_TABLE() | |
125 | ||
126 | IMPLEMENT_APP(MyApp) | |
127 | ||
128 | // ============================================================================ | |
129 | // implementation | |
130 | // ============================================================================ | |
131 | ||
132 | // ---------------------------------------------------------------------------- | |
133 | // the application class | |
134 | // ---------------------------------------------------------------------------- | |
135 | ||
136 | // `Main program' equivalent: the program execution "starts" here | |
137 | bool MyApp::OnInit() | |
138 | { | |
139 | #if wxUSE_SYSTEM_OPTIONS | |
140 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); | |
141 | #endif | |
142 | ||
143 | wxInitAllImageHandlers(); | |
144 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS | |
145 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
146 | #endif | |
147 | ||
148 | SetVendorName(wxT("wxWidgets")); | |
149 | SetAppName(wxT("wxHtmlTest")); | |
150 | // the following call to wxConfig::Get will use it to create an object... | |
151 | ||
152 | // Create the main application window | |
153 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), | |
154 | wxDefaultPosition, wxSize(640, 480)); | |
155 | ||
156 | frame->Show(); | |
157 | ||
158 | return true /* continue running */; | |
159 | } | |
160 | ||
161 | // ---------------------------------------------------------------------------- | |
162 | // main frame | |
163 | // ---------------------------------------------------------------------------- | |
164 | ||
165 | // frame constructor | |
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")) | |
169 | { | |
170 | // create a menu bar | |
171 | wxMenu *menuFile = new wxMenu; | |
172 | wxMenu *menuNav = new wxMenu; | |
173 | ||
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); | |
179 | ||
180 | menuFile->AppendSeparator(); | |
181 | menuFile->Append(wxID_EXIT, _("&Close frame")); | |
182 | menuNav->Append(ID_Back, _("Go &BACK")); | |
183 | menuNav->Append(ID_Forward, _("Go &FORWARD")); | |
184 | ||
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")); | |
189 | ||
190 | // ... and attach this menu bar to the frame | |
191 | SetMenuBar(menuBar); | |
192 | ||
193 | SetIcon(wxIcon(sample_xpm)); | |
194 | ||
195 | #if wxUSE_ACCEL | |
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); | |
200 | ||
201 | wxAcceleratorTable accel(WXSIZEOF(entries), entries); | |
202 | SetAcceleratorTable(accel); | |
203 | #endif // wxUSE_ACCEL | |
204 | ||
205 | #if wxUSE_STATUSBAR | |
206 | CreateStatusBar(2); | |
207 | #endif // wxUSE_STATUSBAR | |
208 | ||
209 | m_Processor = new BoldProcessor; | |
210 | m_Processor->Enable(false); | |
211 | m_Html = new MyHtmlWindow(this); | |
212 | m_Html->SetRelatedFrame(this, _("HTML : %s")); | |
213 | #if wxUSE_STATUSBAR | |
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); | |
219 | } | |
220 | ||
221 | ||
222 | // event handlers | |
223 | ||
224 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
225 | { | |
226 | m_Html->WriteCustomization(wxConfig::Get()); | |
227 | delete wxConfig::Set(NULL); | |
228 | ||
229 | // true is to force the frame to close | |
230 | Close(true); | |
231 | } | |
232 | ||
233 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
234 | { | |
235 | #if wxUSE_FILEDLG | |
236 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, | |
237 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html")); | |
238 | ||
239 | if (!p.empty()) | |
240 | m_Html->LoadFile(wxFileName(p)); | |
241 | #endif // wxUSE_FILEDLG | |
242 | } | |
243 | ||
244 | void MyFrame::OnDefaultBrowser(wxCommandEvent& WXUNUSED(event)) | |
245 | { | |
246 | wxString page = m_Html->GetOpenedPage(); | |
247 | if (!page.empty()) | |
248 | { | |
249 | wxLaunchDefaultBrowser(page); | |
250 | } | |
251 | } | |
252 | ||
253 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
254 | { | |
255 | if (!m_Html->HistoryBack()) | |
256 | { | |
257 | wxMessageBox(_("You reached prehistory era!")); | |
258 | } | |
259 | } | |
260 | ||
261 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
262 | { | |
263 | if (!m_Html->HistoryForward()) | |
264 | { | |
265 | wxMessageBox(_("No more items in history!")); | |
266 | } | |
267 | } | |
268 | ||
269 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
270 | { | |
271 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
272 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
273 | } | |
274 | ||
275 | wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
276 | const wxString& url, | |
277 | wxString *WXUNUSED(redirect)) const | |
278 | { | |
279 | GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1); | |
280 | return wxHTML_OPEN; | |
281 | } |