]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: typetest.cpp | |
3 | // Purpose: Types wxWidgets sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
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 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
23 | #include "wx/variant.h" | |
24 | #include "wx/mimetype.h" | |
25 | ||
26 | #include "typetest.h" | |
27 | ||
28 | #if !defined(__WXMSW__) && !defined(__WXPM__) | |
29 | #include "../sample.xpm" | |
30 | #endif | |
31 | ||
32 | #ifdef new | |
33 | #undef new | |
34 | #endif | |
35 | ||
36 | #include "wx/ioswrap.h" | |
37 | ||
38 | #if wxUSE_IOSTREAMH | |
39 | #include <fstream.h> | |
40 | #else | |
41 | #include <fstream> | |
42 | #endif | |
43 | ||
44 | #include "wx/wfstream.h" | |
45 | #include "wx/datstrm.h" | |
46 | #include "wx/txtstrm.h" | |
47 | #include "wx/mstream.h" | |
48 | ||
49 | // Create a new application object | |
50 | IMPLEMENT_APP (MyApp) | |
51 | ||
52 | IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp) | |
53 | ||
54 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
55 | EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo) | |
56 | EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo) | |
57 | #if wxUSE_UNICODE | |
58 | EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) | |
59 | #endif // wxUSE_UNICODE | |
60 | EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) | |
61 | EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2) | |
62 | EVT_MENU(TYPES_STREAM3, MyApp::DoStreamDemo3) | |
63 | EVT_MENU(TYPES_STREAM4, MyApp::DoStreamDemo4) | |
64 | EVT_MENU(TYPES_STREAM5, MyApp::DoStreamDemo5) | |
65 | EVT_MENU(TYPES_STREAM6, MyApp::DoStreamDemo6) | |
66 | EVT_MENU(TYPES_STREAM7, MyApp::DoStreamDemo7) | |
67 | EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo) | |
68 | END_EVENT_TABLE() | |
69 | ||
70 | wxString file_name = wxT("test_wx.dat"); | |
71 | wxString file_name2 = wxString(wxT("test_wx2.dat")); | |
72 | ||
73 | bool MyApp::OnInit() | |
74 | { | |
75 | if ( !wxApp::OnInit() ) | |
76 | return false; | |
77 | ||
78 | // Create the main frame window | |
79 | MyFrame *frame = new MyFrame((wxFrame *) NULL, wxT("wxWidgets Types Demo"), | |
80 | wxPoint(50, 50), wxSize(450, 340)); | |
81 | ||
82 | // Give it an icon | |
83 | frame->SetIcon(wxICON(sample)); | |
84 | ||
85 | // Make a menubar | |
86 | wxMenu *file_menu = new wxMenu; | |
87 | ||
88 | file_menu->Append(TYPES_ABOUT, wxT("&About")); | |
89 | file_menu->AppendSeparator(); | |
90 | file_menu->Append(TYPES_QUIT, wxT("E&xit\tAlt-X")); | |
91 | ||
92 | wxMenu *test_menu = new wxMenu; | |
93 | test_menu->Append(TYPES_VARIANT, wxT("&Variant test")); | |
94 | test_menu->Append(TYPES_BYTEORDER, wxT("&Byteorder test")); | |
95 | #if wxUSE_UNICODE | |
96 | test_menu->Append(TYPES_UNICODE, wxT("&Unicode test")); | |
97 | #endif // wxUSE_UNICODE | |
98 | test_menu->Append(TYPES_STREAM, wxT("&Stream test")); | |
99 | test_menu->Append(TYPES_STREAM2, wxT("&Stream seek test")); | |
100 | test_menu->Append(TYPES_STREAM3, wxT("&Stream error test")); | |
101 | test_menu->Append(TYPES_STREAM4, wxT("&Stream buffer test")); | |
102 | test_menu->Append(TYPES_STREAM5, wxT("&Stream peek test")); | |
103 | test_menu->Append(TYPES_STREAM6, wxT("&Stream ungetch test")); | |
104 | test_menu->Append(TYPES_STREAM7, wxT("&Stream ungetch test for a buffered stream")); | |
105 | test_menu->AppendSeparator(); | |
106 | test_menu->Append(TYPES_MIME, wxT("&MIME database test")); | |
107 | ||
108 | wxMenuBar *menu_bar = new wxMenuBar; | |
109 | menu_bar->Append(file_menu, wxT("&File")); | |
110 | menu_bar->Append(test_menu, wxT("&Tests")); | |
111 | frame->SetMenuBar(menu_bar); | |
112 | ||
113 | m_textCtrl = new wxTextCtrl(frame, wxID_ANY, wxEmptyString, | |
114 | wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); | |
115 | ||
116 | // Show the frame | |
117 | frame->Show(true); | |
118 | ||
119 | return true; | |
120 | } | |
121 | ||
122 | void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) | |
123 | { | |
124 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
125 | ||
126 | textCtrl.Clear(); | |
127 | textCtrl << wxT("\nTest fstream vs. wxFileStream:\n\n"); | |
128 | ||
129 | textCtrl.WriteText( wxT("Writing to ofstream and wxFileOutputStream:\n") ); | |
130 | ||
131 | wxSTD ofstream std_file_output( "test_std.dat" ); | |
132 | wxFileOutputStream file_output( file_name ); | |
133 | wxBufferedOutputStream buf_output( file_output ); | |
134 | wxTextOutputStream text_output( buf_output ); | |
135 | ||
136 | wxString tmp; | |
137 | signed int si = 0xFFFFFFFF; | |
138 | tmp.Printf( wxT("Signed int: %d\n"), si ); | |
139 | textCtrl.WriteText( tmp ); | |
140 | text_output << si << wxT("\n"); | |
141 | std_file_output << si << "\n"; | |
142 | ||
143 | unsigned int ui = 0xFFFFFFFF; | |
144 | tmp.Printf( wxT("Unsigned int: %u\n"), ui ); | |
145 | textCtrl.WriteText( tmp ); | |
146 | text_output << ui << wxT("\n"); | |
147 | std_file_output << ui << "\n"; | |
148 | ||
149 | double d = 2.01234567890123456789; | |
150 | tmp.Printf( wxT("Double: %f\n"), d ); | |
151 | textCtrl.WriteText( tmp ); | |
152 | text_output << d << wxT("\n"); | |
153 | std_file_output << d << "\n"; | |
154 | ||
155 | float f = (float)0.00001; | |
156 | tmp.Printf( wxT("Float: %f\n"), f ); | |
157 | textCtrl.WriteText( tmp ); | |
158 | text_output << f << wxT("\n"); | |
159 | std_file_output << f << "\n"; | |
160 | ||
161 | wxString str( wxT("Hello!") ); | |
162 | tmp.Printf( wxT("String: %s\n"), str.c_str() ); | |
163 | textCtrl.WriteText( tmp ); | |
164 | text_output << str << wxT("\n"); | |
165 | std_file_output << str.ToAscii() << "\n"; | |
166 | ||
167 | std_file_output.close(); | |
168 | ||
169 | textCtrl.WriteText( wxT("\nReading from ifstream:\n") ); | |
170 | ||
171 | wxSTD ifstream std_file_input( "test_std.dat" ); | |
172 | ||
173 | std_file_input >> si; | |
174 | tmp.Printf( wxT("Signed int: %d\n"), si ); | |
175 | textCtrl.WriteText( tmp ); | |
176 | ||
177 | std_file_input >> ui; | |
178 | tmp.Printf( wxT("Unsigned int: %u\n"), ui ); | |
179 | textCtrl.WriteText( tmp ); | |
180 | ||
181 | std_file_input >> d; | |
182 | tmp.Printf( wxT("Double: %f\n"), d ); | |
183 | textCtrl.WriteText( tmp ); | |
184 | ||
185 | std_file_input >> f; | |
186 | tmp.Printf( wxT("Float: %f\n"), f ); | |
187 | textCtrl.WriteText( tmp ); | |
188 | ||
189 | char std_buf[200]; | |
190 | std_file_input >> std_buf; | |
191 | str = wxString::FromAscii(std_buf); | |
192 | tmp.Printf( wxT("String: %s\n"), str.c_str() ); | |
193 | textCtrl.WriteText( tmp ); | |
194 | ||
195 | textCtrl.WriteText( wxT("\nReading from wxFileInputStream:\n") ); | |
196 | ||
197 | buf_output.Sync(); | |
198 | ||
199 | wxFileInputStream file_input( file_name ); | |
200 | wxBufferedInputStream buf_input( file_input ); | |
201 | wxTextInputStream text_input( file_input ); | |
202 | ||
203 | text_input >> si; | |
204 | tmp.Printf( wxT("Signed int: %d\n"), si ); | |
205 | textCtrl.WriteText( tmp ); | |
206 | ||
207 | text_input >> ui; | |
208 | tmp.Printf( wxT("Unsigned int: %u\n"), ui ); | |
209 | textCtrl.WriteText( tmp ); | |
210 | ||
211 | text_input >> d; | |
212 | tmp.Printf( wxT("Double: %f\n"), d ); | |
213 | textCtrl.WriteText( tmp ); | |
214 | ||
215 | text_input >> f; | |
216 | tmp.Printf( wxT("Float: %f\n"), f ); | |
217 | textCtrl.WriteText( tmp ); | |
218 | ||
219 | text_input >> str; | |
220 | tmp.Printf( wxT("String: %s\n"), str.c_str() ); | |
221 | textCtrl.WriteText( tmp ); | |
222 | ||
223 | ||
224 | ||
225 | textCtrl << wxT("\nTest for wxDataStream:\n\n"); | |
226 | ||
227 | textCtrl.WriteText( wxT("Writing to wxDataOutputStream:\n") ); | |
228 | ||
229 | file_output.SeekO( 0 ); | |
230 | wxDataOutputStream data_output( buf_output ); | |
231 | ||
232 | wxInt16 i16 = (unsigned short)0xFFFF; | |
233 | tmp.Printf( wxT("Signed int16: %d\n"), (int)i16 ); | |
234 | textCtrl.WriteText( tmp ); | |
235 | data_output.Write16( i16 ); | |
236 | ||
237 | wxUint16 ui16 = 0xFFFF; | |
238 | tmp.Printf( wxT("Unsigned int16: %u\n"), (unsigned int) ui16 ); | |
239 | textCtrl.WriteText( tmp ); | |
240 | data_output.Write16( ui16 ); | |
241 | ||
242 | d = 2.01234567890123456789; | |
243 | tmp.Printf( wxT("Double: %f\n"), d ); | |
244 | textCtrl.WriteText( tmp ); | |
245 | data_output.WriteDouble( d ); | |
246 | ||
247 | str = wxT("Hello!"); | |
248 | tmp.Printf( wxT("String: %s\n"), str.c_str() ); | |
249 | textCtrl.WriteText( tmp ); | |
250 | data_output.WriteString( str ); | |
251 | ||
252 | buf_output.Sync(); | |
253 | ||
254 | textCtrl.WriteText( wxT("\nReading from wxDataInputStream:\n") ); | |
255 | ||
256 | file_input.SeekI( 0 ); | |
257 | wxDataInputStream data_input( buf_input ); | |
258 | ||
259 | i16 = data_input.Read16(); | |
260 | tmp.Printf( wxT("Signed int16: %d\n"), (int)i16 ); | |
261 | textCtrl.WriteText( tmp ); | |
262 | ||
263 | ui16 = data_input.Read16(); | |
264 | tmp.Printf( wxT("Unsigned int16: %u\n"), (unsigned int) ui16 ); | |
265 | textCtrl.WriteText( tmp ); | |
266 | ||
267 | d = data_input.ReadDouble(); | |
268 | tmp.Printf( wxT("Double: %f\n"), d ); | |
269 | textCtrl.WriteText( tmp ); | |
270 | ||
271 | str = data_input.ReadString(); | |
272 | tmp.Printf( wxT("String: %s\n"), str.c_str() ); | |
273 | textCtrl.WriteText( tmp ); | |
274 | } | |
275 | ||
276 | void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) | |
277 | { | |
278 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
279 | ||
280 | textCtrl.Clear(); | |
281 | textCtrl << wxT("\nTesting wxBufferedStream:\n\n"); | |
282 | ||
283 | char ch,ch2; | |
284 | ||
285 | textCtrl.WriteText( wxT("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") ); | |
286 | ||
287 | wxFileOutputStream file_output( file_name ); | |
288 | wxBufferedOutputStream buf_output( file_output ); | |
289 | for (ch = 0; ch < 10; ch++) | |
290 | buf_output.Write( &ch, 1 ); | |
291 | buf_output.Sync(); | |
292 | ||
293 | wxFileInputStream file_input( file_name ); | |
294 | for (ch2 = 0; ch2 < 10; ch2++) | |
295 | { | |
296 | file_input.Read( &ch, 1 ); | |
297 | textCtrl.WriteText( (wxChar)(ch + wxT('0')) ); | |
298 | } | |
299 | textCtrl.WriteText( wxT("\n\n\n") ); | |
300 | ||
301 | textCtrl.WriteText( wxT("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") ); | |
302 | textCtrl.WriteText( wxT("seeking back to #3 and writing 0:\n\n") ); | |
303 | ||
304 | wxFileOutputStream file_output2( file_name2 ); | |
305 | wxBufferedOutputStream buf_output2( file_output2 ); | |
306 | for (ch = 0; ch < 10; ch++) | |
307 | buf_output2.Write( &ch, 1 ); | |
308 | buf_output2.SeekO( 3 ); | |
309 | ch = 0; | |
310 | buf_output2.Write( &ch, 1 ); | |
311 | buf_output2.Sync(); | |
312 | ||
313 | wxFileInputStream file_input2( file_name2 ); | |
314 | for (ch2 = 0; ch2 < 10; ch2++) | |
315 | { | |
316 | file_input2.Read( &ch, 1 ); | |
317 | textCtrl.WriteText( (wxChar)(ch + wxT('0')) ); | |
318 | } | |
319 | textCtrl.WriteText( wxT("\n\n\n") ); | |
320 | ||
321 | // now append 2000 bytes to file (bigger than buffer) | |
322 | buf_output2.SeekO( 0, wxFromEnd ); | |
323 | ch = 1; | |
324 | for (int i = 0; i < 2000; i++) | |
325 | buf_output2.Write( &ch, 1 ); | |
326 | buf_output2.Sync(); | |
327 | ||
328 | textCtrl.WriteText( wxT("Reading number 0 to 9 from buffered wxFileInputStream, then\n") ); | |
329 | textCtrl.WriteText( wxT("seeking back to #3 and reading the 0:\n\n") ); | |
330 | ||
331 | wxFileInputStream file_input3( file_name2 ); | |
332 | wxBufferedInputStream buf_input3( file_input3 ); | |
333 | for (ch2 = 0; ch2 < 10; ch2++) | |
334 | { | |
335 | buf_input3.Read( &ch, 1 ); | |
336 | textCtrl.WriteText( (wxChar)(ch + wxT('0')) ); | |
337 | } | |
338 | for (int j = 0; j < 2000; j++) | |
339 | buf_input3.Read( &ch, 1 ); | |
340 | textCtrl.WriteText( wxT("\n") ); | |
341 | buf_input3.SeekI( 3 ); | |
342 | buf_input3.Read( &ch, 1 ); | |
343 | textCtrl.WriteText( (wxChar)(ch + wxT('0')) ); | |
344 | textCtrl.WriteText( wxT("\n\n\n") ); | |
345 | ||
346 | } | |
347 | ||
348 | void MyApp::DoStreamDemo3(wxCommandEvent& WXUNUSED(event)) | |
349 | { | |
350 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
351 | ||
352 | textCtrl.Clear(); | |
353 | textCtrl << wxT("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n"); | |
354 | ||
355 | char ch,ch2; | |
356 | ||
357 | textCtrl.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream:\n\n") ); | |
358 | ||
359 | wxFileOutputStream file_output( file_name ); | |
360 | for (ch = 0; ch < 10; ch++) | |
361 | file_output.Write( &ch, 1 ); | |
362 | ||
363 | // Testing wxFileInputStream | |
364 | ||
365 | textCtrl.WriteText( wxT("Reading 0 to 10 to wxFileInputStream:\n\n") ); | |
366 | ||
367 | wxFileInputStream file_input( file_name ); | |
368 | for (ch2 = 0; ch2 < 11; ch2++) | |
369 | { | |
370 | file_input.Read( &ch, 1 ); | |
371 | textCtrl.WriteText( wxT("Value read: ") ); | |
372 | textCtrl.WriteText( (wxChar)(ch + '0') ); | |
373 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
374 | switch (file_input.GetLastError()) | |
375 | { | |
376 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
377 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
378 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
379 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
380 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
381 | } | |
382 | } | |
383 | textCtrl.WriteText( wxT("\n") ); | |
384 | ||
385 | textCtrl.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") ); | |
386 | file_input.SeekI( 0 ); | |
387 | switch (file_input.GetLastError()) | |
388 | { | |
389 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
390 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
391 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
392 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
393 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
394 | } | |
395 | textCtrl.WriteText( wxT("\n") ); | |
396 | ||
397 | file_input.Read( &ch, 1 ); | |
398 | textCtrl.WriteText( wxT("Value read: ") ); | |
399 | textCtrl.WriteText( (wxChar)(ch + wxT('0')) ); | |
400 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
401 | switch (file_input.GetLastError()) | |
402 | { | |
403 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
404 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
405 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
406 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
407 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
408 | } | |
409 | textCtrl.WriteText( wxT("\n\n") ); | |
410 | ||
411 | ||
412 | // Testing wxFFileInputStream | |
413 | ||
414 | textCtrl.WriteText( wxT("Reading 0 to 10 to wxFFileInputStream:\n\n") ); | |
415 | ||
416 | wxFFileInputStream ffile_input( file_name ); | |
417 | for (ch2 = 0; ch2 < 11; ch2++) | |
418 | { | |
419 | ffile_input.Read( &ch, 1 ); | |
420 | textCtrl.WriteText( wxT("Value read: ") ); | |
421 | textCtrl.WriteText( (wxChar)(ch + '0') ); | |
422 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
423 | switch (ffile_input.GetLastError()) | |
424 | { | |
425 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
426 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
427 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
428 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
429 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
430 | } | |
431 | } | |
432 | textCtrl.WriteText( wxT("\n") ); | |
433 | ||
434 | textCtrl.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") ); | |
435 | ffile_input.SeekI( 0 ); | |
436 | switch (ffile_input.GetLastError()) | |
437 | { | |
438 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
439 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
440 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
441 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
442 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
443 | } | |
444 | textCtrl.WriteText( wxT("\n") ); | |
445 | ||
446 | ffile_input.Read( &ch, 1 ); | |
447 | textCtrl.WriteText( wxT("Value read: ") ); | |
448 | textCtrl.WriteText( (wxChar)(ch + '0') ); | |
449 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
450 | switch (ffile_input.GetLastError()) | |
451 | { | |
452 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
453 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
454 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
455 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
456 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
457 | } | |
458 | textCtrl.WriteText( wxT("\n\n") ); | |
459 | ||
460 | // Testing wxFFileInputStream | |
461 | ||
462 | textCtrl.WriteText( wxT("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") ); | |
463 | ||
464 | wxFFileInputStream ffile_input2( file_name ); | |
465 | wxBufferedInputStream buf_input( ffile_input2 ); | |
466 | for (ch2 = 0; ch2 < 11; ch2++) | |
467 | { | |
468 | buf_input.Read( &ch, 1 ); | |
469 | textCtrl.WriteText( wxT("Value read: ") ); | |
470 | textCtrl.WriteText( (wxChar)(ch + '0') ); | |
471 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
472 | switch (buf_input.GetLastError()) | |
473 | { | |
474 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
475 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
476 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
477 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
478 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
479 | } | |
480 | } | |
481 | textCtrl.WriteText( wxT("\n") ); | |
482 | ||
483 | textCtrl.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") ); | |
484 | buf_input.SeekI( 0 ); | |
485 | switch (buf_input.GetLastError()) | |
486 | { | |
487 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
488 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
489 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
490 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
491 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
492 | } | |
493 | textCtrl.WriteText( wxT("\n") ); | |
494 | ||
495 | buf_input.Read( &ch, 1 ); | |
496 | textCtrl.WriteText( wxT("Value read: ") ); | |
497 | textCtrl.WriteText( (wxChar)(ch + '0') ); | |
498 | textCtrl.WriteText( wxT("; stream.GetLastError() returns: ") ); | |
499 | switch (buf_input.GetLastError()) | |
500 | { | |
501 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
502 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
503 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
504 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
505 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
506 | } | |
507 | } | |
508 | ||
509 | void MyApp::DoStreamDemo4(wxCommandEvent& WXUNUSED(event)) | |
510 | { | |
511 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
512 | ||
513 | wxString msg; | |
514 | ||
515 | textCtrl.Clear(); | |
516 | textCtrl << wxT("\nTesting wxStreamBuffer:\n\n"); | |
517 | ||
518 | // bigger than buffer | |
519 | textCtrl.WriteText( wxT("Writing 2000x 1 to wxFileOutputStream.\n\n") ); | |
520 | ||
521 | wxFileOutputStream file_output( file_name ); | |
522 | for (int i = 0; i < 2000; i++) | |
523 | { | |
524 | char ch = 1; | |
525 | file_output.Write( &ch, 1 ); | |
526 | } | |
527 | ||
528 | textCtrl.WriteText( wxT("Opening with a buffered wxFileInputStream:\n\n") ); | |
529 | ||
530 | wxFileInputStream file_input( file_name ); | |
531 | wxBufferedInputStream buf_input( file_input ); | |
532 | ||
533 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
534 | switch (buf_input.GetLastError()) | |
535 | { | |
536 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
537 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
538 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
539 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
540 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
541 | } | |
542 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
543 | textCtrl.WriteText( msg ); | |
544 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
545 | textCtrl.WriteText( msg ); | |
546 | textCtrl.WriteText( wxT("\n\n") ); | |
547 | ||
548 | ||
549 | textCtrl.WriteText( wxT("Seeking to position 300:\n\n") ); | |
550 | ||
551 | buf_input.SeekI( 300 ); | |
552 | ||
553 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
554 | switch (buf_input.GetLastError()) | |
555 | { | |
556 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
557 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
558 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
559 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
560 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
561 | } | |
562 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
563 | textCtrl.WriteText( msg ); | |
564 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
565 | textCtrl.WriteText( msg ); | |
566 | textCtrl.WriteText( wxT("\n\n") ); | |
567 | ||
568 | ||
569 | char buf[2000]; | |
570 | ||
571 | textCtrl.WriteText( wxT("Reading 500 bytes:\n\n") ); | |
572 | ||
573 | buf_input.Read( buf, 500 ); | |
574 | ||
575 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
576 | switch (buf_input.GetLastError()) | |
577 | { | |
578 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
579 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
580 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
581 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
582 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
583 | } | |
584 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
585 | textCtrl.WriteText( msg ); | |
586 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
587 | textCtrl.WriteText( msg ); | |
588 | textCtrl.WriteText( wxT("\n\n") ); | |
589 | ||
590 | textCtrl.WriteText( wxT("Reading another 500 bytes:\n\n") ); | |
591 | ||
592 | buf_input.Read( buf, 500 ); | |
593 | ||
594 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
595 | switch (buf_input.GetLastError()) | |
596 | { | |
597 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
598 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
599 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
600 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
601 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
602 | } | |
603 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
604 | textCtrl.WriteText( msg ); | |
605 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
606 | textCtrl.WriteText( msg ); | |
607 | textCtrl.WriteText( wxT("\n\n") ); | |
608 | ||
609 | textCtrl.WriteText( wxT("Reading another 500 bytes:\n\n") ); | |
610 | ||
611 | buf_input.Read( buf, 500 ); | |
612 | ||
613 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
614 | switch (buf_input.GetLastError()) | |
615 | { | |
616 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
617 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
618 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
619 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
620 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
621 | } | |
622 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
623 | textCtrl.WriteText( msg ); | |
624 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
625 | textCtrl.WriteText( msg ); | |
626 | textCtrl.WriteText( wxT("\n\n") ); | |
627 | ||
628 | textCtrl.WriteText( wxT("Reading another 500 bytes:\n\n") ); | |
629 | ||
630 | buf_input.Read( buf, 500 ); | |
631 | ||
632 | textCtrl.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") ); | |
633 | switch (buf_input.GetLastError()) | |
634 | { | |
635 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break; | |
636 | case wxSTREAM_EOF: textCtrl.WriteText( wxT("wxSTREAM_EOF\n") ); break; | |
637 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break; | |
638 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break; | |
639 | default: textCtrl.WriteText( wxT("Huh?\n") ); break; | |
640 | } | |
641 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); | |
642 | textCtrl.WriteText( msg ); | |
643 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); | |
644 | textCtrl.WriteText( msg ); | |
645 | textCtrl.WriteText( wxT("\n\n") ); | |
646 | } | |
647 | ||
648 | void MyApp::DoStreamDemo5(wxCommandEvent& WXUNUSED(event)) | |
649 | { | |
650 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
651 | ||
652 | textCtrl.Clear(); | |
653 | textCtrl << wxT("\nTesting wxFileInputStream's Peek():\n\n"); | |
654 | ||
655 | char ch; | |
656 | wxString str; | |
657 | ||
658 | textCtrl.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream:\n\n") ); | |
659 | ||
660 | wxFileOutputStream file_output( file_name ); | |
661 | for (ch = 0; ch < 10; ch++) | |
662 | file_output.Write( &ch, 1 ); | |
663 | ||
664 | file_output.Sync(); | |
665 | ||
666 | wxFileInputStream file_input( file_name ); | |
667 | ||
668 | ch = file_input.Peek(); | |
669 | str.Printf( wxT("First char peeked: %d\n"), (int) ch ); | |
670 | textCtrl.WriteText( str ); | |
671 | ||
672 | ch = file_input.GetC(); | |
673 | str.Printf( wxT("First char read: %d\n"), (int) ch ); | |
674 | textCtrl.WriteText( str ); | |
675 | ||
676 | ch = file_input.Peek(); | |
677 | str.Printf( wxT("Second char peeked: %d\n"), (int) ch ); | |
678 | textCtrl.WriteText( str ); | |
679 | ||
680 | ch = file_input.GetC(); | |
681 | str.Printf( wxT("Second char read: %d\n"), (int) ch ); | |
682 | textCtrl.WriteText( str ); | |
683 | ||
684 | ch = file_input.Peek(); | |
685 | str.Printf( wxT("Third char peeked: %d\n"), (int) ch ); | |
686 | textCtrl.WriteText( str ); | |
687 | ||
688 | ch = file_input.GetC(); | |
689 | str.Printf( wxT("Third char read: %d\n"), (int) ch ); | |
690 | textCtrl.WriteText( str ); | |
691 | ||
692 | ||
693 | textCtrl << wxT("\n\n\nTesting wxMemoryInputStream's Peek():\n\n"); | |
694 | ||
695 | char buf[] = { 0,1,2,3,4,5,6,7,8,9,10 }; | |
696 | wxMemoryInputStream input( buf, 10 ); | |
697 | ||
698 | ch = input.Peek(); | |
699 | str.Printf( wxT("First char peeked: %d\n"), (int) ch ); | |
700 | textCtrl.WriteText( str ); | |
701 | ||
702 | ch = input.GetC(); | |
703 | str.Printf( wxT("First char read: %d\n"), (int) ch ); | |
704 | textCtrl.WriteText( str ); | |
705 | ||
706 | ch = input.Peek(); | |
707 | str.Printf( wxT("Second char peeked: %d\n"), (int) ch ); | |
708 | textCtrl.WriteText( str ); | |
709 | ||
710 | ch = input.GetC(); | |
711 | str.Printf( wxT("Second char read: %d\n"), (int) ch ); | |
712 | textCtrl.WriteText( str ); | |
713 | ||
714 | ch = input.Peek(); | |
715 | str.Printf( wxT("Third char peeked: %d\n"), (int) ch ); | |
716 | textCtrl.WriteText( str ); | |
717 | ||
718 | ch = input.GetC(); | |
719 | str.Printf( wxT("Third char read: %d\n"), (int) ch ); | |
720 | textCtrl.WriteText( str ); | |
721 | } | |
722 | ||
723 | void MyApp::DoStreamDemo6(wxCommandEvent& WXUNUSED(event)) | |
724 | { | |
725 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
726 | ||
727 | textCtrl.Clear(); | |
728 | textCtrl.WriteText( wxT("\nTesting Ungetch():\n\n") ); | |
729 | ||
730 | char ch = 0; | |
731 | wxString str; | |
732 | ||
733 | textCtrl.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream...\n\n") ); | |
734 | ||
735 | wxFileOutputStream file_output( file_name ); | |
736 | for (ch = 0; ch < 10; ch++) | |
737 | file_output.Write( &ch, 1 ); | |
738 | ||
739 | file_output.Sync(); | |
740 | ||
741 | textCtrl.WriteText( wxT("Reading char from wxFileInputStream:\n\n") ); | |
742 | ||
743 | wxFileInputStream file_input( file_name ); | |
744 | ||
745 | ch = file_input.GetC(); | |
746 | size_t pos = (size_t)file_input.TellI(); | |
747 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
748 | textCtrl.WriteText( str ); | |
749 | ||
750 | textCtrl.WriteText( wxT("Reading another char from wxFileInputStream:\n\n") ); | |
751 | ||
752 | ch = file_input.GetC(); | |
753 | pos = (size_t)file_input.TellI(); | |
754 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
755 | textCtrl.WriteText( str ); | |
756 | ||
757 | textCtrl.WriteText( wxT("Reading yet another char from wxFileInputStream:\n\n") ); | |
758 | ||
759 | ch = file_input.GetC(); | |
760 | pos = (size_t)file_input.TellI(); | |
761 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
762 | textCtrl.WriteText( str ); | |
763 | ||
764 | textCtrl.WriteText( wxT("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") ); | |
765 | ||
766 | file_input.Ungetch( 5 ); | |
767 | pos = (size_t)file_input.TellI(); | |
768 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); | |
769 | textCtrl.WriteText( str ); | |
770 | ||
771 | textCtrl.WriteText( wxT("Reading char from wxFileInputStream:\n\n") ); | |
772 | ||
773 | ch = file_input.GetC(); | |
774 | pos = (size_t)file_input.TellI(); | |
775 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
776 | textCtrl.WriteText( str ); | |
777 | ||
778 | textCtrl.WriteText( wxT("Reading another char from wxFileInputStream:\n\n") ); | |
779 | ||
780 | ch = file_input.GetC(); | |
781 | pos = (size_t)file_input.TellI(); | |
782 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
783 | textCtrl.WriteText( str ); | |
784 | ||
785 | textCtrl.WriteText( wxT("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") ); | |
786 | ||
787 | file_input.Ungetch( 5 ); | |
788 | pos = (size_t)file_input.TellI(); | |
789 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); | |
790 | textCtrl.WriteText( str ); | |
791 | ||
792 | textCtrl.WriteText( wxT("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") ); | |
793 | ||
794 | file_input.SeekI( 3 ); | |
795 | ||
796 | ch = file_input.GetC(); | |
797 | pos = (size_t)file_input.TellI(); | |
798 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
799 | textCtrl.WriteText( str ); | |
800 | } | |
801 | ||
802 | void MyApp::DoStreamDemo7(wxCommandEvent& WXUNUSED(event)) | |
803 | { | |
804 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
805 | ||
806 | textCtrl.Clear(); | |
807 | textCtrl.WriteText( wxT("\nTesting Ungetch() in buffered input stream:\n\n") ); | |
808 | ||
809 | char ch = 0; | |
810 | wxString str; | |
811 | ||
812 | textCtrl.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream...\n\n") ); | |
813 | ||
814 | wxFileOutputStream file_output( file_name ); | |
815 | for (ch = 0; ch < 10; ch++) | |
816 | file_output.Write( &ch, 1 ); | |
817 | ||
818 | file_output.Sync(); | |
819 | ||
820 | textCtrl.WriteText( wxT("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") ); | |
821 | ||
822 | wxFileInputStream file_input( file_name ); | |
823 | wxBufferedInputStream buf_input( file_input ); | |
824 | ||
825 | ch = buf_input.GetC(); | |
826 | size_t pos = (size_t)buf_input.TellI(); | |
827 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
828 | textCtrl.WriteText( str ); | |
829 | ||
830 | textCtrl.WriteText( wxT("Reading another char from wxBufferedInputStream:\n\n") ); | |
831 | ||
832 | ch = buf_input.GetC(); | |
833 | pos = (size_t)buf_input.TellI(); | |
834 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
835 | textCtrl.WriteText( str ); | |
836 | ||
837 | textCtrl.WriteText( wxT("Reading yet another char from wxBufferedInputStream:\n\n") ); | |
838 | ||
839 | ch = buf_input.GetC(); | |
840 | pos = (size_t)buf_input.TellI(); | |
841 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
842 | textCtrl.WriteText( str ); | |
843 | ||
844 | textCtrl.WriteText( wxT("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") ); | |
845 | ||
846 | buf_input.Ungetch( 5 ); | |
847 | pos = (size_t)buf_input.TellI(); | |
848 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); | |
849 | textCtrl.WriteText( str ); | |
850 | ||
851 | textCtrl.WriteText( wxT("Reading char from wxBufferedInputStream:\n\n") ); | |
852 | ||
853 | ch = buf_input.GetC(); | |
854 | pos = (size_t)buf_input.TellI(); | |
855 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
856 | textCtrl.WriteText( str ); | |
857 | ||
858 | textCtrl.WriteText( wxT("Reading another char from wxBufferedInputStream:\n\n") ); | |
859 | ||
860 | ch = buf_input.GetC(); | |
861 | pos = (size_t)buf_input.TellI(); | |
862 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
863 | textCtrl.WriteText( str ); | |
864 | ||
865 | textCtrl.WriteText( wxT("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") ); | |
866 | ||
867 | buf_input.Ungetch( 5 ); | |
868 | pos = (size_t)buf_input.TellI(); | |
869 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); | |
870 | textCtrl.WriteText( str ); | |
871 | ||
872 | textCtrl.WriteText( wxT("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") ); | |
873 | ||
874 | buf_input.SeekI( 3 ); | |
875 | ||
876 | ch = buf_input.GetC(); | |
877 | pos = (size_t)buf_input.TellI(); | |
878 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); | |
879 | textCtrl.WriteText( str ); | |
880 | } | |
881 | ||
882 | #if wxUSE_UNICODE | |
883 | void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) | |
884 | { | |
885 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
886 | ||
887 | textCtrl.Clear(); | |
888 | textCtrl << wxT("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:"); | |
889 | ||
890 | wxString str; | |
891 | str = wxT("Robert R\366bling\n"); | |
892 | ||
893 | printf( "\n\nConversion with wxConvLocal:\n" ); | |
894 | wxConvCurrent = &wxConvLocal; | |
895 | printf( (const char*) str.mbc_str() ); | |
896 | printf( "\n\nConversion with wxConvLibc:\n" ); | |
897 | wxConvCurrent = &wxConvLibc; | |
898 | printf( (const char*) str.mbc_str() ); | |
899 | ||
900 | } | |
901 | #endif | |
902 | ||
903 | void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event)) | |
904 | { | |
905 | static wxString s_defaultExt = wxT("xyz"); | |
906 | ||
907 | wxString ext = wxGetTextFromUser(wxT("Enter a file extension: "), | |
908 | wxT("MIME database test"), | |
909 | s_defaultExt); | |
910 | if ( !!ext ) | |
911 | { | |
912 | s_defaultExt = ext; | |
913 | ||
914 | // init MIME database if not done yet | |
915 | if ( !m_mimeDatabase ) | |
916 | { | |
917 | m_mimeDatabase = new wxMimeTypesManager; | |
918 | ||
919 | static const wxFileTypeInfo fallbacks[] = | |
920 | { | |
921 | wxFileTypeInfo(wxT("application/xyz"), | |
922 | wxT("XyZ %s"), | |
923 | wxT("XyZ -p %s"), | |
924 | wxT("The one and only XYZ format file"), | |
925 | wxT("xyz"), wxT("123"), wxNullPtr), | |
926 | wxFileTypeInfo(wxT("text/html"), | |
927 | wxT("lynx %s"), | |
928 | wxT("lynx -dump %s | lpr"), | |
929 | wxT("HTML document (from fallback)"), | |
930 | wxT("htm"), wxT("html"), wxNullPtr), | |
931 | ||
932 | // must terminate the table with this! | |
933 | wxFileTypeInfo() | |
934 | }; | |
935 | ||
936 | m_mimeDatabase->AddFallbacks(fallbacks); | |
937 | } | |
938 | ||
939 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
940 | ||
941 | wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext); | |
942 | if ( !filetype ) | |
943 | { | |
944 | textCtrl << wxT("Unknown extension '") << ext << wxT("'\n"); | |
945 | } | |
946 | else | |
947 | { | |
948 | wxString type, desc, open; | |
949 | filetype->GetMimeType(&type); | |
950 | filetype->GetDescription(&desc); | |
951 | ||
952 | wxString filename = wxT("filename"); | |
953 | filename << wxT(".") << ext; | |
954 | wxFileType::MessageParameters params(filename, type); | |
955 | filetype->GetOpenCommand(&open, params); | |
956 | ||
957 | textCtrl << wxT("MIME information about extension '") << ext << wxT('\n') | |
958 | << wxT("\tMIME type: ") << ( !type ? wxString("unknown") : type ) << wxT('\n') | |
959 | << wxT("\tDescription: ") << ( !desc ? wxString(wxEmptyString) : desc ) | |
960 | << wxT('\n') | |
961 | << wxT("\tCommand to open: ") << ( !open ? wxString("no") : open ) | |
962 | << wxT('\n'); | |
963 | ||
964 | delete filetype; | |
965 | } | |
966 | } | |
967 | //else: cancelled by user | |
968 | } | |
969 | ||
970 | void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event)) | |
971 | { | |
972 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
973 | ||
974 | textCtrl.Clear(); | |
975 | textCtrl << wxT("\nTest byte order macros:\n\n"); | |
976 | ||
977 | #if wxBYTE_ORDER == wxLITTLE_ENDIAN | |
978 | textCtrl << wxT("This is a little endian system.\n\n"); | |
979 | #else | |
980 | textCtrl << wxT("This is a big endian system.\n\n"); | |
981 | #endif | |
982 | ||
983 | wxString text; | |
984 | ||
985 | wxInt32 var = 0xF1F2F3F4; | |
986 | text = wxEmptyString; | |
987 | text.Printf( wxT("Value of wxInt32 is now: %#x.\n\n"), var ); | |
988 | textCtrl.WriteText( text ); | |
989 | ||
990 | text = wxEmptyString; | |
991 | text.Printf( wxT("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) ); | |
992 | textCtrl.WriteText( text ); | |
993 | ||
994 | text = wxEmptyString; | |
995 | text.Printf( wxT("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) ); | |
996 | textCtrl.WriteText( text ); | |
997 | ||
998 | text = wxEmptyString; | |
999 | text.Printf( wxT("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) ); | |
1000 | textCtrl.WriteText( text ); | |
1001 | } | |
1002 | ||
1003 | void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) ) | |
1004 | { | |
1005 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
1006 | ||
1007 | wxVariant var1 = wxT("String value"); | |
1008 | textCtrl << wxT("var1 = ") << var1.MakeString() << wxT("\n"); | |
1009 | ||
1010 | // Conversion | |
1011 | wxString str = var1.MakeString(); | |
1012 | ||
1013 | var1 = 123.456; | |
1014 | textCtrl << wxT("var1 = ") << var1.GetReal() << wxT("\n"); | |
1015 | ||
1016 | // Implicit conversion | |
1017 | double v = var1; | |
1018 | ||
1019 | var1 = 9876L; | |
1020 | textCtrl << wxT("var1 = ") << var1.GetLong() << wxT("\n"); | |
1021 | ||
1022 | // Implicit conversion | |
1023 | long l = var1; | |
1024 | ||
1025 | // suppress compile warnings about unused variables | |
1026 | wxUnusedVar(l); | |
1027 | wxUnusedVar(v); | |
1028 | ||
1029 | wxArrayString stringArray; | |
1030 | stringArray.Add(wxT("one")); stringArray.Add(wxT("two")); stringArray.Add(wxT("three")); | |
1031 | var1 = stringArray; | |
1032 | textCtrl << wxT("var1 = ") << var1.MakeString() << wxT("\n"); | |
1033 | ||
1034 | var1.ClearList(); | |
1035 | var1.Append(wxVariant(1.2345)); | |
1036 | var1.Append(wxVariant(wxT("hello"))); | |
1037 | var1.Append(wxVariant(54321L)); | |
1038 | ||
1039 | textCtrl << wxT("var1 = ") << var1.MakeString() << wxT("\n"); | |
1040 | ||
1041 | size_t n = var1.GetCount(); | |
1042 | size_t i; | |
1043 | for (i = (size_t) 0; i < n; i++) | |
1044 | { | |
1045 | textCtrl << wxT("var1[") << (int) i << wxT("] (type ") << var1[i].GetType() << wxT(") = ") << var1[i].MakeString() << wxT("\n"); | |
1046 | } | |
1047 | ||
1048 | var1 = wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT))); | |
1049 | textCtrl << wxT("var1 = (wxfont)\""); | |
1050 | wxFont* font = wxGetVariantCast(var1,wxFont); | |
1051 | if (font) | |
1052 | { | |
1053 | textCtrl << font->GetNativeFontInfoDesc() << wxT("\"\n"); | |
1054 | } | |
1055 | else | |
1056 | { | |
1057 | textCtrl << wxT("(null)\"\n"); | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
1062 | EVT_MENU(TYPES_QUIT, MyFrame::OnQuit) | |
1063 | EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout) | |
1064 | END_EVENT_TABLE() | |
1065 | ||
1066 | // My frame constructor | |
1067 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, | |
1068 | const wxPoint& pos, const wxSize& size) | |
1069 | : wxFrame(parent, wxID_ANY, title, pos, size) | |
1070 | {} | |
1071 | ||
1072 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
1073 | { | |
1074 | Close(true); | |
1075 | } | |
1076 | ||
1077 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
1078 | { | |
1079 | wxMessageDialog dialog(this, wxT("Tests various wxWidgets types"), | |
1080 | wxT("About Types"), wxYES_NO|wxCANCEL); | |
1081 | ||
1082 | dialog.ShowModal(); | |
1083 | } | |
1084 | ||
1085 |