]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: test.cpp | |
3 | // Purpose: wxHtml testing example | |
4 | ///////////////////////////////////////////////////////////////////////////// | |
5 | ||
6 | #ifdef __GNUG__ | |
7 | #pragma implementation "test.cpp" | |
8 | #pragma interface "test.cpp" | |
9 | #endif | |
10 | ||
11 | // For compilers that support precompilation, includes "wx/wx.h". | |
92a19c2e | 12 | #include "wx/wxprec.h" |
5526e819 VS |
13 | |
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | // for all others, include the necessary headers (this file is usually all you | |
19 | // need because it includes almost all "standard" wxWindows headers | |
20 | #ifndef WX_PRECOMP | |
67547666 | 21 | #include "wx/wx.h" |
5526e819 VS |
22 | #endif |
23 | ||
40091824 | 24 | #include "wx/image.h" |
bbb8f29b | 25 | #include "wx/sysopt.h" |
40091824 VS |
26 | #include "wx/html/htmlwin.h" |
27 | #include "wx/html/htmlproc.h" | |
28 | #include "wx/fs_inet.h" | |
29 | #include "wx/filedlg.h" | |
5526e819 VS |
30 | |
31 | // ---------------------------------------------------------------------------- | |
32 | // private classes | |
33 | // ---------------------------------------------------------------------------- | |
34 | ||
35 | // Define a new application type, each program should derive a class from wxApp | |
3e729cbe VS |
36 | class MyApp : public wxApp |
37 | { | |
38 | public: | |
39 | // override base class virtuals | |
40 | // ---------------------------- | |
41 | ||
42 | // this one is called on application startup and is a good place for the app | |
43 | // initialization (doing it here and not in the ctor allows to have an error | |
44 | // return: if OnInit() returns false, the application terminates) | |
45 | virtual bool OnInit(); | |
46 | }; | |
5526e819 VS |
47 | |
48 | // Define a new frame type: this is going to be our main frame | |
3e729cbe VS |
49 | class MyFrame : public wxFrame |
50 | { | |
51 | public: | |
52 | // ctor(s) | |
53 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
54 | ||
55 | // event handlers (these functions should _not_ be virtual) | |
56 | void OnQuit(wxCommandEvent& event); | |
57 | void OnPageOpen(wxCommandEvent& event); | |
58 | void OnBack(wxCommandEvent& event); | |
59 | void OnForward(wxCommandEvent& event); | |
60 | void OnProcessor(wxCommandEvent& event); | |
61 | ||
62 | private: | |
63 | wxHtmlWindow *m_Html; | |
64 | wxHtmlProcessor *m_Processor; | |
65 | // any class wishing to process wxWindows events must use this macro | |
66 | DECLARE_EVENT_TABLE() | |
67 | }; | |
68 | ||
69 | ||
70 | class BoldProcessor : public wxHtmlProcessor | |
71 | { | |
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); | |
3e729cbe VS |
80 | return r; |
81 | } | |
82 | }; | |
5526e819 VS |
83 | |
84 | // ---------------------------------------------------------------------------- | |
85 | // constants | |
86 | // ---------------------------------------------------------------------------- | |
87 | ||
88 | // IDs for the controls and the menu commands | |
89 | enum | |
90 | { | |
91 | // menu items | |
92 | Minimal_Quit = 1, | |
2bdebb91 | 93 | Minimal_PageOpen, |
5526e819 VS |
94 | Minimal_Back, |
95 | Minimal_Forward, | |
3e729cbe | 96 | Minimal_Processor, |
5526e819 VS |
97 | |
98 | // controls start here (the numbers are, of course, arbitrary) | |
b6fa52db | 99 | Minimal_Text = 1000 |
5526e819 VS |
100 | }; |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // event tables and other macros for wxWindows | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
106 | // the event tables connect the wxWindows events with the functions (event | |
107 | // handlers) which process them. It can be also done at run-time, but for the | |
108 | // simple menu events like this the static method is much simpler. | |
109 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
110 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
2bdebb91 | 111 | EVT_MENU(Minimal_PageOpen, MyFrame::OnPageOpen) |
5526e819 VS |
112 | EVT_MENU(Minimal_Back, MyFrame::OnBack) |
113 | EVT_MENU(Minimal_Forward, MyFrame::OnForward) | |
3e729cbe | 114 | EVT_MENU(Minimal_Processor, MyFrame::OnProcessor) |
5526e819 VS |
115 | END_EVENT_TABLE() |
116 | ||
117 | // Create a new application object: this macro will allow wxWindows to create | |
118 | // the application object during program execution (it's better than using a | |
119 | // static object for many reasons) and also declares the accessor function | |
120 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
121 | // not wxApp) | |
122 | IMPLEMENT_APP(MyApp) | |
123 | ||
124 | // ============================================================================ | |
125 | // implementation | |
126 | // ============================================================================ | |
127 | ||
128 | // ---------------------------------------------------------------------------- | |
129 | // the application class | |
130 | // ---------------------------------------------------------------------------- | |
131 | // `Main program' equivalent: the program execution "starts" here | |
132 | bool MyApp::OnInit() | |
133 | { | |
27523ad0 | 134 | wxLog::AddTraceMask(wxT("strconv")); |
954cc8d2 | 135 | #if wxUSE_SYSTEM_OPTIONS |
bbb8f29b | 136 | wxSystemOptions::SetOption(wxT("no-maskblt"), 1); |
954cc8d2 | 137 | #endif |
bbb8f29b | 138 | |
8b260fbe | 139 | wxInitAllImageHandlers(); |
954cc8d2 | 140 | #if wxUSE_FS_INET && wxUSE_STREAMS && wxUSE_SOCKETS |
bd6882ee | 141 | wxFileSystem::AddHandler(new wxInternetFSHandler); |
954cc8d2 | 142 | #endif |
5612e524 VS |
143 | |
144 | SetVendorName("wxWindows"); | |
145 | SetAppName("wxHtmlTest"); | |
146 | // the following call to wxConfig::Get will use it to create an object... | |
147 | ||
5526e819 VS |
148 | // Create the main application window |
149 | MyFrame *frame = new MyFrame("wxHtmlWindow testing application", | |
150 | wxPoint(50, 50), wxSize(640, 480)); | |
151 | ||
152 | // Show it and tell the application that it's our main window | |
153 | // @@@ what does it do exactly, in fact? is it necessary here? | |
154 | frame->Show(TRUE); | |
155 | SetTopWindow(frame); | |
156 | ||
157 | ||
158 | // success: wxApp::OnRun() will be called which will enter the main message | |
159 | // loop and the application will run. If we returned FALSE here, the | |
160 | // application would exit immediately. | |
161 | return TRUE; | |
162 | } | |
163 | ||
164 | // ---------------------------------------------------------------------------- | |
165 | // main frame | |
166 | // ---------------------------------------------------------------------------- | |
167 | ||
5526e819 VS |
168 | |
169 | // frame constructor | |
170 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
b7141364 | 171 | : wxFrame((wxFrame *)NULL, -1, title, pos, size, wxDEFAULT_FRAME_STYLE, "html_test_app") |
5526e819 VS |
172 | { |
173 | // create a menu bar | |
174 | wxMenu *menuFile = new wxMenu; | |
175 | wxMenu *menuNav = new wxMenu; | |
176 | ||
2bdebb91 | 177 | menuFile->Append(Minimal_PageOpen, "&Open HTML page..."); |
3e729cbe VS |
178 | menuFile->AppendSeparator(); |
179 | menuFile->Append(Minimal_Processor, "&Remove bold attribute", "", TRUE); | |
180 | ||
5526e819 | 181 | menuFile->AppendSeparator(); |
2083616e | 182 | menuFile->Append(Minimal_Quit, "&Close frame"); |
5526e819 VS |
183 | menuNav->Append(Minimal_Back, "Go &BACK"); |
184 | menuNav->Append(Minimal_Forward, "Go &FORWARD"); | |
185 | ||
186 | // now append the freshly created menu to the menu bar... | |
187 | wxMenuBar *menuBar = new wxMenuBar; | |
188 | menuBar->Append(menuFile, "&File"); | |
189 | menuBar->Append(menuNav, "&Navigate"); | |
190 | ||
191 | // ... and attach this menu bar to the frame | |
192 | SetMenuBar(menuBar); | |
193 | ||
194 | CreateStatusBar(1); | |
195 | ||
3e729cbe VS |
196 | m_Processor = new BoldProcessor; |
197 | m_Processor->Enable(FALSE); | |
2083616e | 198 | m_Html = new wxHtmlWindow(this); |
3e729cbe VS |
199 | m_Html->SetRelatedFrame(this, "HTML : %s"); |
200 | m_Html->SetRelatedStatusBar(0); | |
201 | m_Html->ReadCustomization(wxConfig::Get()); | |
202 | m_Html->LoadPage("test.htm"); | |
203 | m_Html->AddProcessor(m_Processor); | |
5526e819 VS |
204 | } |
205 | ||
206 | ||
207 | // event handlers | |
208 | ||
3e729cbe VS |
209 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) |
210 | { | |
211 | // TRUE is to force the frame to close | |
212 | m_Html -> WriteCustomization(wxConfig::Get()); | |
213 | delete wxConfig::Set(NULL); | |
214 | Close(TRUE); | |
215 | } | |
216 | ||
217 | void MyFrame::OnPageOpen(wxCommandEvent& WXUNUSED(event)) | |
218 | { | |
4693b20c | 219 | wxString p = wxFileSelector(wxT("Open HTML document"), wxT(""), wxT(""), wxT(""), wxT("HTML files|*.htm")); |
3e729cbe VS |
220 | if (p != wxEmptyString) |
221 | m_Html -> LoadPage(p); | |
222 | } | |
223 | ||
224 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
225 | { | |
226 | if (!m_Html -> HistoryBack()) wxMessageBox("You reached prehistory era!"); | |
227 | } | |
228 | ||
229 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
230 | { | |
231 | if (!m_Html -> HistoryForward()) wxMessageBox("No more items in history!"); | |
232 | } | |
233 | ||
234 | void MyFrame::OnProcessor(wxCommandEvent& WXUNUSED(event)) | |
235 | { | |
236 | m_Processor->Enable(!m_Processor->IsEnabled()); | |
237 | m_Html->LoadPage(m_Html->GetOpenedPage()); | |
3e729cbe | 238 | } |