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