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