]> git.saurik.com Git - wxWidgets.git/blame - samples/html/virtual/virtual.cpp
Don't use GetThreadId() in wxMSW code.
[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
e7092398 27#ifndef wxHAS_IMAGES_IN_RESOURCES
197ab43d
FM
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
18f42b94 172 // Show it
197ab43d 173 frame->Show(true);
18f42b94 174
197ab43d 175 wxFileSystem::AddHandler(new MyVFS);
aec18ff7 176
5526e819 177 // success: wxApp::OnRun() will be called which will enter the main message
348469c2 178 // loop and the application will run. If we returned false here, the
5526e819 179 // application would exit immediately.
197ab43d
FM
180 return true;
181}
5526e819
VS
182
183// ----------------------------------------------------------------------------
184// main frame
185// ----------------------------------------------------------------------------
186
187wxHtmlWindow *html;
188
189// frame constructor
197ab43d
FM
190MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
191 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
192{
193 SetIcon(wxICON(sample));
194
5526e819 195 // create a menu bar
197ab43d
FM
196 wxMenu *menuFile = new wxMenu;
197 wxMenu *menuNav = new wxMenu;
5526e819 198
197ab43d
FM
199 menuFile->Append(Minimal_Quit, _("E&xit"));
200 menuNav->Append(Minimal_Back, _("Go &BACK"));
201 menuNav->Append(Minimal_Forward, _("Go &FORWARD"));
5526e819
VS
202
203 // now append the freshly created menu to the menu bar...
197ab43d
FM
204 wxMenuBar *menuBar = new wxMenuBar;
205 menuBar->Append(menuFile, _("&File"));
206 menuBar->Append(menuNav, _("&Navigate"));
5526e819
VS
207
208 // ... and attach this menu bar to the frame
197ab43d
FM
209 SetMenuBar(menuBar);
210
8520f137 211#if wxUSE_STATUSBAR
197ab43d 212 CreateStatusBar(2);
8520f137 213#endif // wxUSE_STATUSBAR
5526e819 214
197ab43d
FM
215 html = new wxHtmlWindow(this);
216 html -> SetRelatedFrame(this, _("VFS Demo: '%s'"));
8520f137 217#if wxUSE_STATUSBAR
197ab43d 218 html -> SetRelatedStatusBar(1);
8520f137 219#endif // wxUSE_STATUSBAR
197ab43d
FM
220 html -> LoadPage(wxT("start.htm"));
221}
5526e819
VS
222
223
224// event handlers
225
197ab43d
FM
226void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
227{
348469c2 228 // true is to force the frame to close
197ab43d
FM
229 Close(true);
230}
5526e819 231
197ab43d
FM
232void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
233{
234 if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
235}
5526e819 236
197ab43d
FM
237void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
238{
239 if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!"));
240}