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