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