]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml sample: demonstrates virtual file systems feature
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWidgets headers
25 #include "wx/html/htmlwin.h"
28 #include "../../sample.xpm"
33 #include "wx/wfstream.h"
34 #include "wx/mstream.h"
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 class MyVFS
: public wxFileSystemHandler
44 MyVFS() : wxFileSystemHandler() {}
46 wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
47 bool CanOpen(const wxString
& location
);
50 bool MyVFS::CanOpen(const wxString
& location
)
52 return (GetProtocol(location
) == wxT("myVFS"));
55 wxFSFile
* MyVFS::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
59 static char buf
[1024];
60 const wxWX2MBbuf loc
= location
.ToAscii();
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
,
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
76 str
= new wxMemoryInputStream(buf
, strlen(buf
));
77 f
= new wxFSFile(str
, location
, wxT("text/html"), wxEmptyString
, wxDateTime::Today());
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // Define a new application type, each program should derive a class from wxApp
89 class MyApp
: public wxApp
92 // override base class virtuals
93 // ----------------------------
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();
101 // Define a new frame type: this is going to be our main frame
102 class MyFrame
: public wxFrame
106 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
108 // event handlers (these functions should _not_ be virtual)
109 void OnQuit(wxCommandEvent
& event
);
110 void OnBack(wxCommandEvent
& event
);
111 void OnForward(wxCommandEvent
& event
);
114 // any class wishing to process wxWidgets events must use this macro
115 DECLARE_EVENT_TABLE()
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // IDs for the controls and the menu commands
130 // controls start here (the numbers are, of course, arbitrary)
134 // ----------------------------------------------------------------------------
135 // event tables and other macros for wxWidgets
136 // ----------------------------------------------------------------------------
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
)
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
154 // ============================================================================
156 // ============================================================================
158 // ----------------------------------------------------------------------------
159 // the application class
160 // ----------------------------------------------------------------------------
162 // `Main program' equivalent: the program execution "starts" here
165 if ( !wxApp::OnInit() )
168 // Create the main application window
169 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
170 wxDefaultPosition
, wxSize(640, 480));
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?
176 wxFileSystem::AddHandler(new MyVFS
);
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.
184 // ----------------------------------------------------------------------------
186 // ----------------------------------------------------------------------------
191 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
192 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
194 SetIcon(wxICON(sample
));
197 wxMenu
*menuFile
= new wxMenu
;
198 wxMenu
*menuNav
= new wxMenu
;
200 menuFile
->Append(Minimal_Quit
, _("E&xit"));
201 menuNav
->Append(Minimal_Back
, _("Go &BACK"));
202 menuNav
->Append(Minimal_Forward
, _("Go &FORWARD"));
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"));
209 // ... and attach this menu bar to the frame
214 #endif // wxUSE_STATUSBAR
216 html
= new wxHtmlWindow(this);
217 html
-> SetRelatedFrame(this, _("VFS Demo: '%s'"));
219 html
-> SetRelatedStatusBar(1);
220 #endif // wxUSE_STATUSBAR
221 html
-> LoadPage(wxT("start.htm"));
227 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
229 // true is to force the frame to close
233 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
235 if (!html
-> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
238 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
240 if (!html
-> HistoryForward()) wxMessageBox(_("No more items in history!"));