]>
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" |
0f11c233 VZ |
31 | #include "wx/clipbrd.h" |
32 | #include "wx/dataobj.h" | |
5526e819 | 33 | |
18922659 JS |
34 | #include "../../sample.xpm" |
35 | ||
5526e819 VS |
36 | // ---------------------------------------------------------------------------- |
37 | // private classes | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // Define a new application type, each program should derive a class from wxApp | |
3e729cbe VS |
41 | class MyApp : public wxApp |
42 | { | |
43 | public: | |
3f6bd7c1 | 44 | virtual bool OnInit(); |
3e729cbe | 45 | }; |
5526e819 | 46 | |
4bfa3189 WS |
47 | // Define a new html window type: this is a wrapper for handling wxHtmlWindow events |
48 | class MyHtmlWindow : public wxHtmlWindow | |
49 | { | |
50 | public: | |
51 | MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { } | |
52 | ||
53 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
54 | const wxString& WXUNUSED(url), | |
55 | wxString *WXUNUSED(redirect)) const; | |
56 | ||
57 | private: | |
0f11c233 VZ |
58 | void OnClipboardEvent(wxClipboardTextEvent& event); |
59 | ||
60 | #if wxUSE_CLIPBOARD | |
61 | DECLARE_EVENT_TABLE() | |
62 | #endif // wxUSE_CLIPBOARD | |
4bfa3189 WS |
63 | DECLARE_NO_COPY_CLASS(MyHtmlWindow) |
64 | }; | |
65 | ||
5526e819 | 66 | // Define a new frame type: this is going to be our main frame |
3e729cbe VS |
67 | class MyFrame : public wxFrame |
68 | { | |
69 | public: | |
3f6bd7c1 | 70 | // ctor(s) |
3e729cbe VS |
71 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
72 | ||
3f6bd7c1 | 73 | // event handlers (these functions should _not_ be virtual) |
3e729cbe VS |
74 | void OnQuit(wxCommandEvent& event); |
75 | void OnPageOpen(wxCommandEvent& event); | |
17ede0b1 RR |
76 | void OnDefaultLocalBrowser(wxCommandEvent& event); |
77 | void OnDefaultWebBrowser(wxCommandEvent& event); | |
3e729cbe VS |
78 | void OnBack(wxCommandEvent& event); |
79 | void OnForward(wxCommandEvent& event); | |
80 | void OnProcessor(wxCommandEvent& event); | |
81 | ||
a1c3cdc4 VS |
82 | void OnHtmlLinkClicked(wxHtmlLinkEvent& event); |
83 | void OnHtmlCellHover(wxHtmlCellEvent &event); | |
84 | void OnHtmlCellClicked(wxHtmlCellEvent &event); | |
85 | ||
3e729cbe | 86 | private: |
4bfa3189 | 87 | MyHtmlWindow *m_Html; |
3f6bd7c1 DS |
88 | wxHtmlProcessor *m_Processor; |
89 | ||
be5a51fb | 90 | // Any class wishing to process wxWidgets events must use this macro |
3f6bd7c1 | 91 | DECLARE_EVENT_TABLE() |
3e729cbe VS |
92 | }; |
93 | ||
94 | ||
95 | class BoldProcessor : public wxHtmlProcessor | |
96 | { | |
3f6bd7c1 DS |
97 | public: |
98 | virtual wxString Process(const wxString& s) const | |
99 | { | |
100 | wxString r(s); | |
101 | r.Replace(wxT("<b>"), wxEmptyString); | |
102 | r.Replace(wxT("<B>"), wxEmptyString); | |
103 | r.Replace(wxT("</b>"), wxEmptyString); | |
104 | r.Replace(wxT("</B>"), wxEmptyString); | |
105 | ||
106 | return r; | |
107 | } | |
3e729cbe | 108 | }; |
5526e819 VS |
109 | |
110 | // ---------------------------------------------------------------------------- | |
111 | // constants | |
112 | // ---------------------------------------------------------------------------- | |
113 | ||
114 | // IDs for the controls and the menu commands | |
3f6bd7c1 DS |
115 | enum |
116 | { | |
5526e819 | 117 | // menu items |
3f6bd7c1 | 118 | ID_PageOpen = wxID_HIGHEST, |
17ede0b1 RR |
119 | ID_DefaultLocalBrowser, |
120 | ID_DefaultWebBrowser, | |
3f6bd7c1 DS |
121 | ID_Back, |
122 | ID_Forward, | |
123 | ID_Processor | |
124 | }; | |
5526e819 VS |
125 | |
126 | // ---------------------------------------------------------------------------- | |
be5a51fb | 127 | // event tables and other macros for wxWidgets |
5526e819 VS |
128 | // ---------------------------------------------------------------------------- |
129 | ||
3f6bd7c1 DS |
130 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
131 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
132 | EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen) | |
17ede0b1 RR |
133 | EVT_MENU(ID_DefaultLocalBrowser, MyFrame::OnDefaultLocalBrowser) |
134 | EVT_MENU(ID_DefaultWebBrowser, MyFrame::OnDefaultWebBrowser) | |
3f6bd7c1 DS |
135 | EVT_MENU(ID_Back, MyFrame::OnBack) |
136 | EVT_MENU(ID_Forward, MyFrame::OnForward) | |
137 | EVT_MENU(ID_Processor, MyFrame::OnProcessor) | |
a1c3cdc4 VS |
138 | |
139 | EVT_HTML_LINK_CLICKED(wxID_ANY, MyFrame::OnHtmlLinkClicked) | |
140 | EVT_HTML_CELL_HOVER(wxID_ANY, MyFrame::OnHtmlCellHover) | |
141 | EVT_HTML_CELL_CLICKED(wxID_ANY, MyFrame::OnHtmlCellClicked) | |
3f6bd7c1 DS |
142 | END_EVENT_TABLE() |
143 | ||
144 | IMPLEMENT_APP(MyApp) | |
145 | ||
146 | // ============================================================================ | |
147 | // implementation | |
148 | // ============================================================================ | |
149 | ||
150 | // ---------------------------------------------------------------------------- | |
151 | // the application class | |
152 | // ---------------------------------------------------------------------------- | |
153 | ||
154 | // `Main program' equivalent: the program execution "starts" here | |
155 | bool MyApp::OnInit() | |
156 | { | |
954cc8d2 | 157 | #if wxUSE_SYSTEM_OPTIONS |
3f6bd7c1 | 158 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 159 | #endif |
bbb8f29b | 160 | |
3f6bd7c1 | 161 | wxInitAllImageHandlers(); |
954cc8d2 | 162 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
3f6bd7c1 | 163 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 164 | #endif |
5612e524 | 165 | |
be5a51fb | 166 | SetVendorName(wxT("wxWidgets")); |
3f6bd7c1 DS |
167 | SetAppName(wxT("wxHtmlTest")); |
168 | // the following call to wxConfig::Get will use it to create an object... | |
5612e524 | 169 | |
5526e819 | 170 | // Create the main application window |
3f6bd7c1 DS |
171 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), |
172 | wxDefaultPosition, wxSize(640, 480)); | |
173 | ||
174 | frame->Show(); | |
175 | ||
176 | return true /* continue running */; | |
177 | } | |
5526e819 VS |
178 | |
179 | // ---------------------------------------------------------------------------- | |
180 | // main frame | |
181 | // ---------------------------------------------------------------------------- | |
182 | ||
5526e819 | 183 | // frame constructor |
3f6bd7c1 DS |
184 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
185 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size, | |
186 | wxDEFAULT_FRAME_STYLE, wxT("html_test_app")) | |
187 | { | |
5526e819 | 188 | // create a menu bar |
3f6bd7c1 DS |
189 | wxMenu *menuFile = new wxMenu; |
190 | wxMenu *menuNav = new wxMenu; | |
191 | ||
192 | menuFile->Append(ID_PageOpen, _("&Open HTML page...")); | |
17ede0b1 RR |
193 | menuFile->Append(ID_DefaultLocalBrowser, _("&Open current page with default browser")); |
194 | menuFile->Append(ID_DefaultWebBrowser, _("Open a &web page with default browser")); | |
3f6bd7c1 DS |
195 | menuFile->AppendSeparator(); |
196 | menuFile->Append(ID_Processor, _("&Remove bold attribute"), | |
197 | wxEmptyString, wxITEM_CHECK); | |
198 | ||
199 | menuFile->AppendSeparator(); | |
200 | menuFile->Append(wxID_EXIT, _("&Close frame")); | |
201 | menuNav->Append(ID_Back, _("Go &BACK")); | |
202 | menuNav->Append(ID_Forward, _("Go &FORWARD")); | |
5526e819 VS |
203 | |
204 | // now append the freshly created menu to the menu bar... | |
3f6bd7c1 DS |
205 | wxMenuBar *menuBar = new wxMenuBar; |
206 | menuBar->Append(menuFile, _("&File")); | |
207 | menuBar->Append(menuNav, _("&Navigate")); | |
5526e819 VS |
208 | |
209 | // ... and attach this menu bar to the frame | |
3f6bd7c1 DS |
210 | SetMenuBar(menuBar); |
211 | ||
18922659 | 212 | SetIcon(wxIcon(sample_xpm)); |
11fdee42 | 213 | |
3f6bd7c1 DS |
214 | #if wxUSE_ACCEL |
215 | // Create convenient accelerators for Back and Forward navigation | |
216 | wxAcceleratorEntry entries[2]; | |
217 | entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back); | |
218 | entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward); | |
219 | ||
220 | wxAcceleratorTable accel(WXSIZEOF(entries), entries); | |
221 | SetAcceleratorTable(accel); | |
222 | #endif // wxUSE_ACCEL | |
223 | ||
8520f137 | 224 | #if wxUSE_STATUSBAR |
4bfa3189 | 225 | CreateStatusBar(2); |
8520f137 | 226 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
227 | |
228 | m_Processor = new BoldProcessor; | |
229 | m_Processor->Enable(false); | |
4bfa3189 | 230 | m_Html = new MyHtmlWindow(this); |
3f6bd7c1 | 231 | m_Html->SetRelatedFrame(this, _("HTML : %s")); |
8520f137 | 232 | #if wxUSE_STATUSBAR |
3f6bd7c1 | 233 | m_Html->SetRelatedStatusBar(0); |
8520f137 | 234 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
235 | m_Html->ReadCustomization(wxConfig::Get()); |
236 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
237 | m_Html->AddProcessor(m_Processor); | |
a1c3cdc4 VS |
238 | |
239 | wxTextCtrl *text = new wxTextCtrl(this, wxID_ANY, _T(""), | |
240 | wxDefaultPosition, wxDefaultSize, | |
241 | wxTE_MULTILINE); | |
242 | ||
243 | delete wxLog::SetActiveTarget(new wxLogTextCtrl(text)); | |
244 | ||
245 | wxSizer *sz = new wxBoxSizer(wxVERTICAL); | |
246 | sz->Add(m_Html, 3, wxGROW); | |
247 | sz->Add(text, 1, wxGROW); | |
248 | SetSizer(sz); | |
3f6bd7c1 | 249 | } |
5526e819 VS |
250 | |
251 | ||
252 | // event handlers | |
253 | ||
3e729cbe VS |
254 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
255 | { | |
3f6bd7c1 DS |
256 | m_Html->WriteCustomization(wxConfig::Get()); |
257 | delete wxConfig::Set(NULL); | |
258 | ||
259 | // true is to force the frame to close | |
260 | Close(true); | |
3e729cbe VS |
261 | } |
262 | ||
263 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
264 | { | |
71307412 | 265 | #if wxUSE_FILEDLG |
3f6bd7c1 | 266 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, |
43056e6e | 267 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm;*.html")); |
3f6bd7c1 | 268 | |
71307412 | 269 | if (!p.empty()) |
9dbadebe | 270 | m_Html->LoadFile(wxFileName(p)); |
71307412 | 271 | #endif // wxUSE_FILEDLG |
3e729cbe VS |
272 | } |
273 | ||
17ede0b1 | 274 | void MyFrame::OnDefaultLocalBrowser(wxCommandEvent& WXUNUSED(event)) |
d83c04e6 WS |
275 | { |
276 | wxString page = m_Html->GetOpenedPage(); | |
277 | if (!page.empty()) | |
278 | { | |
279 | wxLaunchDefaultBrowser(page); | |
280 | } | |
281 | } | |
282 | ||
17ede0b1 RR |
283 | void MyFrame::OnDefaultWebBrowser(wxCommandEvent& WXUNUSED(event)) |
284 | { | |
285 | wxString page = m_Html->GetOpenedPage(); | |
286 | if (!page.empty()) | |
287 | { | |
288 | wxLaunchDefaultBrowser(wxT("http://www.google.com")); | |
289 | } | |
290 | } | |
291 | ||
3e729cbe VS |
292 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) |
293 | { | |
3f6bd7c1 DS |
294 | if (!m_Html->HistoryBack()) |
295 | { | |
296 | wxMessageBox(_("You reached prehistory era!")); | |
297 | } | |
3e729cbe VS |
298 | } |
299 | ||
300 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
301 | { | |
3f6bd7c1 DS |
302 | if (!m_Html->HistoryForward()) |
303 | { | |
304 | wxMessageBox(_("No more items in history!")); | |
305 | } | |
3e729cbe VS |
306 | } |
307 | ||
308 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
309 | { | |
310 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
311 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 312 | } |
4bfa3189 | 313 | |
a1c3cdc4 VS |
314 | void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent &event) |
315 | { | |
316 | wxLogMessage(wxT("The url '%s' has been clicked!"), event.GetLinkInfo().GetHref().c_str()); | |
317 | ||
318 | // skipping this event the default behaviour (load the clicked URL) | |
319 | // will happen... | |
320 | event.Skip(); | |
321 | } | |
322 | ||
323 | void MyFrame::OnHtmlCellHover(wxHtmlCellEvent &event) | |
324 | { | |
325 | wxLogMessage(wxT("Mouse moved over cell %d at %d;%d"), | |
326 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); | |
327 | } | |
328 | ||
329 | void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent &event) | |
330 | { | |
331 | wxLogMessage(wxT("Click over cell %d at %d;%d"), | |
332 | event.GetCell(), event.GetPoint().x, event.GetPoint().y); | |
333 | ||
334 | // if we don't skip the event, OnHtmlLinkClicked won't be called! | |
335 | event.Skip(); | |
336 | } | |
337 | ||
4bfa3189 WS |
338 | wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type), |
339 | const wxString& url, | |
340 | wxString *WXUNUSED(redirect)) const | |
341 | { | |
342 | GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1); | |
343 | return wxHTML_OPEN; | |
344 | } | |
0f11c233 VZ |
345 | |
346 | #if wxUSE_CLIPBOARD | |
347 | BEGIN_EVENT_TABLE(MyHtmlWindow, wxHtmlWindow) | |
348 | EVT_TEXT_COPY(wxID_ANY, MyHtmlWindow::OnClipboardEvent) | |
349 | END_EVENT_TABLE() | |
350 | ||
351 | void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent& WXUNUSED(event)) | |
352 | { | |
353 | // explicitly call wxHtmlWindow::CopySelection() method | |
354 | // and show the first 100 characters of the text copied in the status bar | |
355 | if ( CopySelection() ) | |
356 | { | |
357 | wxTextDataObject data; | |
358 | if ( wxTheClipboard && wxTheClipboard->GetData(data) ) | |
359 | { | |
360 | const wxString text = data.GetText(); | |
361 | const size_t maxTextLength = 100; | |
362 | ||
363 | wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"), | |
364 | wxString(text, maxTextLength).c_str(), | |
365 | (text.length() > maxTextLength) ? _T("...") | |
366 | : _T(""))); | |
367 | return; | |
368 | } | |
369 | } | |
370 | ||
371 | wxLogStatus(_T("Clipboard: nothing")); | |
372 | } | |
373 | #endif // wxUSE_CLIPBOARD |