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"
31 #include "wx/clipbrd.h"
32 #include "wx/dataobj.h"
34 #include "../../sample.xpm"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 // Define a new application type, each program should derive a class from wxApp
41 class MyApp
: public wxApp
44 virtual bool OnInit();
47 // Define a new html window type: this is a wrapper for handling wxHtmlWindow events
48 class MyHtmlWindow
: public wxHtmlWindow
51 MyHtmlWindow(wxWindow
*parent
) : wxHtmlWindow( parent
) { }
53 virtual wxHtmlOpeningStatus
OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
54 const wxString
& WXUNUSED(url
),
55 wxString
*WXUNUSED(redirect
)) const;
58 void OnClipboardEvent(wxClipboardTextEvent
& event
);
62 #endif // wxUSE_CLIPBOARD
63 DECLARE_NO_COPY_CLASS(MyHtmlWindow
)
66 // Define a new frame type: this is going to be our main frame
67 class MyFrame
: public wxFrame
71 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
73 // event handlers (these functions should _not_ be virtual)
74 void OnQuit(wxCommandEvent
& event
);
75 void OnPageOpen(wxCommandEvent
& event
);
76 void OnDefaultLocalBrowser(wxCommandEvent
& event
);
77 void OnDefaultWebBrowser(wxCommandEvent
& event
);
78 void OnBack(wxCommandEvent
& event
);
79 void OnForward(wxCommandEvent
& event
);
80 void OnProcessor(wxCommandEvent
& event
);
82 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
83 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
84 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
88 wxHtmlProcessor
*m_Processor
;
90 // Any class wishing to process wxWidgets events must use this macro
95 class BoldProcessor
: public wxHtmlProcessor
98 virtual wxString
Process(const wxString
& s
) const
101 r
.Replace(wxT("<b>"), wxEmptyString
);
102 r
.Replace(wxT("<B>"), wxEmptyString
);
103 r
.Replace(wxT("</b>"), wxEmptyString
);
104 r
.Replace(wxT("</B>"), wxEmptyString
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 // IDs for the controls and the menu commands
118 ID_PageOpen
= wxID_HIGHEST
,
119 ID_DefaultLocalBrowser
,
120 ID_DefaultWebBrowser
,
126 // ----------------------------------------------------------------------------
127 // event tables and other macros for wxWidgets
128 // ----------------------------------------------------------------------------
130 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
131 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
132 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
133 EVT_MENU(ID_DefaultLocalBrowser
, MyFrame::OnDefaultLocalBrowser
)
134 EVT_MENU(ID_DefaultWebBrowser
, MyFrame::OnDefaultWebBrowser
)
135 EVT_MENU(ID_Back
, MyFrame::OnBack
)
136 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
137 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
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
)
146 // ============================================================================
148 // ============================================================================
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
154 // `Main program' equivalent: the program execution "starts" here
157 #if wxUSE_SYSTEM_OPTIONS
158 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
161 wxInitAllImageHandlers();
162 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
163 wxFileSystem::AddHandler(new wxInternetFSHandler
);
166 SetVendorName(wxT("wxWidgets"));
167 SetAppName(wxT("wxHtmlTest"));
168 // the following call to wxConfig::Get will use it to create an object...
170 // Create the main application window
171 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
172 wxDefaultPosition
, wxSize(640, 480));
176 return true /* continue running */;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
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"))
189 wxMenu
*menuFile
= new wxMenu
;
190 wxMenu
*menuNav
= new wxMenu
;
192 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
193 menuFile
->Append(ID_DefaultLocalBrowser
, _("&Open current page with default browser"));
194 menuFile
->Append(ID_DefaultWebBrowser
, _("Open a &web page with default browser"));
195 menuFile
->AppendSeparator();
196 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
197 wxEmptyString
, wxITEM_CHECK
);
199 menuFile
->AppendSeparator();
200 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
201 menuNav
->Append(ID_Back
, _("Go &BACK"));
202 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
204 // now append the freshly created menu to the menu bar...
205 wxMenuBar
*menuBar
= new wxMenuBar
;
206 menuBar
->Append(menuFile
, _("&File"));
207 menuBar
->Append(menuNav
, _("&Navigate"));
209 // ... and attach this menu bar to the frame
212 SetIcon(wxIcon(sample_xpm
));
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
);
220 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
221 SetAcceleratorTable(accel
);
222 #endif // wxUSE_ACCEL
226 #endif // wxUSE_STATUSBAR
228 m_Processor
= new BoldProcessor
;
229 m_Processor
->Enable(false);
230 m_Html
= new MyHtmlWindow(this);
231 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
233 m_Html
->SetRelatedStatusBar(0);
234 #endif // wxUSE_STATUSBAR
235 m_Html
->ReadCustomization(wxConfig::Get());
236 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
237 m_Html
->AddProcessor(m_Processor
);
239 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
240 wxDefaultPosition
, wxDefaultSize
,
243 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
245 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
246 sz
->Add(m_Html
, 3, wxGROW
);
247 sz
->Add(text
, 1, wxGROW
);
254 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
256 m_Html
->WriteCustomization(wxConfig::Get());
257 delete wxConfig::Set(NULL
);
259 // true is to force the frame to close
263 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
266 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
267 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
270 m_Html
->LoadFile(wxFileName(p
));
271 #endif // wxUSE_FILEDLG
274 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
276 wxString page
= m_Html
->GetOpenedPage();
279 wxLaunchDefaultBrowser(page
);
283 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
285 wxString page
= m_Html
->GetOpenedPage();
288 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
292 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
294 if (!m_Html
->HistoryBack())
296 wxMessageBox(_("You reached prehistory era!"));
300 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
302 if (!m_Html
->HistoryForward())
304 wxMessageBox(_("No more items in history!"));
308 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
310 m_Processor
->Enable(!m_Processor
->IsEnabled());
311 m_Html
->LoadPage(m_Html
->GetOpenedPage());
314 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
316 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
318 // skipping this event the default behaviour (load the clicked URL)
323 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
325 wxLogMessage(wxT("Mouse moved over cell %d at %d;%d"),
326 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
329 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
331 wxLogMessage(wxT("Click over cell %d at %d;%d"),
332 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
334 // if we don't skip the event, OnHtmlLinkClicked won't be called!
338 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
340 wxString
*WXUNUSED(redirect
)) const
342 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);
347 BEGIN_EVENT_TABLE(MyHtmlWindow
, wxHtmlWindow
)
348 EVT_TEXT_COPY(wxID_ANY
, MyHtmlWindow::OnClipboardEvent
)
351 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
353 // explicitly call wxHtmlWindow::CopySelection() method
354 // and show the first 100 characters of the text copied in the status bar
355 if ( CopySelection() )
357 wxTextDataObject data
;
358 if ( wxTheClipboard
&& wxTheClipboard
->GetData(data
) )
360 const wxString text
= data
.GetText();
361 const size_t maxTextLength
= 100;
363 wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"),
364 wxString(text
, maxTextLength
).c_str(),
365 (text
.length() > maxTextLength
) ? _T("...")
371 wxLogStatus(_T("Clipboard: nothing"));
373 #endif // wxUSE_CLIPBOARD