]> git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
58310342a60a7751a734a846e75599074e9956a5
[wxWidgets.git] / samples / html / virtual / virtual.cpp
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 and tell the application that it's our main window
173 // @@@ what does it do exactly, in fact? is it necessary here?
174 frame->Show(true);
175 SetTopWindow(frame);
176 wxFileSystem::AddHandler(new MyVFS);
177
178 // success: wxApp::OnRun() will be called which will enter the main message
179 // loop and the application will run. If we returned false here, the
180 // application would exit immediately.
181 return true;
182 }
183
184 // ----------------------------------------------------------------------------
185 // main frame
186 // ----------------------------------------------------------------------------
187
188 wxHtmlWindow *html;
189
190 // frame constructor
191 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
192 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
193 {
194 SetIcon(wxICON(sample));
195
196 // create a menu bar
197 wxMenu *menuFile = new wxMenu;
198 wxMenu *menuNav = new wxMenu;
199
200 menuFile->Append(Minimal_Quit, _("E&xit"));
201 menuNav->Append(Minimal_Back, _("Go &BACK"));
202 menuNav->Append(Minimal_Forward, _("Go &FORWARD"));
203
204 // now append the freshly created menu to the menu bar...
205 wxMenuBar *menuBar = new wxMenuBar;
206 menuBar->Append(menuFile, _("&File"));
207 menuBar->Append(menuNav, _("&Navigate"));
208
209 // ... and attach this menu bar to the frame
210 SetMenuBar(menuBar);
211
212 #if wxUSE_STATUSBAR
213 CreateStatusBar(2);
214 #endif // wxUSE_STATUSBAR
215
216 html = new wxHtmlWindow(this);
217 html -> SetRelatedFrame(this, _("VFS Demo: '%s'"));
218 #if wxUSE_STATUSBAR
219 html -> SetRelatedStatusBar(1);
220 #endif // wxUSE_STATUSBAR
221 html -> LoadPage(wxT("start.htm"));
222 }
223
224
225 // event handlers
226
227 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
228 {
229 // true is to force the frame to close
230 Close(true);
231 }
232
233 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
234 {
235 if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
236 }
237
238 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
239 {
240 if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!"));
241 }