]> git.saurik.com Git - wxWidgets.git/blame - samples/html/virtual/virtual.cpp
Use template class instead of template function in wxVectorSort().
[wxWidgets.git] / samples / html / virtual / virtual.cpp
CommitLineData
5526e819 1/////////////////////////////////////////////////////////////////////////////
197ab43d
FM
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
5526e819
VS
10/////////////////////////////////////////////////////////////////////////////
11
5526e819 12// For compilers that support precompilation, includes "wx/wx.h".
92a19c2e 13#include "wx/wxprec.h"
5526e819
VS
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19// for all others, include the necessary headers (this file is usually all you
be5a51fb 20// need because it includes almost all "standard" wxWidgets headers
5526e819 21#ifndef WX_PRECOMP
67547666 22 #include "wx/wx.h"
5526e819
VS
23#endif
24
67547666 25#include "wx/html/htmlwin.h"
5526e819 26
197ab43d
FM
27#ifndef __WXMSW__
28 #include "../../sample.xpm"
29#endif
5526e819
VS
30
31// new handler class:
32
67547666
GD
33#include "wx/wfstream.h"
34#include "wx/mstream.h"
5526e819
VS
35
36
197ab43d
FM
37// ----------------------------------------------------------------------------
38// MyVFS
39// ----------------------------------------------------------------------------
5526e819
VS
40
41class MyVFS : public wxFileSystemHandler
42{
43public:
44 MyVFS() : wxFileSystemHandler() {}
45
46 wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
47 bool CanOpen(const wxString& location);
48};
49
5526e819
VS
50bool MyVFS::CanOpen(const wxString& location)
51{
2b5f62a0 52 return (GetProtocol(location) == wxT("myVFS"));
5526e819
VS
53}
54
87728739 55wxFSFile* MyVFS::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
5526e819
VS
56{
57 wxFSFile *f;
58 wxInputStream *str;
2bc524f3 59 static char buf[1024];
2b5f62a0 60 const wxWX2MBbuf loc = location.ToAscii();
5526e819
VS
61
62 sprintf(buf, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>"
197ab43d
FM
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);
7e1e0960 70
2bc524f3
VS
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.
5526e819 76 str = new wxMemoryInputStream(buf, strlen(buf));
2b5f62a0 77 f = new wxFSFile(str, location, wxT("text/html"), wxEmptyString, wxDateTime::Today());
197ab43d 78
5526e819
VS
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
197ab43d
FM
89class MyApp : public wxApp
90{
91public:
5526e819
VS
92 // override base class virtuals
93 // ----------------------------
197ab43d 94
5526e819
VS
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)
197ab43d
FM
98 virtual bool OnInit();
99};
5526e819
VS
100
101// Define a new frame type: this is going to be our main frame
197ab43d
FM
102class MyFrame : public wxFrame
103{
104public:
5526e819 105 // ctor(s)
197ab43d
FM
106 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
107
5526e819 108 // event handlers (these functions should _not_ be virtual)
197ab43d
FM
109 void OnQuit(wxCommandEvent& event);
110 void OnBack(wxCommandEvent& event);
111 void OnForward(wxCommandEvent& event);
5526e819 112
197ab43d 113private:
be5a51fb 114 // any class wishing to process wxWidgets events must use this macro
5526e819 115 DECLARE_EVENT_TABLE()
197ab43d 116};
5526e819
VS
117
118// ----------------------------------------------------------------------------
119// constants
120// ----------------------------------------------------------------------------
121
122// IDs for the controls and the menu commands
197ab43d
FM
123enum
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};
5526e819
VS
133
134// ----------------------------------------------------------------------------
be5a51fb 135// event tables and other macros for wxWidgets
5526e819
VS
136// ----------------------------------------------------------------------------
137
be5a51fb 138// the event tables connect the wxWidgets events with the functions (event
5526e819
VS
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.
197ab43d
FM
141BEGIN_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)
145END_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)
152IMPLEMENT_APP(MyApp)
153
154// ============================================================================
155// implementation
156// ============================================================================
157
158// ----------------------------------------------------------------------------
159// the application class
160// ----------------------------------------------------------------------------
161
162// `Main program' equivalent: the program execution "starts" here
163bool MyApp::OnInit()
164{
165 if ( !wxApp::OnInit() )
166 return false;
45e6e6f8 167
5526e819 168 // Create the main application window
197ab43d
FM
169 MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
170 wxDefaultPosition, wxSize(640, 480));
aec18ff7 171
5526e819
VS
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?
197ab43d
FM
174 frame->Show(true);
175 SetTopWindow(frame);
176 wxFileSystem::AddHandler(new MyVFS);
aec18ff7 177
5526e819 178 // success: wxApp::OnRun() will be called which will enter the main message
348469c2 179 // loop and the application will run. If we returned false here, the
5526e819 180 // application would exit immediately.
197ab43d
FM
181 return true;
182}
5526e819
VS
183
184// ----------------------------------------------------------------------------
185// main frame
186// ----------------------------------------------------------------------------
187
188wxHtmlWindow *html;
189
190// frame constructor
197ab43d
FM
191MyFrame::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
5526e819 196 // create a menu bar
197ab43d
FM
197 wxMenu *menuFile = new wxMenu;
198 wxMenu *menuNav = new wxMenu;
5526e819 199
197ab43d
FM
200 menuFile->Append(Minimal_Quit, _("E&xit"));
201 menuNav->Append(Minimal_Back, _("Go &BACK"));
202 menuNav->Append(Minimal_Forward, _("Go &FORWARD"));
5526e819
VS
203
204 // now append the freshly created menu to the menu bar...
197ab43d
FM
205 wxMenuBar *menuBar = new wxMenuBar;
206 menuBar->Append(menuFile, _("&File"));
207 menuBar->Append(menuNav, _("&Navigate"));
5526e819
VS
208
209 // ... and attach this menu bar to the frame
197ab43d
FM
210 SetMenuBar(menuBar);
211
8520f137 212#if wxUSE_STATUSBAR
197ab43d 213 CreateStatusBar(2);
8520f137 214#endif // wxUSE_STATUSBAR
5526e819 215
197ab43d
FM
216 html = new wxHtmlWindow(this);
217 html -> SetRelatedFrame(this, _("VFS Demo: '%s'"));
8520f137 218#if wxUSE_STATUSBAR
197ab43d 219 html -> SetRelatedStatusBar(1);
8520f137 220#endif // wxUSE_STATUSBAR
197ab43d
FM
221 html -> LoadPage(wxT("start.htm"));
222}
5526e819
VS
223
224
225// event handlers
226
197ab43d
FM
227void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
228{
348469c2 229 // true is to force the frame to close
197ab43d
FM
230 Close(true);
231}
5526e819 232
197ab43d
FM
233void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
234{
235 if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
236}
5526e819 237
197ab43d
FM
238void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
239{
240 if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!"));
241}