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