]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml testing example
4 // demonstrates virtual file systems feature
5 /////////////////////////////////////////////////////////////////////////////
8 #pragma implementation "test.cpp"
9 #pragma interface "test.cpp"
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" wxWindows headers
26 #include "wx/html/htmlwin.h"
31 #include "wx/wfstream.h"
32 #include "wx/mstream.h"
36 class MyVFS
: public wxFileSystemHandler
39 MyVFS() : wxFileSystemHandler() {}
41 wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
42 bool CanOpen(const wxString
& location
);
46 bool MyVFS::CanOpen(const wxString
& location
)
48 return (GetProtocol(location
) == "myVFS");
53 wxFSFile
* MyVFS::OpenFile(wxFileSystem
& fs
, const wxString
& location
)
57 static char buf
[1024];
59 sprintf(buf
, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>"
60 "Where do you want to go?<br><blockquote>"
61 "<a href=\"%s-1\">sub-1</a><br>"
62 "<a href=\"%s-2\">sub-2</a><br>"
63 "<a href=\"%s-3\">sub-3</a><br>"
64 "</blockquote></body></html>",
65 location
.GetData(), location
.GetData(), location
.GetData(), location
.GetData());
67 // NB: There's a terrible hack involved: we fill 'buf' with new data every
68 // time this method is called and return new wxMemoryInputStream pointing to it.
69 // This won't work as soon as there are 2+ myVFS files opened. Fortunately,
70 // this won't happen because wxHTML keeps only one "page" file opened at the
72 str
= new wxMemoryInputStream(buf
, strlen(buf
));
73 f
= new wxFSFile(str
, location
, "text/html", wxEmptyString
, wxDateTime::Today());
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 // Define a new application type, each program should derive a class from wxApp
85 class MyApp
: public wxApp
88 // override base class virtuals
89 // ----------------------------
91 // this one is called on application startup and is a good place for the app
92 // initialization (doing it here and not in the ctor allows to have an error
93 // return: if OnInit() returns false, the application terminates)
94 virtual bool OnInit();
97 // Define a new frame type: this is going to be our main frame
98 class MyFrame
: public wxFrame
102 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
104 // event handlers (these functions should _not_ be virtual)
105 void OnQuit(wxCommandEvent
& event
);
106 void OnAbout(wxCommandEvent
& event
);
107 void OnBack(wxCommandEvent
& event
);
108 void OnForward(wxCommandEvent
& event
);
111 // any class wishing to process wxWindows events must use this macro
112 DECLARE_EVENT_TABLE()
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // IDs for the controls and the menu commands
128 // controls start here (the numbers are, of course, arbitrary)
132 // ----------------------------------------------------------------------------
133 // event tables and other macros for wxWindows
134 // ----------------------------------------------------------------------------
136 // the event tables connect the wxWindows events with the functions (event
137 // handlers) which process them. It can be also done at run-time, but for the
138 // simple menu events like this the static method is much simpler.
139 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
140 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
141 EVT_MENU(Minimal_About
, MyFrame::OnAbout
)
142 EVT_MENU(Minimal_Back
, MyFrame::OnBack
)
143 EVT_MENU(Minimal_Forward
, MyFrame::OnForward
)
146 // Create a new application object: this macro will allow wxWindows 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
153 // ============================================================================
155 // ============================================================================
157 // ----------------------------------------------------------------------------
158 // the application class
159 // ----------------------------------------------------------------------------
161 // `Main program' equivalent: the program execution "starts" here
164 // Create the main application window
165 MyFrame
*frame
= new MyFrame("wxHtmlWindow testing application",
166 wxPoint(50, 50), wxSize(640, 480));
168 // Show it and tell the application that it's our main window
169 // @@@ what does it do exactly, in fact? is it necessary here?
172 wxFileSystem::AddHandler(new MyVFS
);
174 // success: wxApp::OnRun() will be called which will enter the main message
175 // loop and the application will run. If we returned FALSE here, the
176 // application would exit immediately.
180 // ----------------------------------------------------------------------------
182 // ----------------------------------------------------------------------------
187 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
188 : wxFrame((wxFrame
*)NULL
, -1, title
, pos
, size
)
191 wxMenu
*menuFile
= new wxMenu
;
192 wxMenu
*menuNav
= new wxMenu
;
194 menuFile
->Append(Minimal_Quit
, "E&xit");
195 menuNav
->Append(Minimal_Back
, "Go &BACK");
196 menuNav
->Append(Minimal_Forward
, "Go &FORWARD");
198 // now append the freshly created menu to the menu bar...
199 wxMenuBar
*menuBar
= new wxMenuBar
;
200 menuBar
->Append(menuFile
, "&File");
201 menuBar
->Append(menuNav
, "&Navigate");
203 // ... and attach this menu bar to the frame
208 html
= new wxHtmlWindow(this);
209 html
-> SetRelatedFrame(this, "VFS Demo: '%s'");
210 html
-> SetRelatedStatusBar(1);
211 html
-> LoadPage("start.htm");
217 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
219 // TRUE is to force the frame to close
223 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
229 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
231 if (!html
-> HistoryBack()) wxMessageBox("You reached prehistory era!");
235 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
237 if (!html
-> HistoryForward()) wxMessageBox("No more items in history!");