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 ( !wxApp::OnInit() )
160 #if wxUSE_SYSTEM_OPTIONS
161 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
164 wxInitAllImageHandlers();
165 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
166 wxFileSystem::AddHandler(new wxInternetFSHandler
);
169 SetVendorName(wxT("wxWidgets"));
170 SetAppName(wxT("wxHtmlTest"));
171 // the following call to wxConfig::Get will use it to create an object...
173 // Create the main application window
174 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
175 wxDefaultPosition
, wxSize(640, 480));
179 return true /* continue running */;
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
187 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
188 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
189 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
192 wxMenu
*menuFile
= new wxMenu
;
193 wxMenu
*menuNav
= new wxMenu
;
195 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
196 menuFile
->Append(ID_DefaultLocalBrowser
, _("&Open current page with default browser"));
197 menuFile
->Append(ID_DefaultWebBrowser
, _("Open a &web page with default browser"));
198 menuFile
->AppendSeparator();
199 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
200 wxEmptyString
, wxITEM_CHECK
);
202 menuFile
->AppendSeparator();
203 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
204 menuNav
->Append(ID_Back
, _("Go &BACK"));
205 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
207 // now append the freshly created menu to the menu bar...
208 wxMenuBar
*menuBar
= new wxMenuBar
;
209 menuBar
->Append(menuFile
, _("&File"));
210 menuBar
->Append(menuNav
, _("&Navigate"));
212 // ... and attach this menu bar to the frame
215 SetIcon(wxIcon(sample_xpm
));
218 // Create convenient accelerators for Back and Forward navigation
219 wxAcceleratorEntry entries
[2];
220 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
221 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
223 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
224 SetAcceleratorTable(accel
);
225 #endif // wxUSE_ACCEL
229 #endif // wxUSE_STATUSBAR
231 m_Processor
= new BoldProcessor
;
232 m_Processor
->Enable(false);
233 m_Html
= new MyHtmlWindow(this);
234 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
236 m_Html
->SetRelatedStatusBar(0);
237 #endif // wxUSE_STATUSBAR
238 m_Html
->ReadCustomization(wxConfig::Get());
239 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
240 m_Html
->AddProcessor(m_Processor
);
242 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
243 wxDefaultPosition
, wxDefaultSize
,
246 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
248 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
249 sz
->Add(m_Html
, 3, wxGROW
);
250 sz
->Add(text
, 1, wxGROW
);
257 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
259 m_Html
->WriteCustomization(wxConfig::Get());
260 delete wxConfig::Set(NULL
);
262 // true is to force the frame to close
266 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
269 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
270 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
273 m_Html
->LoadFile(wxFileName(p
));
274 #endif // wxUSE_FILEDLG
277 void MyFrame::OnDefaultLocalBrowser(wxCommandEvent
& WXUNUSED(event
))
279 wxString page
= m_Html
->GetOpenedPage();
282 wxLaunchDefaultBrowser(page
);
286 void MyFrame::OnDefaultWebBrowser(wxCommandEvent
& WXUNUSED(event
))
288 wxString page
= m_Html
->GetOpenedPage();
291 wxLaunchDefaultBrowser(wxT("http://www.google.com"));
295 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
297 if (!m_Html
->HistoryBack())
299 wxMessageBox(_("You reached prehistory era!"));
303 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
305 if (!m_Html
->HistoryForward())
307 wxMessageBox(_("No more items in history!"));
311 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
313 m_Processor
->Enable(!m_Processor
->IsEnabled());
314 m_Html
->LoadPage(m_Html
->GetOpenedPage());
317 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
319 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
321 // skipping this event the default behaviour (load the clicked URL)
326 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
328 wxLogMessage(wxT("Mouse moved over cell %p at %d;%d"),
329 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
332 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
334 wxLogMessage(wxT("Click over cell %p at %d;%d"),
335 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
337 // if we don't skip the event, OnHtmlLinkClicked won't be called!
341 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
343 wxString
*WXUNUSED(redirect
)) const
345 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);
350 BEGIN_EVENT_TABLE(MyHtmlWindow
, wxHtmlWindow
)
351 EVT_TEXT_COPY(wxID_ANY
, MyHtmlWindow::OnClipboardEvent
)
354 void MyHtmlWindow::OnClipboardEvent(wxClipboardTextEvent
& WXUNUSED(event
))
356 // explicitly call wxHtmlWindow::CopySelection() method
357 // and show the first 100 characters of the text copied in the status bar
358 if ( CopySelection() )
360 wxTextDataObject data
;
361 if ( wxTheClipboard
&& wxTheClipboard
->Open() && wxTheClipboard
->GetData(data
) )
363 const wxString text
= data
.GetText();
364 const size_t maxTextLength
= 100;
366 wxLogStatus(wxString::Format(_T("Clipboard: '%s%s'"),
367 wxString(text
, maxTextLength
).c_str(),
368 (text
.length() > maxTextLength
) ? _T("...")
370 wxTheClipboard
->Close();
376 wxLogStatus(_T("Clipboard: nothing"));
378 #endif // wxUSE_CLIPBOARD