]>
git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxHtml sample: demonstrates virtual file systems feature
7 // Copyright: (c) wxWidgets team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
18 // for all others, include the necessary headers (this file is usually all you
19 // need because it includes almost all "standard" wxWidgets headers
24 #include "wx/html/htmlwin.h"
26 #ifndef wxHAS_IMAGES_IN_RESOURCES
27 #include "../../sample.xpm"
32 #include "wx/wfstream.h"
33 #include "wx/mstream.h"
36 // ----------------------------------------------------------------------------
38 // ----------------------------------------------------------------------------
40 class MyVFS
: public wxFileSystemHandler
43 MyVFS() : wxFileSystemHandler() {}
45 wxFSFile
* OpenFile(wxFileSystem
& fs
, const wxString
& location
);
46 bool CanOpen(const wxString
& location
);
49 bool MyVFS::CanOpen(const wxString
& location
)
51 return (GetProtocol(location
) == wxT("myVFS"));
54 wxFSFile
* MyVFS::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
58 static char buf
[1024];
59 const wxWX2MBbuf loc
= location
.ToAscii();
61 sprintf(buf
, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>"
62 "Where do you want to go?<br><blockquote>"
63 "<a href=\"%s-1\">sub-1</a><br>"
64 "<a href=\"%s-2\">sub-2</a><br>"
65 "<a href=\"%s-3\">sub-3</a><br>"
66 "</blockquote></body></html>",
67 (const char*)loc
, (const char*)loc
, (const char*)loc
,
70 // NB: There's a terrible hack involved: we fill 'buf' with new data every
71 // time this method is called and return new wxMemoryInputStream pointing to it.
72 // This won't work as soon as there are 2+ myVFS files opened. Fortunately,
73 // this won't happen because wxHTML keeps only one "page" file opened at the
75 str
= new wxMemoryInputStream(buf
, strlen(buf
));
76 f
= new wxFSFile(str
, location
, wxT("text/html"), wxEmptyString
, wxDateTime::Today());
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 // Define a new application type, each program should derive a class from wxApp
88 class MyApp
: public wxApp
91 // override base class virtuals
92 // ----------------------------
94 // this one is called on application startup and is a good place for the app
95 // initialization (doing it here and not in the ctor allows to have an error
96 // return: if OnInit() returns false, the application terminates)
97 virtual bool OnInit();
100 // Define a new frame type: this is going to be our main frame
101 class MyFrame
: public wxFrame
105 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
107 // event handlers (these functions should _not_ be virtual)
108 void OnQuit(wxCommandEvent
& event
);
109 void OnBack(wxCommandEvent
& event
);
110 void OnForward(wxCommandEvent
& event
);
113 // any class wishing to process wxWidgets events must use this macro
114 DECLARE_EVENT_TABLE()
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 // IDs for the controls and the menu commands
129 // controls start here (the numbers are, of course, arbitrary)
133 // ----------------------------------------------------------------------------
134 // event tables and other macros for wxWidgets
135 // ----------------------------------------------------------------------------
137 // the event tables connect the wxWidgets events with the functions (event
138 // handlers) which process them. It can be also done at run-time, but for the
139 // simple menu events like this the static method is much simpler.
140 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
141 EVT_MENU(Minimal_Quit
, MyFrame::OnQuit
)
142 EVT_MENU(Minimal_Back
, MyFrame::OnBack
)
143 EVT_MENU(Minimal_Forward
, MyFrame::OnForward
)
146 // Create a new application object: this macro will allow wxWidgets 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 if ( !wxApp::OnInit() )
167 // Create the main application window
168 MyFrame
*frame
= new MyFrame(_("wxHtmlWindow testing application"),
169 wxDefaultPosition
, wxSize(640, 480));
174 wxFileSystem::AddHandler(new MyVFS
);
176 // success: wxApp::OnRun() will be called which will enter the main message
177 // loop and the application will run. If we returned false here, the
178 // application would exit immediately.
182 // ----------------------------------------------------------------------------
184 // ----------------------------------------------------------------------------
189 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
190 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
192 SetIcon(wxICON(sample
));
195 wxMenu
*menuFile
= new wxMenu
;
196 wxMenu
*menuNav
= new wxMenu
;
198 menuFile
->Append(Minimal_Quit
, _("E&xit"));
199 menuNav
->Append(Minimal_Back
, _("Go &BACK"));
200 menuNav
->Append(Minimal_Forward
, _("Go &FORWARD"));
202 // now append the freshly created menu to the menu bar...
203 wxMenuBar
*menuBar
= new wxMenuBar
;
204 menuBar
->Append(menuFile
, _("&File"));
205 menuBar
->Append(menuNav
, _("&Navigate"));
207 // ... and attach this menu bar to the frame
212 #endif // wxUSE_STATUSBAR
214 html
= new wxHtmlWindow(this);
215 html
-> SetRelatedFrame(this, _("VFS Demo: '%s'"));
217 html
-> SetRelatedStatusBar(1);
218 #endif // wxUSE_STATUSBAR
219 html
-> LoadPage(wxT("start.htm"));
225 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
227 // true is to force the frame to close
231 void MyFrame::OnBack(wxCommandEvent
& WXUNUSED(event
))
233 if (!html
-> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
236 void MyFrame::OnForward(wxCommandEvent
& WXUNUSED(event
))
238 if (!html
-> HistoryForward()) wxMessageBox(_("No more items in history!"));