1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 // Author: Vaclav Slavik
7 // Copyright: (c) Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // For all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers
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"
32 #include "../../sample.xpm"
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 // Define a new application type, each program should derive a class from wxApp
39 class MyApp
: public wxApp
42 virtual bool OnInit();
45 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
46 class MyHtmlWindow
: public wxHtmlWindow
49 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
51 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
52 const wxString
& WXUNUSED(url
),
53 wxString
*WXUNUSED(redirect
)) const;
56 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
59 // Define a new frame type: this is going to be our main frame
60 class MyFrame
: public wxFrame
64 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
75 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
76 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
77 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
81 wxHtmlProcessor
*m_Processor
;
83 // Any class wishing to process wxWidgets events must use this macro
88 class BoldProcessor
: public wxHtmlProcessor
91 virtual wxString
Process(const wxString
& s
) const
94 r
.Replace(wxT("<b>"), wxEmptyString
);
95 r
.Replace(wxT("<B>"), wxEmptyString
);
96 r
.Replace(wxT("</b>"), wxEmptyString
);
97 r
.Replace(wxT("</B>"), wxEmptyString
);
103 // ----------------------------------------------------------------------------
105 // ----------------------------------------------------------------------------
107 // IDs for the controls and the menu commands
111 ID_PageOpen
= wxID_HIGHEST
,
112 ID_DefaultLocalBrowser
,
113 ID_DefaultWebBrowser
,
119 // ----------------------------------------------------------------------------
120 // event tables and other macros for wxWidgets
121 // ----------------------------------------------------------------------------
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
)
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
)
139 // ============================================================================
141 // ============================================================================
143 // ----------------------------------------------------------------------------
144 // the application class
145 // ----------------------------------------------------------------------------
147 // `Main program' equivalent: the program execution "starts" here
150 #if wxUSE_SYSTEM_OPTIONS
151 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
154 wxInitAllImageHandlers();
155 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
156 wxFileSystem::AddHandler(new wxInternetFSHandler
);
159 SetVendorName(wxT("wxWidgets"));
160 SetAppName(wxT("wxHtmlTest"));
161 // the following call to wxConfig::Get will use it to create an object...
163 // Create the main application window
164 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
165 wxDefaultPosition
, wxSize(640, 480));
169 return true /* continue running */;
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
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"))
182 wxMenu
*menuFile
= new wxMenu
;
183 wxMenu
*menuNav
= new wxMenu
;
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
);
192 menuFile
->AppendSeparator();
193 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
194 menuNav
->Append(ID_Back
, _("Go &BACK"));
195 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
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"));
202 // ... and attach this menu bar to the frame
205 SetIcon(wxIcon(sample_xpm
));
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
);
213 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
214 SetAcceleratorTable(accel
);
215 #endif // wxUSE_ACCEL
219 #endif // wxUSE_STATUSBAR
221 m_Processor
= new BoldProcessor
;
222 m_Processor
->Enable(false);
223 m_Html
= new MyHtmlWindow(this);
224 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
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
);
232 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
233 wxDefaultPosition
, wxDefaultSize
,
236 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
238 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
239 sz
->Add(m_Html
, 3, wxGROW
);
240 sz
->Add(text
, 1, wxGROW
);
247 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
249 m_Html
->WriteCustomization(wxConfig::Get());
250 delete wxConfig::Set(NULL
);
252 // true is to force the frame to close
256 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
259 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
260 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
263 m_Html
->LoadFile(wxFileName(p
));
264 #endif // wxUSE_FILEDLG
267 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
269 wxString page
= m_Html
->GetOpenedPage();
272 wxLaunchDefaultBrowser(page
);
276 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
278 wxString page
= m_Html
->GetOpenedPage();
281 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
285 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
287 if (!m_Html
->HistoryBack())
289 wxMessageBox(_("You reached prehistory era!"));
293 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
295 if (!m_Html
->HistoryForward())
297 wxMessageBox(_("No more items in history!"));
301 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
303 m_Processor
->Enable(!m_Processor
->IsEnabled());
304 m_Html
->LoadPage(m_Html
->GetOpenedPage());
307 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
309 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
311 // skipping this event the default behaviour (load the clicked URL)
316 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
318 wxLogMessage(wxT("Mouse moved over cell %d at %d;%d"),
319 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
322 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
324 wxLogMessage(wxT("Click over cell %d at %d;%d"),
325 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
327 // if we don't skip the event, OnHtmlLinkClicked won't be called!
331 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
333 wxString
*WXUNUSED(redirect
)) const
335 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);