]>
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 | #if defined(__GNUG__) && !defined(__APPLE__) | |
12 | #pragma implementation | |
13 | #pragma interface | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | // For all others, include the necessary headers (this file is usually all you | |
24 | // need because it includes almost all "standard" wxWidgets headers | |
25 | #ifndef WX_PRECOMP | |
26 | #include "wx/wx.h" | |
27 | #endif | |
28 | ||
29 | #include "wx/image.h" | |
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" | |
35 | ||
36 | #include "../../sample.xpm" | |
37 | ||
38 | // ---------------------------------------------------------------------------- | |
39 | // private classes | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // Define a new application type, each program should derive a class from wxApp | |
43 | class MyApp : public wxApp | |
44 | { | |
45 | public: | |
46 | virtual bool OnInit(); | |
47 | }; | |
48 | ||
49 | // Define a new html window type: this is a wrapper for handling wxHtmlWindow events | |
50 | class MyHtmlWindow : public wxHtmlWindow | |
51 | { | |
52 | public: | |
53 | MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { } | |
54 | ||
55 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
56 | const wxString& WXUNUSED(url), | |
57 | wxString *WXUNUSED(redirect)) const; | |
58 | ||
59 | private: | |
60 | DECLARE_NO_COPY_CLASS(MyHtmlWindow) | |
61 | }; | |
62 | ||
63 | // Define a new frame type: this is going to be our main frame | |
64 | class MyFrame : public wxFrame | |
65 | { | |
66 | public: | |
67 | // ctor(s) | |
68 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
69 | ||
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); | |
76 | ||
77 | private: | |
78 | MyHtmlWindow *m_Html; | |
79 | wxHtmlProcessor *m_Processor; | |
80 | ||
81 | // Any class wishing to process wxWidgets events must use this macro | |
82 | DECLARE_EVENT_TABLE() | |
83 | }; | |
84 | ||
85 | ||
86 | class BoldProcessor : public wxHtmlProcessor | |
87 | { | |
88 | public: | |
89 | virtual wxString Process(const wxString& s) const | |
90 | { | |
91 | wxString r(s); | |
92 | r.Replace(wxT("<b>"), wxEmptyString); | |
93 | r.Replace(wxT("<B>"), wxEmptyString); | |
94 | r.Replace(wxT("</b>"), wxEmptyString); | |
95 | r.Replace(wxT("</B>"), wxEmptyString); | |
96 | ||
97 | return r; | |
98 | } | |
99 | }; | |
100 | ||
101 | // ---------------------------------------------------------------------------- | |
102 | // constants | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | // IDs for the controls and the menu commands | |
106 | enum | |
107 | { | |
108 | // menu items | |
109 | ID_PageOpen = wxID_HIGHEST, | |
110 | ID_Back, | |
111 | ID_Forward, | |
112 | ID_Processor | |
113 | }; | |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
116 | // event tables and other macros for wxWidgets | |
117 | // ---------------------------------------------------------------------------- | |
118 | ||
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) | |
125 | END_EVENT_TABLE() | |
126 | ||
127 | IMPLEMENT_APP(MyApp) | |
128 | ||
129 | // ============================================================================ | |
130 | // implementation | |
131 | // ============================================================================ | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // the application class | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | // `Main program' equivalent: the program execution "starts" here | |
138 | bool MyApp::OnInit() | |
139 | { | |
140 | #if wxUSE_SYSTEM_OPTIONS | |
141 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); | |
142 | #endif | |
143 | ||
144 | wxInitAllImageHandlers(); | |
145 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS | |
146 | wxFileSystem::AddHandler(new wxInternetFSHandler); | |
147 | #endif | |
148 | ||
149 | SetVendorName(wxT("wxWidgets")); | |
150 | SetAppName(wxT("wxHtmlTest")); | |
151 | // the following call to wxConfig::Get will use it to create an object... | |
152 | ||
153 | // Create the main application window | |
154 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), | |
155 | wxDefaultPosition, wxSize(640, 480)); | |
156 | ||
157 | frame->Show(); | |
158 | ||
159 | return true /* continue running */; | |
160 | } | |
161 | ||
162 | // ---------------------------------------------------------------------------- | |
163 | // main frame | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
166 | // frame constructor | |
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")) | |
170 | { | |
171 | // create a menu bar | |
172 | wxMenu *menuFile = new wxMenu; | |
173 | wxMenu *menuNav = new wxMenu; | |
174 | ||
175 | menuFile->Append(ID_PageOpen, _("&Open HTML page...")); | |
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 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, | |
236 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm")); | |
237 | ||
238 | if (p != wxEmptyString) | |
239 | m_Html->LoadPage(p); | |
240 | } | |
241 | ||
242 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
243 | { | |
244 | if (!m_Html->HistoryBack()) | |
245 | { | |
246 | wxMessageBox(_("You reached prehistory era!")); | |
247 | } | |
248 | } | |
249 | ||
250 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
251 | { | |
252 | if (!m_Html->HistoryForward()) | |
253 | { | |
254 | wxMessageBox(_("No more items in history!")); | |
255 | } | |
256 | } | |
257 | ||
258 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
259 | { | |
260 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
261 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
262 | } | |
263 | ||
264 | wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
265 | const wxString& url, | |
266 | wxString *WXUNUSED(redirect)) const | |
267 | { | |
268 | GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1); | |
269 | return wxHTML_OPEN; | |
270 | } |