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