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