]>
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 VS |
46 | |
47 | // Define a new frame type: this is going to be our main frame | |
3e729cbe VS |
48 | class MyFrame : public wxFrame |
49 | { | |
50 | public: | |
3f6bd7c1 | 51 | // ctor(s) |
3e729cbe VS |
52 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); |
53 | ||
3f6bd7c1 | 54 | // event handlers (these functions should _not_ be virtual) |
3e729cbe VS |
55 | void OnQuit(wxCommandEvent& event); |
56 | void OnPageOpen(wxCommandEvent& event); | |
57 | void OnBack(wxCommandEvent& event); | |
58 | void OnForward(wxCommandEvent& event); | |
59 | void OnProcessor(wxCommandEvent& event); | |
60 | ||
61 | private: | |
3f6bd7c1 DS |
62 | wxHtmlWindow *m_Html; |
63 | wxHtmlProcessor *m_Processor; | |
64 | ||
be5a51fb | 65 | // Any class wishing to process wxWidgets events must use this macro |
3f6bd7c1 | 66 | DECLARE_EVENT_TABLE() |
3e729cbe VS |
67 | }; |
68 | ||
69 | ||
70 | class BoldProcessor : public wxHtmlProcessor | |
71 | { | |
3f6bd7c1 DS |
72 | public: |
73 | virtual wxString Process(const wxString& s) const | |
74 | { | |
75 | wxString r(s); | |
76 | r.Replace(wxT("<b>"), wxEmptyString); | |
77 | r.Replace(wxT("<B>"), wxEmptyString); | |
78 | r.Replace(wxT("</b>"), wxEmptyString); | |
79 | r.Replace(wxT("</B>"), wxEmptyString); | |
80 | ||
81 | return r; | |
82 | } | |
3e729cbe | 83 | }; |
5526e819 VS |
84 | |
85 | // ---------------------------------------------------------------------------- | |
86 | // constants | |
87 | // ---------------------------------------------------------------------------- | |
88 | ||
89 | // IDs for the controls and the menu commands | |
3f6bd7c1 DS |
90 | enum |
91 | { | |
5526e819 | 92 | // menu items |
3f6bd7c1 DS |
93 | ID_PageOpen = wxID_HIGHEST, |
94 | ID_Back, | |
95 | ID_Forward, | |
96 | ID_Processor | |
97 | }; | |
5526e819 VS |
98 | |
99 | // ---------------------------------------------------------------------------- | |
be5a51fb | 100 | // event tables and other macros for wxWidgets |
5526e819 VS |
101 | // ---------------------------------------------------------------------------- |
102 | ||
3f6bd7c1 DS |
103 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) |
104 | EVT_MENU(wxID_EXIT, MyFrame::OnQuit) | |
105 | EVT_MENU(ID_PageOpen, MyFrame::OnPageOpen) | |
106 | EVT_MENU(ID_Back, MyFrame::OnBack) | |
107 | EVT_MENU(ID_Forward, MyFrame::OnForward) | |
108 | EVT_MENU(ID_Processor, MyFrame::OnProcessor) | |
109 | END_EVENT_TABLE() | |
110 | ||
111 | IMPLEMENT_APP(MyApp) | |
112 | ||
113 | // ============================================================================ | |
114 | // implementation | |
115 | // ============================================================================ | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // the application class | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | // `Main program' equivalent: the program execution "starts" here | |
122 | bool MyApp::OnInit() | |
123 | { | |
954cc8d2 | 124 | #if wxUSE_SYSTEM_OPTIONS |
3f6bd7c1 | 125 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 126 | #endif |
bbb8f29b | 127 | |
3f6bd7c1 | 128 | wxInitAllImageHandlers(); |
954cc8d2 | 129 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
3f6bd7c1 | 130 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 131 | #endif |
5612e524 | 132 | |
be5a51fb | 133 | SetVendorName(wxT("wxWidgets")); |
3f6bd7c1 DS |
134 | SetAppName(wxT("wxHtmlTest")); |
135 | // the following call to wxConfig::Get will use it to create an object... | |
5612e524 | 136 | |
5526e819 | 137 | // Create the main application window |
3f6bd7c1 DS |
138 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), |
139 | wxDefaultPosition, wxSize(640, 480)); | |
140 | ||
141 | frame->Show(); | |
142 | ||
143 | return true /* continue running */; | |
144 | } | |
5526e819 VS |
145 | |
146 | // ---------------------------------------------------------------------------- | |
147 | // main frame | |
148 | // ---------------------------------------------------------------------------- | |
149 | ||
5526e819 | 150 | // frame constructor |
3f6bd7c1 DS |
151 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) |
152 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size, | |
153 | wxDEFAULT_FRAME_STYLE, wxT("html_test_app")) | |
154 | { | |
5526e819 | 155 | // create a menu bar |
3f6bd7c1 DS |
156 | wxMenu *menuFile = new wxMenu; |
157 | wxMenu *menuNav = new wxMenu; | |
158 | ||
159 | menuFile->Append(ID_PageOpen, _("&Open HTML page...")); | |
160 | menuFile->AppendSeparator(); | |
161 | menuFile->Append(ID_Processor, _("&Remove bold attribute"), | |
162 | wxEmptyString, wxITEM_CHECK); | |
163 | ||
164 | menuFile->AppendSeparator(); | |
165 | menuFile->Append(wxID_EXIT, _("&Close frame")); | |
166 | menuNav->Append(ID_Back, _("Go &BACK")); | |
167 | menuNav->Append(ID_Forward, _("Go &FORWARD")); | |
5526e819 VS |
168 | |
169 | // now append the freshly created menu to the menu bar... | |
3f6bd7c1 DS |
170 | wxMenuBar *menuBar = new wxMenuBar; |
171 | menuBar->Append(menuFile, _("&File")); | |
172 | menuBar->Append(menuNav, _("&Navigate")); | |
5526e819 VS |
173 | |
174 | // ... and attach this menu bar to the frame | |
3f6bd7c1 DS |
175 | SetMenuBar(menuBar); |
176 | ||
177 | #if wxUSE_ACCEL | |
178 | // Create convenient accelerators for Back and Forward navigation | |
179 | wxAcceleratorEntry entries[2]; | |
180 | entries[0].Set(wxACCEL_ALT, WXK_LEFT, ID_Back); | |
181 | entries[1].Set(wxACCEL_ALT, WXK_RIGHT, ID_Forward); | |
182 | ||
183 | wxAcceleratorTable accel(WXSIZEOF(entries), entries); | |
184 | SetAcceleratorTable(accel); | |
185 | #endif // wxUSE_ACCEL | |
186 | ||
8520f137 | 187 | #if wxUSE_STATUSBAR |
3f6bd7c1 | 188 | CreateStatusBar(1); |
8520f137 | 189 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
190 | |
191 | m_Processor = new BoldProcessor; | |
192 | m_Processor->Enable(false); | |
193 | m_Html = new wxHtmlWindow(this); | |
194 | m_Html->SetRelatedFrame(this, _("HTML : %s")); | |
8520f137 | 195 | #if wxUSE_STATUSBAR |
3f6bd7c1 | 196 | m_Html->SetRelatedStatusBar(0); |
8520f137 | 197 | #endif // wxUSE_STATUSBAR |
3f6bd7c1 DS |
198 | m_Html->ReadCustomization(wxConfig::Get()); |
199 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
200 | m_Html->AddProcessor(m_Processor); | |
201 | } | |
5526e819 VS |
202 | |
203 | ||
204 | // event handlers | |
205 | ||
3e729cbe VS |
206 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
207 | { | |
3f6bd7c1 DS |
208 | m_Html->WriteCustomization(wxConfig::Get()); |
209 | delete wxConfig::Set(NULL); | |
210 | ||
211 | // true is to force the frame to close | |
212 | Close(true); | |
3e729cbe VS |
213 | } |
214 | ||
215 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
216 | { | |
3f6bd7c1 DS |
217 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, |
218 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm")); | |
219 | ||
220 | if (p != wxEmptyString) | |
221 | m_Html->LoadPage(p); | |
3e729cbe VS |
222 | } |
223 | ||
224 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
225 | { | |
3f6bd7c1 DS |
226 | if (!m_Html->HistoryBack()) |
227 | { | |
228 | wxMessageBox(_("You reached prehistory era!")); | |
229 | } | |
3e729cbe VS |
230 | } |
231 | ||
232 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
233 | { | |
3f6bd7c1 DS |
234 | if (!m_Html->HistoryForward()) |
235 | { | |
236 | wxMessageBox(_("No more items in history!")); | |
237 | } | |
3e729cbe VS |
238 | } |
239 | ||
240 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
241 | { | |
242 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
243 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 244 | } |