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