]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: virtual.cpp | |
3 | // Purpose: wxHtml sample: demonstrates virtual file systems feature | |
4 | // Author: ? | |
5 | // Modified by: | |
6 | // Created: ? | |
7 | // Copyright: (c) wxWidgets team | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
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" wxWidgets headers | |
20 | #ifndef WX_PRECOMP | |
21 | #include "wx/wx.h" | |
22 | #endif | |
23 | ||
24 | #include "wx/html/htmlwin.h" | |
25 | ||
26 | #ifndef wxHAS_IMAGES_IN_RESOURCES | |
27 | #include "../../sample.xpm" | |
28 | #endif | |
29 | ||
30 | // new handler class: | |
31 | ||
32 | #include "wx/wfstream.h" | |
33 | #include "wx/mstream.h" | |
34 | ||
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // MyVFS | |
38 | // ---------------------------------------------------------------------------- | |
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 | ||
49 | bool MyVFS::CanOpen(const wxString& location) | |
50 | { | |
51 | return (GetProtocol(location) == wxT("myVFS")); | |
52 | } | |
53 | ||
54 | wxFSFile* MyVFS::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
55 | { | |
56 | wxFSFile *f; | |
57 | wxInputStream *str; | |
58 | static char buf[1024]; | |
59 | const wxWX2MBbuf loc = location.ToAscii(); | |
60 | ||
61 | sprintf(buf, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>" | |
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); | |
69 | ||
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. | |
75 | str = new wxMemoryInputStream(buf, strlen(buf)); | |
76 | f = new wxFSFile(str, location, wxT("text/html"), wxEmptyString, wxDateTime::Today()); | |
77 | ||
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 | |
88 | class MyApp : public wxApp | |
89 | { | |
90 | public: | |
91 | // override base class virtuals | |
92 | // ---------------------------- | |
93 | ||
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) | |
97 | virtual bool OnInit(); | |
98 | }; | |
99 | ||
100 | // Define a new frame type: this is going to be our main frame | |
101 | class MyFrame : public wxFrame | |
102 | { | |
103 | public: | |
104 | // ctor(s) | |
105 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
106 | ||
107 | // event handlers (these functions should _not_ be virtual) | |
108 | void OnQuit(wxCommandEvent& event); | |
109 | void OnBack(wxCommandEvent& event); | |
110 | void OnForward(wxCommandEvent& event); | |
111 | ||
112 | private: | |
113 | // any class wishing to process wxWidgets events must use this macro | |
114 | DECLARE_EVENT_TABLE() | |
115 | }; | |
116 | ||
117 | // ---------------------------------------------------------------------------- | |
118 | // constants | |
119 | // ---------------------------------------------------------------------------- | |
120 | ||
121 | // IDs for the controls and the menu commands | |
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 | }; | |
132 | ||
133 | // ---------------------------------------------------------------------------- | |
134 | // event tables and other macros for wxWidgets | |
135 | // ---------------------------------------------------------------------------- | |
136 | ||
137 | // the event tables connect the wxWidgets events with the functions (event | |
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. | |
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; | |
166 | ||
167 | // Create the main application window | |
168 | MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"), | |
169 | wxDefaultPosition, wxSize(640, 480)); | |
170 | ||
171 | // Show it | |
172 | frame->Show(true); | |
173 | ||
174 | wxFileSystem::AddHandler(new MyVFS); | |
175 | ||
176 | // success: wxApp::OnRun() will be called which will enter the main message | |
177 | // loop and the application will run. If we returned false here, the | |
178 | // application would exit immediately. | |
179 | return true; | |
180 | } | |
181 | ||
182 | // ---------------------------------------------------------------------------- | |
183 | // main frame | |
184 | // ---------------------------------------------------------------------------- | |
185 | ||
186 | wxHtmlWindow *html; | |
187 | ||
188 | // frame constructor | |
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 | ||
194 | // create a menu bar | |
195 | wxMenu *menuFile = new wxMenu; | |
196 | wxMenu *menuNav = new wxMenu; | |
197 | ||
198 | menuFile->Append(Minimal_Quit, _("E&xit")); | |
199 | menuNav->Append(Minimal_Back, _("Go &BACK")); | |
200 | menuNav->Append(Minimal_Forward, _("Go &FORWARD")); | |
201 | ||
202 | // now append the freshly created menu to the menu bar... | |
203 | wxMenuBar *menuBar = new wxMenuBar; | |
204 | menuBar->Append(menuFile, _("&File")); | |
205 | menuBar->Append(menuNav, _("&Navigate")); | |
206 | ||
207 | // ... and attach this menu bar to the frame | |
208 | SetMenuBar(menuBar); | |
209 | ||
210 | #if wxUSE_STATUSBAR | |
211 | CreateStatusBar(2); | |
212 | #endif // wxUSE_STATUSBAR | |
213 | ||
214 | html = new wxHtmlWindow(this); | |
215 | html -> SetRelatedFrame(this, _("VFS Demo: '%s'")); | |
216 | #if wxUSE_STATUSBAR | |
217 | html -> SetRelatedStatusBar(1); | |
218 | #endif // wxUSE_STATUSBAR | |
219 | html -> LoadPage(wxT("start.htm")); | |
220 | } | |
221 | ||
222 | ||
223 | // event handlers | |
224 | ||
225 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
226 | { | |
227 | // true is to force the frame to close | |
228 | Close(true); | |
229 | } | |
230 | ||
231 | void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event)) | |
232 | { | |
233 | if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!")); | |
234 | } | |
235 | ||
236 | void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event)) | |
237 | { | |
238 | if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!")); | |
239 | } |