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