]>
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" | |
5526e819 | 35 | |
18922659 JS |
36 | #include "../../sample.xpm" |
37 | ||
5526e819 VS |
38 | // ---------------------------------------------------------------------------- |
39 | // private classes | |
40 | // ---------------------------------------------------------------------------- | |
41 | ||
42 | // Define a new application type, each program should derive a class from wxApp | |
3e729cbe VS |
43 | class MyApp : public wxApp |
44 | { | |
45 | public: | |
3f6bd7c1 | 46 | virtual bool OnInit(); |
3e729cbe | 47 | }; |
5526e819 | 48 | |
4bfa3189 WS |
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 | ||
5526e819 | 63 | // Define a new frame type: this is going to be our main frame |
3e729cbe VS |
64 | class MyFrame : public wxFrame |
65 | { | |
66 | public: | |
3f6bd7c1 | 67 | // ctor(s) |
3e729cbe VS |
68 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
69 | ||
3f6bd7c1 | 70 | // event handlers (these functions should _not_ be virtual) |
3e729cbe VS |
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: | |
4bfa3189 | 78 | MyHtmlWindow *m_Html; |
3f6bd7c1 DS |
79 | wxHtmlProcessor *m_Processor; |
80 | ||
be5a51fb | 81 | // Any class wishing to process wxWidgets events must use this macro |
3f6bd7c1 | 82 | DECLARE_EVENT_TABLE() |
3e729cbe VS |
83 | }; |
84 | ||
85 | ||
86 | class BoldProcessor : public wxHtmlProcessor | |
87 | { | |
3f6bd7c1 DS |
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 | } | |
3e729cbe | 99 | }; |
5526e819 VS |
100 | |
101 | // ---------------------------------------------------------------------------- | |
102 | // constants | |
103 | // ---------------------------------------------------------------------------- | |
104 | ||
105 | // IDs for the controls and the menu commands | |
3f6bd7c1 DS |
106 | enum |
107 | { | |
5526e819 | 108 | // menu items |
3f6bd7c1 DS |
109 | ID_PageOpen = wxID_HIGHEST, |
110 | ID_Back, | |
111 | ID_Forward, | |
112 | ID_Processor | |
113 | }; | |
5526e819 VS |
114 | |
115 | // ---------------------------------------------------------------------------- | |
be5a51fb | 116 | // event tables and other macros for wxWidgets |
5526e819 VS |
117 | // ---------------------------------------------------------------------------- |
118 | ||
3f6bd7c1 DS |
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 | { | |
954cc8d2 | 140 | #if wxUSE_SYSTEM_OPTIONS |
3f6bd7c1 | 141 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 142 | #endif |
bbb8f29b | 143 | |
3f6bd7c1 | 144 | wxInitAllImageHandlers(); |
954cc8d2 | 145 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
3f6bd7c1 | 146 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 147 | #endif |
5612e524 | 148 | |
be5a51fb | 149 | SetVendorName(wxT("wxWidgets")); |
3f6bd7c1 DS |
150 | SetAppName(wxT("wxHtmlTest")); |
151 | // the following call to wxConfig::Get will use it to create an object... | |
5612e524 | 152 | |
5526e819 | 153 | // Create the main application window |
3f6bd7c1 DS |
154 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), |
155 | wxDefaultPosition, wxSize(640, 480)); | |
156 | ||
157 | frame->Show(); | |
158 | ||
159 | return true /* continue running */; | |
160 | } | |
5526e819 VS |
161 | |
162 | // ---------------------------------------------------------------------------- | |
163 | // main frame | |
164 | // ---------------------------------------------------------------------------- | |
165 | ||
5526e819 | 166 | // frame constructor |
3f6bd7c1 DS |
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 | { | |
5526e819 | 171 | // create a menu bar |
3f6bd7c1 DS |
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")); | |
5526e819 VS |
184 | |
185 | // now append the freshly created menu to the menu bar... | |
3f6bd7c1 DS |
186 | wxMenuBar *menuBar = new wxMenuBar; |
187 | menuBar->Append(menuFile, _("&File")); | |
188 | menuBar->Append(menuNav, _("&Navigate")); | |
5526e819 VS |
189 | |
190 | // ... and attach this menu bar to the frame | |
3f6bd7c1 DS |
191 | SetMenuBar(menuBar); |
192 | ||
18922659 | 193 | SetIcon(wxIcon(sample_xpm)); |
11fdee42 | 194 | |
3f6bd7c1 DS |
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 | ||
8520f137 | 205 | #if wxUSE_STATUSBAR |
4bfa3189 | 206 | CreateStatusBar(2); |
8520f137 | 207 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
208 | |
209 | m_Processor = new BoldProcessor; | |
210 | m_Processor->Enable(false); | |
4bfa3189 | 211 | m_Html = new MyHtmlWindow(this); |
3f6bd7c1 | 212 | m_Html->SetRelatedFrame(this, _("HTML : %s")); |
8520f137 | 213 | #if wxUSE_STATUSBAR |
3f6bd7c1 | 214 | m_Html->SetRelatedStatusBar(0); |
8520f137 | 215 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
216 | m_Html->ReadCustomization(wxConfig::Get()); |
217 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
218 | m_Html->AddProcessor(m_Processor); | |
219 | } | |
5526e819 VS |
220 | |
221 | ||
222 | // event handlers | |
223 | ||
3e729cbe VS |
224 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
225 | { | |
3f6bd7c1 DS |
226 | m_Html->WriteCustomization(wxConfig::Get()); |
227 | delete wxConfig::Set(NULL); | |
228 | ||
229 | // true is to force the frame to close | |
230 | Close(true); | |
3e729cbe VS |
231 | } |
232 | ||
233 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
234 | { | |
3f6bd7c1 DS |
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); | |
3e729cbe VS |
240 | } |
241 | ||
242 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
243 | { | |
3f6bd7c1 DS |
244 | if (!m_Html->HistoryBack()) |
245 | { | |
246 | wxMessageBox(_("You reached prehistory era!")); | |
247 | } | |
3e729cbe VS |
248 | } |
249 | ||
250 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
251 | { | |
3f6bd7c1 DS |
252 | if (!m_Html->HistoryForward()) |
253 | { | |
254 | wxMessageBox(_("No more items in history!")); | |
255 | } | |
3e729cbe VS |
256 | } |
257 | ||
258 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
259 | { | |
260 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
261 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 262 | } |
4bfa3189 WS |
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 | } |