]>
Commit | Line | Data |
---|---|---|
5526e819 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: virtua;.cpp | |
3 | // Purpose: wxHtml testing example | |
4 | // demonstrates virtual file systems feature | |
5 | ///////////////////////////////////////////////////////////////////////////// | |
6 | ||
7 | #ifdef __GNUG__ | |
8 | #pragma implementation "test.cpp" | |
9 | #pragma interface "test.cpp" | |
10 | #endif | |
11 | ||
12 | // For compilers that support precompilation, includes "wx/wx.h". | |
92a19c2e | 13 | #include "wx/wxprec.h" |
5526e819 VS |
14 | |
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | // for all others, include the necessary headers (this file is usually all you | |
20 | // need because it includes almost all "standard" wxWindows headers | |
21 | #ifndef WX_PRECOMP | |
67547666 | 22 | #include "wx/wx.h" |
5526e819 VS |
23 | #endif |
24 | ||
25 | ||
67547666 | 26 | #include "wx/html/htmlwin.h" |
5526e819 VS |
27 | |
28 | ||
29 | // new handler class: | |
30 | ||
67547666 GD |
31 | #include "wx/wfstream.h" |
32 | #include "wx/mstream.h" | |
5526e819 VS |
33 | |
34 | ||
35 | ||
36 | class MyVFS : public wxFileSystemHandler | |
37 | { | |
38 | public: | |
39 | MyVFS() : wxFileSystemHandler() {} | |
40 | ||
41 | wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location); | |
42 | bool CanOpen(const wxString& location); | |
43 | }; | |
44 | ||
45 | ||
46 | bool MyVFS::CanOpen(const wxString& location) | |
47 | { | |
48 | return (GetProtocol(location) == "myVFS"); | |
49 | } | |
50 | ||
51 | ||
52 | ||
53 | wxFSFile* MyVFS::OpenFile(wxFileSystem& fs, const wxString& location) | |
54 | { | |
55 | wxFSFile *f; | |
56 | wxInputStream *str; | |
2bc524f3 | 57 | static char buf[1024]; |
5526e819 VS |
58 | |
59 | sprintf(buf, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>" | |
60 | "Where do you want to go?<br><blockquote>" | |
61 | "<a href=\"%s-1\">sub-1</a><br>" | |
62 | "<a href=\"%s-2\">sub-2</a><br>" | |
63 | "<a href=\"%s-3\">sub-3</a><br>" | |
64 | "</blockquote></body></html>", | |
65 | location.GetData(), location.GetData(), location.GetData(), location.GetData()); | |
7e1e0960 | 66 | |
2bc524f3 VS |
67 | // NB: There's a terrible hack involved: we fill 'buf' with new data every |
68 | // time this method is called and return new wxMemoryInputStream pointing to it. | |
69 | // This won't work as soon as there are 2+ myVFS files opened. Fortunately, | |
70 | // this won't happen because wxHTML keeps only one "page" file opened at the | |
71 | // time. | |
5526e819 | 72 | str = new wxMemoryInputStream(buf, strlen(buf)); |
3592c01e | 73 | f = new wxFSFile(str, location, "text/html", wxEmptyString, wxDateTime::Today()); |
2bc524f3 | 74 | |
5526e819 VS |
75 | return f; |
76 | } | |
77 | ||
78 | ||
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // private classes | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | // Define a new application type, each program should derive a class from wxApp | |
85 | class MyApp : public wxApp | |
86 | { | |
87 | public: | |
88 | // override base class virtuals | |
89 | // ---------------------------- | |
90 | ||
91 | // this one is called on application startup and is a good place for the app | |
92 | // initialization (doing it here and not in the ctor allows to have an error | |
93 | // return: if OnInit() returns false, the application terminates) | |
94 | virtual bool OnInit(); | |
95 | }; | |
96 | ||
97 | // Define a new frame type: this is going to be our main frame | |
98 | class MyFrame : public wxFrame | |
99 | { | |
100 | public: | |
101 | // ctor(s) | |
102 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
103 | ||
104 | // event handlers (these functions should _not_ be virtual) | |
105 | void OnQuit(wxCommandEvent& event); | |
5526e819 VS |
106 | void OnBack(wxCommandEvent& event); |
107 | void OnForward(wxCommandEvent& event); | |
108 | ||
109 | private: | |
110 | // any class wishing to process wxWindows events must use this macro | |
111 | DECLARE_EVENT_TABLE() | |
112 | }; | |
113 | ||
114 | // ---------------------------------------------------------------------------- | |
115 | // constants | |
116 | // ---------------------------------------------------------------------------- | |
117 | ||
118 | // IDs for the controls and the menu commands | |
119 | enum | |
120 | { | |
121 | // menu items | |
122 | Minimal_Quit = 1, | |
5526e819 VS |
123 | Minimal_Back, |
124 | Minimal_Forward, | |
125 | ||
126 | // controls start here (the numbers are, of course, arbitrary) | |
127 | Minimal_Text = 1000, | |
128 | }; | |
129 | ||
130 | // ---------------------------------------------------------------------------- | |
131 | // event tables and other macros for wxWindows | |
132 | // ---------------------------------------------------------------------------- | |
133 | ||
134 | // the event tables connect the wxWindows events with the functions (event | |
135 | // handlers) which process them. It can be also done at run-time, but for the | |
136 | // simple menu events like this the static method is much simpler. | |
137 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
138 | EVT_MENU(Minimal_Quit, MyFrame::OnQuit) | |
5526e819 VS |
139 | EVT_MENU(Minimal_Back, MyFrame::OnBack) |
140 | EVT_MENU(Minimal_Forward, MyFrame::OnForward) | |
141 | END_EVENT_TABLE() | |
142 | ||
143 | // Create a new application object: this macro will allow wxWindows to create | |
144 | // the application object during program execution (it's better than using a | |
145 | // static object for many reasons) and also declares the accessor function | |
146 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
147 | // not wxApp) | |
148 | IMPLEMENT_APP(MyApp) | |
149 | ||
150 | // ============================================================================ | |
151 | // implementation | |
152 | // ============================================================================ | |
153 | ||
154 | // ---------------------------------------------------------------------------- | |
155 | // the application class | |
156 | // ---------------------------------------------------------------------------- | |
157 | ||
158 | // `Main program' equivalent: the program execution "starts" here | |
159 | bool MyApp::OnInit() | |
160 | { | |
161 | // Create the main application window | |
162 | MyFrame *frame = new MyFrame("wxHtmlWindow testing application", | |
163 | wxPoint(50, 50), wxSize(640, 480)); | |
aec18ff7 | 164 | |
5526e819 VS |
165 | // Show it and tell the application that it's our main window |
166 | // @@@ what does it do exactly, in fact? is it necessary here? | |
167 | frame->Show(TRUE); | |
168 | SetTopWindow(frame); | |
169 | wxFileSystem::AddHandler(new MyVFS); | |
aec18ff7 | 170 | |
5526e819 VS |
171 | // success: wxApp::OnRun() will be called which will enter the main message |
172 | // loop and the application will run. If we returned FALSE here, the | |
173 | // application would exit immediately. | |
174 | return TRUE; | |
175 | } | |
176 | ||
177 | // ---------------------------------------------------------------------------- | |
178 | // main frame | |
179 | // ---------------------------------------------------------------------------- | |
180 | ||
181 | wxHtmlWindow *html; | |
182 | ||
183 | // frame constructor | |
184 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
185 | : wxFrame((wxFrame *)NULL, -1, title, pos, size) | |
186 | { | |
187 | // create a menu bar | |
188 | wxMenu *menuFile = new wxMenu; | |
189 | wxMenu *menuNav = new wxMenu; | |
190 | ||
191 | menuFile->Append(Minimal_Quit, "E&xit"); | |
192 | menuNav->Append(Minimal_Back, "Go &BACK"); | |
193 | menuNav->Append(Minimal_Forward, "Go &FORWARD"); | |
194 | ||
195 | // now append the freshly created menu to the menu bar... | |
196 | wxMenuBar *menuBar = new wxMenuBar; | |
197 | menuBar->Append(menuFile, "&File"); | |
198 | menuBar->Append(menuNav, "&Navigate"); | |
199 | ||
200 | // ... and attach this menu bar to the frame | |
201 | SetMenuBar(menuBar); | |
202 | ||
9eeb92ac | 203 | CreateStatusBar(2); |
5526e819 VS |
204 | |
205 | html = new wxHtmlWindow(this); | |
206 | html -> SetRelatedFrame(this, "VFS Demo: '%s'"); | |
207 | html -> SetRelatedStatusBar(1); | |
208 | html -> LoadPage("start.htm"); | |
209 | } | |
210 | ||
211 | ||
212 | // event handlers | |
213 | ||
214 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
215 | { | |
216 | // TRUE is to force the frame to close | |
217 | Close(TRUE); | |
218 | } | |
219 | ||
5526e819 VS |
220 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) |
221 | { | |
222 | if (!html -> HistoryBack()) wxMessageBox("You reached prehistory era!"); | |
223 | } | |
224 | ||
225 | ||
226 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
227 | { | |
228 | if (!html -> HistoryForward()) wxMessageBox("No more items in history!"); | |
229 | } |