]>
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 |
5526e819 VS |
24 | // need because it includes almost all "standard" wxWindows headers |
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 | ||
65 | // Any class wishing to process wxWindows events must use this macro | |
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 | // ---------------------------------------------------------------------------- | |
100 | // event tables and other macros for wxWindows | |
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 | |
3f6bd7c1 DS |
133 | SetVendorName(wxT("wxWindows")); |
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 | ||
187 | CreateStatusBar(1); | |
188 | ||
189 | m_Processor = new BoldProcessor; | |
190 | m_Processor->Enable(false); | |
191 | m_Html = new wxHtmlWindow(this); | |
192 | m_Html->SetRelatedFrame(this, _("HTML : %s")); | |
193 | m_Html->SetRelatedStatusBar(0); | |
194 | m_Html->ReadCustomization(wxConfig::Get()); | |
195 | m_Html->LoadFile(wxFileName(wxT("test.htm"))); | |
196 | m_Html->AddProcessor(m_Processor); | |
197 | } | |
5526e819 VS |
198 | |
199 | ||
200 | // event handlers | |
201 | ||
3e729cbe VS |
202 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
203 | { | |
3f6bd7c1 DS |
204 | m_Html->WriteCustomization(wxConfig::Get()); |
205 | delete wxConfig::Set(NULL); | |
206 | ||
207 | // true is to force the frame to close | |
208 | Close(true); | |
3e729cbe VS |
209 | } |
210 | ||
211 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
212 | { | |
3f6bd7c1 DS |
213 | wxString p = wxFileSelector(_("Open HTML document"), wxEmptyString, |
214 | wxEmptyString, wxEmptyString, wxT("HTML files|*.htm")); | |
215 | ||
216 | if (p != wxEmptyString) | |
217 | m_Html->LoadPage(p); | |
3e729cbe VS |
218 | } |
219 | ||
220 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
221 | { | |
3f6bd7c1 DS |
222 | if (!m_Html->HistoryBack()) |
223 | { | |
224 | wxMessageBox(_("You reached prehistory era!")); | |
225 | } | |
3e729cbe VS |
226 | } |
227 | ||
228 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
229 | { | |
3f6bd7c1 DS |
230 | if (!m_Html->HistoryForward()) |
231 | { | |
232 | wxMessageBox(_("No more items in history!")); | |
233 | } | |
3e729cbe VS |
234 | } |
235 | ||
236 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
237 | { | |
238 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
239 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 240 | } |