]> git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
Corrections in light of recent toolbar and other changes
[wxWidgets.git] / samples / html / virtual / virtual.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: virtua;.cpp
3 // Purpose: wxHtml testing example
4 // demonstrates virtual file systems feature
5 /////////////////////////////////////////////////////////////////////////////
6
7 #ifdef __GNUG__
8 #pragma implementation "test.cpp"
9 #pragma interface "test.cpp"
10 #endif
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 // for all others, include the necessary headers (this file is usually all you
20 // need because it includes almost all "standard" wxWindows headers
21 #ifndef WX_PRECOMP
22 #include "wx/wx.h"
23 #endif
24
25
26 #include "wx/html/htmlwin.h"
27
28
29 // new handler class:
30
31 #include "wx/wfstream.h"
32 #include "wx/mstream.h"
33
34
35
36 class MyVFS : public wxFileSystemHandler
37 {
38 public:
39 MyVFS() : wxFileSystemHandler() {}
40
41 wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
42 bool CanOpen(const wxString& location);
43 };
44
45
46 bool MyVFS::CanOpen(const wxString& location)
47 {
48 return (GetProtocol(location) == "myVFS");
49 }
50
51
52
53 wxFSFile* MyVFS::OpenFile(wxFileSystem& fs, const wxString& location)
54 {
55 wxFSFile *f;
56 wxInputStream *str;
57 char *buf = (char*)malloc(1024);
58
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());
66
67 // WARNING: wxMemoryInputStream will not free buf.
68 // There is a memory leak here.
69 str = new wxMemoryInputStream(buf, strlen(buf));
70 f = new wxFSFile(str, location, "text/html", wxEmptyString, wxDateTime::Today());
71 return f;
72 }
73
74
75
76 // ----------------------------------------------------------------------------
77 // private classes
78 // ----------------------------------------------------------------------------
79
80 // Define a new application type, each program should derive a class from wxApp
81 class MyApp : public wxApp
82 {
83 public:
84 // override base class virtuals
85 // ----------------------------
86
87 // this one is called on application startup and is a good place for the app
88 // initialization (doing it here and not in the ctor allows to have an error
89 // return: if OnInit() returns false, the application terminates)
90 virtual bool OnInit();
91 };
92
93 // Define a new frame type: this is going to be our main frame
94 class MyFrame : public wxFrame
95 {
96 public:
97 // ctor(s)
98 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
99
100 // event handlers (these functions should _not_ be virtual)
101 void OnQuit(wxCommandEvent& event);
102 void OnAbout(wxCommandEvent& event);
103 void OnBack(wxCommandEvent& event);
104 void OnForward(wxCommandEvent& event);
105
106 private:
107 // any class wishing to process wxWindows events must use this macro
108 DECLARE_EVENT_TABLE()
109 };
110
111 // ----------------------------------------------------------------------------
112 // constants
113 // ----------------------------------------------------------------------------
114
115 // IDs for the controls and the menu commands
116 enum
117 {
118 // menu items
119 Minimal_Quit = 1,
120 Minimal_About,
121 Minimal_Back,
122 Minimal_Forward,
123
124 // controls start here (the numbers are, of course, arbitrary)
125 Minimal_Text = 1000,
126 };
127
128 // ----------------------------------------------------------------------------
129 // event tables and other macros for wxWindows
130 // ----------------------------------------------------------------------------
131
132 // the event tables connect the wxWindows events with the functions (event
133 // handlers) which process them. It can be also done at run-time, but for the
134 // simple menu events like this the static method is much simpler.
135 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
136 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
137 EVT_MENU(Minimal_About, MyFrame::OnAbout)
138 EVT_MENU(Minimal_Back, MyFrame::OnBack)
139 EVT_MENU(Minimal_Forward, MyFrame::OnForward)
140 END_EVENT_TABLE()
141
142 // Create a new application object: this macro will allow wxWindows to create
143 // the application object during program execution (it's better than using a
144 // static object for many reasons) and also declares the accessor function
145 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
146 // not wxApp)
147 IMPLEMENT_APP(MyApp)
148
149 // ============================================================================
150 // implementation
151 // ============================================================================
152
153 // ----------------------------------------------------------------------------
154 // the application class
155 // ----------------------------------------------------------------------------
156
157 // `Main program' equivalent: the program execution "starts" here
158 bool MyApp::OnInit()
159 {
160 // Create the main application window
161 MyFrame *frame = new MyFrame("wxHtmlWindow testing application",
162 wxPoint(50, 50), wxSize(640, 480));
163
164 // Show it and tell the application that it's our main window
165 // @@@ what does it do exactly, in fact? is it necessary here?
166 frame->Show(TRUE);
167 SetTopWindow(frame);
168 wxFileSystem::AddHandler(new MyVFS);
169
170 // success: wxApp::OnRun() will be called which will enter the main message
171 // loop and the application will run. If we returned FALSE here, the
172 // application would exit immediately.
173 return TRUE;
174 }
175
176 // ----------------------------------------------------------------------------
177 // main frame
178 // ----------------------------------------------------------------------------
179
180 wxHtmlWindow *html;
181
182 // frame constructor
183 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
184 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
185 {
186 // create a menu bar
187 wxMenu *menuFile = new wxMenu;
188 wxMenu *menuNav = new wxMenu;
189
190 menuFile->Append(Minimal_Quit, "E&xit");
191 menuNav->Append(Minimal_Back, "Go &BACK");
192 menuNav->Append(Minimal_Forward, "Go &FORWARD");
193
194 // now append the freshly created menu to the menu bar...
195 wxMenuBar *menuBar = new wxMenuBar;
196 menuBar->Append(menuFile, "&File");
197 menuBar->Append(menuNav, "&Navigate");
198
199 // ... and attach this menu bar to the frame
200 SetMenuBar(menuBar);
201
202 CreateStatusBar(2);
203
204 html = new wxHtmlWindow(this);
205 html -> SetRelatedFrame(this, "VFS Demo: '%s'");
206 html -> SetRelatedStatusBar(1);
207 html -> LoadPage("start.htm");
208 }
209
210
211 // event handlers
212
213 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
214 {
215 // TRUE is to force the frame to close
216 Close(TRUE);
217 }
218
219 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
220 {
221 }
222
223
224
225 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
226 {
227 if (!html -> HistoryBack()) wxMessageBox("You reached prehistory era!");
228 }
229
230
231 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
232 {
233 if (!html -> HistoryForward()) wxMessageBox("No more items in history!");
234 }