]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: wxHtml testing example | |
3f6bd7c1 DS |
4 | // Author: Vaclav Slavik |
5 | // Created: 1999-07-07 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
5526e819 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
788233da | 11 | #if defined(__GNUG__) && !defined(__APPLE__) |
5526e819 VS |
12 | #pragma implementation "test.cpp" |
13 | #pragma interface "test.cpp" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
92a19c2e | 17 | #include "wx/wxprec.h" |
5526e819 VS |
18 | |
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
3f6bd7c1 | 23 | // For all others, include the necessary headers (this file is usually all you |
be5a51fb | 24 | // need because it includes almost all "standard" wxWidgets headers |
5526e819 | 25 | #ifndef WX_PRECOMP |
67547666 | 26 | #include "wx/wx.h" |
5526e819 VS |
27 | #endif |
28 | ||
40091824 | 29 | #include "wx/image.h" |
bbb8f29b | 30 | #include "wx/sysopt.h" |
40091824 VS |
31 | #include "wx/html/htmlwin.h" |
32 | #include "wx/html/htmlproc.h" | |
33 | #include "wx/fs_inet.h" | |
34 | #include "wx/filedlg.h" | |
5526e819 VS |
35 | |
36 | // ---------------------------------------------------------------------------- | |
37 | // private classes | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | // Define a new application type, each program should derive a class from wxApp | |
3e729cbe VS |
41 | class MyApp : public wxApp |
42 | { | |
43 | public: | |
3f6bd7c1 | 44 | virtual bool OnInit(); |
3e729cbe | 45 | }; |
5526e819 | 46 | |
4bfa3189 WS |
47 | // Define a new html window type: this is a wrapper for handling wxHtmlWindow events |
48 | class MyHtmlWindow : public wxHtmlWindow | |
49 | { | |
50 | public: | |
51 | MyHtmlWindow(wxWindow *parent) : wxHtmlWindow( parent ) { } | |
52 | ||
53 | virtual wxHtmlOpeningStatus OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
54 | const wxString& WXUNUSED(url), | |
55 | wxString *WXUNUSED(redirect)) const; | |
56 | ||
57 | private: | |
58 | DECLARE_NO_COPY_CLASS(MyHtmlWindow) | |
59 | }; | |
60 | ||
5526e819 | 61 | // Define a new frame type: this is going to be our main frame |
3e729cbe VS |
62 | class MyFrame : public wxFrame |
63 | { | |
64 | public: | |
3f6bd7c1 | 65 | // ctor(s) |
3e729cbe VS |
66 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
67 | ||
3f6bd7c1 | 68 | // event handlers (these functions should _not_ be virtual) |
3e729cbe VS |
69 | void OnQuit(wxCommandEvent& event); |
70 | void OnPageOpen(wxCommandEvent& event); | |
71 | void OnBack(wxCommandEvent& event); | |
72 | void OnForward(wxCommandEvent& event); | |
73 | void OnProcessor(wxCommandEvent& event); | |
74 | ||
75 | private: | |
4bfa3189 | 76 | MyHtmlWindow *m_Html; |
3f6bd7c1 DS |
77 | wxHtmlProcessor *m_Processor; |
78 | ||
be5a51fb | 79 | // Any class wishing to process wxWidgets events must use this macro |
3f6bd7c1 | 80 | DECLARE_EVENT_TABLE() |
3e729cbe VS |
81 | }; |
82 | ||
83 | ||
84 | class BoldProcessor : public wxHtmlProcessor | |
85 | { | |
3f6bd7c1 DS |
86 | public: |
87 | virtual wxString Process(const wxString& s) const | |
88 | { | |
89 | wxString r(s); | |
90 | r.Replace(wxT("<b>"), wxEmptyString); | |
91 | r.Replace(wxT("<B>"), wxEmptyString); | |
92 | r.Replace(wxT("</b>"), wxEmptyString); | |
93 | r.Replace(wxT("</B>"), wxEmptyString); | |
94 | ||
95 | return r; | |
96 | } | |
3e729cbe | 97 | }; |
5526e819 VS |
98 | |
99 | // ---------------------------------------------------------------------------- | |
100 | // constants | |
101 | // ---------------------------------------------------------------------------- | |
102 | ||
103 | // IDs for the controls and the menu commands | |
3f6bd7c1 DS |
104 | enum |
105 | { | |
5526e819 | 106 | // menu items |
3f6bd7c1 DS |
107 | ID_PageOpen = wxID_HIGHEST, |
108 | ID_Back, | |
109 | ID_Forward, | |
110 | ID_Processor | |
111 | }; | |
5526e819 VS |
112 | |
113 | // ---------------------------------------------------------------------------- | |
be5a51fb | 114 | // event tables and other macros for wxWidgets |
5526e819 VS |
115 | // ---------------------------------------------------------------------------- |
116 | ||
3f6bd7c1 DS |
117 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
118 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
119 | EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen) | |
120 | EVT_MENU(ID_Back, MyFrame::OnBack) | |
121 | EVT_MENU(ID_Forward, MyFrame::OnForward) | |
122 | EVT_MENU(ID_Processor, MyFrame::OnProcessor) | |
123 | END_EVENT_TABLE() | |
124 | ||
125 | IMPLEMENT_APP(MyApp) | |
126 | ||
127 | // ============================================================================ | |
128 | // implementation | |
129 | // ============================================================================ | |
130 | ||
131 | // ---------------------------------------------------------------------------- | |
132 | // the application class | |
133 | // ---------------------------------------------------------------------------- | |
134 | ||
135 | // `Main program' equivalent: the program execution "starts" here | |
136 | bool MyApp::OnInit() | |
137 | { | |
954cc8d2 | 138 | #if wxUSE_SYSTEM_OPTIONS |
3f6bd7c1 | 139 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 140 | #endif |
bbb8f29b | 141 | |
3f6bd7c1 | 142 | wxInitAllImageHandlers(); |
954cc8d2 | 143 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
3f6bd7c1 | 144 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 145 | #endif |
5612e524 | 146 | |
be5a51fb | 147 | SetVendorName(wxT("wxWidgets")); |
3f6bd7c1 DS |
148 | SetAppName(wxT("wxHtmlTest")); |
149 | // the following call to wxConfig::Get will use it to create an object... | |
5612e524 | 150 | |
5526e819 | 151 | // Create the main application window |
3f6bd7c1 DS |
152 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), |
153 | wxDefaultPosition, wxSize(640, 480)); | |
154 | ||
155 | frame->Show(); | |
156 | ||
157 | return true /* continue running */; | |
158 | } | |
5526e819 VS |
159 | |
160 | // ---------------------------------------------------------------------------- | |
161 | // main frame | |
162 | // ---------------------------------------------------------------------------- | |
163 | ||
5526e819 | 164 | // frame constructor |
3f6bd7c1 DS |
165 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
166 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size, | |
167 | wxDEFAULT_FRAME_STYLE, wxT("html_test_app")) | |
168 | { | |
5526e819 | 169 | // create a menu bar |
3f6bd7c1 DS |
170 | wxMenu *menuFile = new wxMenu; |
171 | wxMenu *menuNav = new wxMenu; | |
172 | ||
173 | menuFile->Append(ID_PageOpen, _("&Open HTML page...")); | |
174 | menuFile->AppendSeparator(); | |
175 | menuFile->Append(ID_Processor, _("&Remove bold attribute"), | |
176 | wxEmptyString, wxITEM_CHECK); | |
177 | ||
178 | menuFile->AppendSeparator(); | |
179 | menuFile->Append(wxID_EXIT, _("&Close frame")); | |
180 | menuNav->Append(ID_Back, _("Go &BACK")); | |
181 | menuNav->Append(ID_Forward, _("Go &FORWARD")); | |
5526e819 VS |
182 | |
183 | // now append the freshly created menu to the menu bar... | |
3f6bd7c1 DS |
184 | wxMenuBar *menuBar = new wxMenuBar; |
185 | menuBar->Append(menuFile, _("&File")); | |
186 | menuBar->Append(menuNav, _("&Navigate")); | |
5526e819 VS |
187 | |
188 | // ... and attach this menu bar to the frame | |
3f6bd7c1 DS |
189 | SetMenuBar(menuBar); |
190 | ||
191 | #if wxUSE_ACCEL | |
192 | // Create convenient accelerators for Back and Forward navigation | |
193 | wxAcceleratorEntry entries[2]; | |
194 | entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back); | |
195 | entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward); | |
196 | ||
197 | wxAcceleratorTable accel(WXSIZEOF(entries), entries); | |
198 | SetAcceleratorTable(accel); | |
199 | #endif // wxUSE_ACCEL | |
200 | ||
8520f137 | 201 | #if wxUSE_STATUSBAR |
4bfa3189 | 202 | CreateStatusBar(2); |
8520f137 | 203 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
204 | |
205 | m_Processor = new BoldProcessor; | |
206 | m_Processor->Enable(false); | |
4bfa3189 | 207 | m_Html = new MyHtmlWindow(this); |
3f6bd7c1 | 208 | m_Html->SetRelatedFrame(this, _("HTML : %s")); |
8520f137 | 209 | #if wxUSE_STATUSBAR |
3f6bd7c1 | 210 | m_Html->SetRelatedStatusBar(0); |
8520f137 | 211 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
212 | m_Html->ReadCustomization(wxConfig::Get()); |
213 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
214 | m_Html->AddProcessor(m_Processor); | |
215 | } | |
5526e819 VS |
216 | |
217 | ||
218 | // event handlers | |
219 | ||
3e729cbe VS |
220 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
221 | { | |
3f6bd7c1 DS |
222 | m_Html->WriteCustomization(wxConfig::Get()); |
223 | delete wxConfig::Set(NULL); | |
224 | ||
225 | // true is to force the frame to close | |
226 | Close(true); | |
3e729cbe VS |
227 | } |
228 | ||
229 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
230 | { | |
3f6bd7c1 DS |
231 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, |
232 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm")); | |
233 | ||
234 | if (p != wxEmptyString) | |
235 | m_Html->LoadPage(p); | |
3e729cbe VS |
236 | } |
237 | ||
238 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
239 | { | |
3f6bd7c1 DS |
240 | if (!m_Html->HistoryBack()) |
241 | { | |
242 | wxMessageBox(_("You reached prehistory era!")); | |
243 | } | |
3e729cbe VS |
244 | } |
245 | ||
246 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
247 | { | |
3f6bd7c1 DS |
248 | if (!m_Html->HistoryForward()) |
249 | { | |
250 | wxMessageBox(_("No more items in history!")); | |
251 | } | |
3e729cbe VS |
252 | } |
253 | ||
254 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
255 | { | |
256 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
257 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 258 | } |
4bfa3189 WS |
259 | |
260 | wxHtmlOpeningStatus MyHtmlWindow::OnOpeningURL(wxHtmlURLType WXUNUSED(type), | |
261 | const wxString& url, | |
262 | wxString *WXUNUSED(redirect)) const | |
263 | { | |
264 | GetRelatedFrame()->SetStatusText(url + _T(" lately opened"),1); | |
265 | return wxHTML_OPEN; | |
266 | } |