]> git.saurik.com Git - wxWidgets.git/blob - samples/html/virtual/virtual.cpp
more type-safety
[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 str = new wxMemoryInputStream(buf, strlen(buf));
67 f = new wxFSFile(str, location, "text/html", wxEmptyString);
68 return f;
69 }
70
71
72
73 // ----------------------------------------------------------------------------
74 // private classes
75 // ----------------------------------------------------------------------------
76
77 // Define a new application type, each program should derive a class from wxApp
78 class MyApp : public wxApp
79 {
80 public:
81 // override base class virtuals
82 // ----------------------------
83
84 // this one is called on application startup and is a good place for the app
85 // initialization (doing it here and not in the ctor allows to have an error
86 // return: if OnInit() returns false, the application terminates)
87 virtual bool OnInit();
88 };
89
90 // Define a new frame type: this is going to be our main frame
91 class MyFrame : public wxFrame
92 {
93 public:
94 // ctor(s)
95 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
96
97 // event handlers (these functions should _not_ be virtual)
98 void OnQuit(wxCommandEvent& event);
99 void OnAbout(wxCommandEvent& event);
100 void OnBack(wxCommandEvent& event);
101 void OnForward(wxCommandEvent& event);
102
103 private:
104 // any class wishing to process wxWindows events must use this macro
105 DECLARE_EVENT_TABLE()
106 };
107
108 // ----------------------------------------------------------------------------
109 // constants
110 // ----------------------------------------------------------------------------
111
112 // IDs for the controls and the menu commands
113 enum
114 {
115 // menu items
116 Minimal_Quit = 1,
117 Minimal_About,
118 Minimal_Back,
119 Minimal_Forward,
120
121 // controls start here (the numbers are, of course, arbitrary)
122 Minimal_Text = 1000,
123 };
124
125 // ----------------------------------------------------------------------------
126 // event tables and other macros for wxWindows
127 // ----------------------------------------------------------------------------
128
129 // the event tables connect the wxWindows events with the functions (event
130 // handlers) which process them. It can be also done at run-time, but for the
131 // simple menu events like this the static method is much simpler.
132 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
133 EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
134 EVT_MENU(Minimal_About, MyFrame::OnAbout)
135 EVT_MENU(Minimal_Back, MyFrame::OnBack)
136 EVT_MENU(Minimal_Forward, MyFrame::OnForward)
137 END_EVENT_TABLE()
138
139 // Create a new application object: this macro will allow wxWindows to create
140 // the application object during program execution (it's better than using a
141 // static object for many reasons) and also declares the accessor function
142 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
143 // not wxApp)
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // `Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 // Create the main application window
158 MyFrame *frame = new MyFrame("wxHtmlWindow testing application",
159 wxPoint(50, 50), wxSize(640, 480));
160
161 // Show it and tell the application that it's our main window
162 // @@@ what does it do exactly, in fact? is it necessary here?
163 frame->Show(TRUE);
164 SetTopWindow(frame);
165 wxFileSystem::AddHandler(new MyVFS);
166
167 // success: wxApp::OnRun() will be called which will enter the main message
168 // loop and the application will run. If we returned FALSE here, the
169 // application would exit immediately.
170 return TRUE;
171 }
172
173 // ----------------------------------------------------------------------------
174 // main frame
175 // ----------------------------------------------------------------------------
176
177 wxHtmlWindow *html;
178
179 // frame constructor
180 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
181 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
182 {
183 // create a menu bar
184 wxMenu *menuFile = new wxMenu;
185 wxMenu *menuNav = new wxMenu;
186
187 menuFile->Append(Minimal_Quit, "E&xit");
188 menuNav->Append(Minimal_Back, "Go &BACK");
189 menuNav->Append(Minimal_Forward, "Go &FORWARD");
190
191 // now append the freshly created menu to the menu bar...
192 wxMenuBar *menuBar = new wxMenuBar;
193 menuBar->Append(menuFile, "&File");
194 menuBar->Append(menuNav, "&Navigate");
195
196 // ... and attach this menu bar to the frame
197 SetMenuBar(menuBar);
198
199 CreateStatusBar(1);
200
201 html = new wxHtmlWindow(this);
202 html -> SetRelatedFrame(this, "VFS Demo: '%s'");
203 html -> SetRelatedStatusBar(1);
204 html -> LoadPage("start.htm");
205 }
206
207
208 // event handlers
209
210 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
211 {
212 // TRUE is to force the frame to close
213 Close(TRUE);
214 }
215
216 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
217 {
218 }
219
220
221
222 void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
223 {
224 if (!html -> HistoryBack()) wxMessageBox("You reached prehistory era!");
225 }
226
227
228 void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
229 {
230 if (!html -> HistoryForward()) wxMessageBox("No more items in history!");
231 }