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 OnDefaultBrowser(wxCommandEvent
& event
);
70 void OnBack(wxCommandEvent
& event
);
71 void OnForward(wxCommandEvent
& event
);
72 void OnProcessor(wxCommandEvent
& event
);
74 void OnHtmlLinkClicked(wxHtmlLinkEvent
& event
);
75 void OnHtmlCellHover(wxHtmlCellEvent
&event
);
76 void OnHtmlCellClicked(wxHtmlCellEvent
&event
);
80 wxHtmlProcessor
*m_Processor
;
82 // Any class wishing to process wxWidgets events must use this macro
87 class BoldProcessor
: public wxHtmlProcessor
90 virtual wxString
Process(const wxString
& s
) const
93 r
.Replace(wxT("<b>"), wxEmptyString
);
94 r
.Replace(wxT("<B>"), wxEmptyString
);
95 r
.Replace(wxT("</b>"), wxEmptyString
);
96 r
.Replace(wxT("</B>"), wxEmptyString
);
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 // IDs for the controls and the menu commands
110 ID_PageOpen
= wxID_HIGHEST
,
117 // ----------------------------------------------------------------------------
118 // event tables and other macros for wxWidgets
119 // ----------------------------------------------------------------------------
121 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
122 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
123 EVT_MENU(ID_PageOpen
, MyFrame::OnPageOpen
)
124 EVT_MENU(ID_DefaultBrowser
, MyFrame::OnDefaultBrowser
)
125 EVT_MENU(ID_Back
, MyFrame::OnBack
)
126 EVT_MENU(ID_Forward
, MyFrame::OnForward
)
127 EVT_MENU(ID_Processor
, MyFrame::OnProcessor
)
129 EVT_HTML_LINK_CLICKED(wxID_ANY
, MyFrame::OnHtmlLinkClicked
)
130 EVT_HTML_CELL_HOVER(wxID_ANY
, MyFrame::OnHtmlCellHover
)
131 EVT_HTML_CELL_CLICKED(wxID_ANY
, MyFrame::OnHtmlCellClicked
)
136 // ============================================================================
138 // ============================================================================
140 // ----------------------------------------------------------------------------
141 // the application class
142 // ----------------------------------------------------------------------------
144 // `Main program' equivalent: the program execution "starts" here
147 #if wxUSE_SYSTEM_OPTIONS
148 wxSystemOptions::SetOption(wxT("no-maskblt"), 1);
151 wxInitAllImageHandlers();
152 #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS
153 wxFileSystem::AddHandler(new wxInternetFSHandler
);
156 SetVendorName(wxT("wxWidgets"));
157 SetAppName(wxT("wxHtmlTest"));
158 // the following call to wxConfig::Get will use it to create an object...
160 // Create the main application window
161 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
162 wxDefaultPosition
, wxSize(640, 480));
166 return true /* continue running */;
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
174 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
175 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
,
176 wxDEFAULT_FRAME_STYLE
, wxT("html_test_app"))
179 wxMenu
*menuFile
= new wxMenu
;
180 wxMenu
*menuNav
= new wxMenu
;
182 menuFile
->Append(ID_PageOpen
, _("&Open HTML page..."));
183 menuFile
->Append(ID_DefaultBrowser
, _("&Open current page with default browser"));
184 menuFile
->AppendSeparator();
185 menuFile
->Append(ID_Processor
, _("&Remove bold attribute"),
186 wxEmptyString
, wxITEM_CHECK
);
188 menuFile
->AppendSeparator();
189 menuFile
->Append(wxID_EXIT
, _("&Close frame"));
190 menuNav
->Append(ID_Back
, _("Go &BACK"));
191 menuNav
->Append(ID_Forward
, _("Go &FORWARD"));
193 // now append the freshly created menu to the menu bar...
194 wxMenuBar
*menuBar
= new wxMenuBar
;
195 menuBar
->Append(menuFile
, _("&File"));
196 menuBar
->Append(menuNav
, _("&Navigate"));
198 // ... and attach this menu bar to the frame
201 SetIcon(wxIcon(sample_xpm
));
204 // Create convenient accelerators for Back and Forward navigation
205 wxAcceleratorEntry entries
[2];
206 entries
[0].Set(wxACCEL_ALT
, WXK_LEFT
, ID_Back
);
207 entries
[1].Set(wxACCEL_ALT
, WXK_RIGHT
, ID_Forward
);
209 wxAcceleratorTable
accel(WXSIZEOF(entries
), entries
);
210 SetAcceleratorTable(accel
);
211 #endif // wxUSE_ACCEL
215 #endif // wxUSE_STATUSBAR
217 m_Processor
= new BoldProcessor
;
218 m_Processor
->Enable(false);
219 m_Html
= new MyHtmlWindow(this);
220 m_Html
->SetRelatedFrame(this, _("HTML : %s"));
222 m_Html
->SetRelatedStatusBar(0);
223 #endif // wxUSE_STATUSBAR
224 m_Html
->ReadCustomization(wxConfig::Get());
225 m_Html
->LoadFile(wxFileName(wxT("test.htm")));
226 m_Html
->AddProcessor(m_Processor
);
228 wxTextCtrl
*text
= new wxTextCtrl(this, wxID_ANY
, _T(""),
229 wxDefaultPosition
, wxDefaultSize
,
232 delete wxLog::SetActiveTarget(new wxLogTextCtrl(text
));
234 wxSizer
*sz
= new wxBoxSizer(wxVERTICAL
);
235 sz
->Add(m_Html
, 3, wxGROW
);
236 sz
->Add(text
, 1, wxGROW
);
243 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
245 m_Html
->WriteCustomization(wxConfig::Get());
246 delete wxConfig::Set(NULL
);
248 // true is to force the frame to close
252 void MyFrame::OnPageOpen(wxCommandEvent
& WXUNUSED(event
))
255 wxString p
= wxFileSelector(_("Open HTML document"), wxEmptyString
,
256 wxEmptyString
, wxEmptyString
, wxT("HTML files|*.htm;*.html"));
259 m_Html
->LoadFile(wxFileName(p
));
260 #endif // wxUSE_FILEDLG
263 void MyFrame::OnDefaultBrowser(wxCommandEvent
& WXUNUSED(event
))
265 wxString page
= m_Html
->GetOpenedPage();
268 wxLaunchDefaultBrowser(page
);
272 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
274 if (!m_Html
->HistoryBack())
276 wxMessageBox(_("You reached prehistory era!"));
280 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
282 if (!m_Html
->HistoryForward())
284 wxMessageBox(_("No more items in history!"));
288 void MyFrame::OnProcessor(wxCommandEvent
& WXUNUSED(event
))
290 m_Processor
->Enable(!m_Processor
->IsEnabled());
291 m_Html
->LoadPage(m_Html
->GetOpenedPage());
294 void MyFrame::OnHtmlLinkClicked(wxHtmlLinkEvent
&event
)
296 wxLogMessage(wxT("The url '%s' has been clicked!"), event
.GetLinkInfo().GetHref().c_str());
298 // skipping this event the default behaviour (load the clicked URL)
303 void MyFrame::OnHtmlCellHover(wxHtmlCellEvent
&event
)
305 wxLogMessage(wxT("Mouse moved over cell %d at %d;%d"),
306 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
309 void MyFrame::OnHtmlCellClicked(wxHtmlCellEvent
&event
)
311 wxLogMessage(wxT("Click over cell %d at %d;%d"),
312 event
.GetCell(), event
.GetPoint().x
, event
.GetPoint().y
);
314 // if we don't skip the event, OnHtmlLinkClicked won't be called!
318 wxHtmlOpeningStatus
MyHtmlWindow::OnOpeningURL(wxHtmlURLType
WXUNUSED(type
),
320 wxString
*WXUNUSED(redirect
)) const
322 GetRelatedFrame()->SetStatusText(url
+ _T(" lately opened"),1);