]>
git.saurik.com Git - wxWidgets.git/blob - samples/typetest/typetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "typetest.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/variant.h"
28 #include "wx/mimetype.h"
32 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
33 #include "mondrian.xpm"
40 #include "wx/ioswrap.h"
48 #include "wx/wfstream.h"
49 #include "wx/datstrm.h"
50 #include "wx/txtstrm.h"
51 #include "wx/mstream.h"
53 // Create a new application object
56 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
58 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
59 EVT_MENU(TYPES_VARIANT
, MyApp::DoVariantDemo
)
60 EVT_MENU(TYPES_BYTEORDER
, MyApp::DoByteOrderDemo
)
62 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
63 #endif // wxUSE_UNICODE
64 EVT_MENU(TYPES_STREAM
, MyApp::DoStreamDemo
)
65 EVT_MENU(TYPES_STREAM2
, MyApp::DoStreamDemo2
)
66 EVT_MENU(TYPES_STREAM3
, MyApp::DoStreamDemo3
)
67 EVT_MENU(TYPES_STREAM4
, MyApp::DoStreamDemo4
)
68 EVT_MENU(TYPES_STREAM5
, MyApp::DoStreamDemo5
)
69 EVT_MENU(TYPES_STREAM6
, MyApp::DoStreamDemo6
)
70 EVT_MENU(TYPES_STREAM7
, MyApp::DoStreamDemo7
)
71 EVT_MENU(TYPES_MIME
, MyApp::DoMIMEDemo
)
76 // Create the main frame window
77 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWindows Types Demo"),
78 wxPoint(50, 50), wxSize(450, 340));
81 frame
->SetIcon(wxICON(mondrian
));
84 wxMenu
*file_menu
= new wxMenu
;
86 file_menu
->Append(TYPES_ABOUT
, _T("&About"));
87 file_menu
->AppendSeparator();
88 file_menu
->Append(TYPES_QUIT
, _T("E&xit\tAlt-X"));
90 wxMenu
*test_menu
= new wxMenu
;
91 test_menu
->Append(TYPES_VARIANT
, _T("&Variant test"));
92 test_menu
->Append(TYPES_BYTEORDER
, _T("&Byteorder test"));
94 test_menu
->Append(TYPES_UNICODE
, _T("&Unicode test"));
95 #endif // wxUSE_UNICODE
96 test_menu
->Append(TYPES_STREAM
, _T("&Stream test"));
97 test_menu
->Append(TYPES_STREAM2
, _T("&Stream seek test"));
98 test_menu
->Append(TYPES_STREAM3
, _T("&Stream error test"));
99 test_menu
->Append(TYPES_STREAM4
, _T("&Stream buffer test"));
100 test_menu
->Append(TYPES_STREAM5
, _T("&Stream peek test"));
101 test_menu
->Append(TYPES_STREAM6
, _T("&Stream ungetch test"));
102 test_menu
->Append(TYPES_STREAM7
, _T("&Stream ungetch test for a buffered stream"));
103 test_menu
->AppendSeparator();
104 test_menu
->Append(TYPES_MIME
, _T("&MIME database test"));
106 wxMenuBar
*menu_bar
= new wxMenuBar
;
107 menu_bar
->Append(file_menu
, _T("&File"));
108 menu_bar
->Append(test_menu
, _T("&Tests"));
109 frame
->SetMenuBar(menu_bar
);
111 m_textCtrl
= new wxTextCtrl(frame
, -1, _T(""), wxPoint(0, 0), wxDefaultSize
, wxTE_MULTILINE
);
121 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
123 wxTextCtrl
& textCtrl
= * GetTextCtrl();
126 textCtrl
<< _T("\nTest fstream vs. wxFileStream:\n\n");
128 textCtrl
.WriteText( _T("Writing to ofstream and wxFileOutputStream:\n") );
130 wxSTD ofstream
std_file_output( "test_std.dat" );
131 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
132 wxBufferedOutputStream
buf_output( file_output
);
133 wxTextOutputStream
text_output( buf_output
);
136 signed int si
= 0xFFFFFFFF;
137 tmp
.Printf( _T("Signed int: %d\n"), si
);
138 textCtrl
.WriteText( tmp
);
139 text_output
<< si
<< _T("\n");
140 std_file_output
<< si
<< _T("\n");
142 unsigned int ui
= 0xFFFFFFFF;
143 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
144 textCtrl
.WriteText( tmp
);
145 text_output
<< ui
<< _T("\n");
146 std_file_output
<< ui
<< _T("\n");
148 double d
= 2.01234567890123456789;
149 tmp
.Printf( _T("Double: %f\n"), d
);
150 textCtrl
.WriteText( tmp
);
151 text_output
<< d
<< _T("\n");
152 std_file_output
<< d
<< _T("\n");
154 float f
= (float)0.00001;
155 tmp
.Printf( _T("Float: %f\n"), f
);
156 textCtrl
.WriteText( tmp
);
157 text_output
<< f
<< _T("\n");
158 std_file_output
<< f
<< _T("\n");
160 wxString
str( _T("Hello!") );
161 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
162 textCtrl
.WriteText( tmp
);
163 text_output
<< str
<< _T("\n");
164 std_file_output
<< str
.c_str() << _T("\n");
166 textCtrl
.WriteText( _T("\nReading from ifstream:\n") );
168 wxSTD ifstream
std_file_input( "test_std.dat" );
170 std_file_input
>> si
;
171 tmp
.Printf( _T("Signed int: %d\n"), si
);
172 textCtrl
.WriteText( tmp
);
174 std_file_input
>> ui
;
175 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
176 textCtrl
.WriteText( tmp
);
179 tmp
.Printf( _T("Double: %f\n"), d
);
180 textCtrl
.WriteText( tmp
);
183 tmp
.Printf( _T("Float: %f\n"), f
);
184 textCtrl
.WriteText( tmp
);
186 // Why doesn't this work?
189 std_file_input
>> std_buf
;
190 tmp
.Printf( _T("String: %s\n"), std_buf
);
191 textCtrl
.WriteText( tmp
);
194 textCtrl
.WriteText( _T("\nReading from wxFileInputStream:\n") );
198 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
199 wxBufferedInputStream
buf_input( file_input
);
200 wxTextInputStream
text_input( file_input
);
203 tmp
.Printf( _T("Signed int: %d\n"), si
);
204 textCtrl
.WriteText( tmp
);
207 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
208 textCtrl
.WriteText( tmp
);
211 tmp
.Printf( _T("Double: %f\n"), d
);
212 textCtrl
.WriteText( tmp
);
215 tmp
.Printf( _T("Float: %f\n"), f
);
216 textCtrl
.WriteText( tmp
);
219 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
220 textCtrl
.WriteText( tmp
);
224 textCtrl
<< _T("\nTest for wxDataStream:\n\n");
226 textCtrl
.WriteText( _T("Writing to wxDataOutputStream:\n") );
228 file_output
.SeekO( 0 );
229 wxDataOutputStream
data_output( buf_output
);
231 wxInt16 i16
= (unsigned short)0xFFFF;
232 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
233 textCtrl
.WriteText( tmp
);
234 data_output
.Write16( i16
);
236 wxUint16 ui16
= 0xFFFF;
237 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
238 textCtrl
.WriteText( tmp
);
239 data_output
.Write16( ui16
);
241 d
= 2.01234567890123456789;
242 tmp
.Printf( _T("Double: %f\n"), d
);
243 textCtrl
.WriteText( tmp
);
244 data_output
.WriteDouble( d
);
247 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
248 textCtrl
.WriteText( tmp
);
249 data_output
.WriteString( str
);
253 textCtrl
.WriteText( _T("\nReading from wxDataInputStream:\n") );
255 file_input
.SeekI( 0 );
256 wxDataInputStream
data_input( buf_input
);
258 i16
= data_input
.Read16();
259 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
260 textCtrl
.WriteText( tmp
);
262 ui16
= data_input
.Read16();
263 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
264 textCtrl
.WriteText( tmp
);
266 d
= data_input
.ReadDouble();
267 tmp
.Printf( _T("Double: %f\n"), d
);
268 textCtrl
.WriteText( tmp
);
270 str
= data_input
.ReadString();
271 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
272 textCtrl
.WriteText( tmp
);
275 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
277 wxTextCtrl
& textCtrl
= * GetTextCtrl();
280 textCtrl
<< _T("\nTesting wxBufferedStream:\n\n");
284 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") );
286 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
287 wxBufferedOutputStream
buf_output( file_output
);
288 for (ch
= 0; ch
< 10; ch
++)
289 buf_output
.Write( &ch
, 1 );
292 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
293 for (ch2
= 0; ch2
< 10; ch2
++)
295 file_input
.Read( &ch
, 1 );
296 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
298 textCtrl
.WriteText( _T("\n\n\n") );
300 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") );
301 textCtrl
.WriteText( _T("seeking back to #3 and writing 0:\n\n") );
303 wxFileOutputStream
file_output2( wxString(_T("test_wx2.dat")) );
304 wxBufferedOutputStream
buf_output2( file_output2
);
305 for (ch
= 0; ch
< 10; ch
++)
306 buf_output2
.Write( &ch
, 1 );
307 buf_output2
.SeekO( 3 );
309 buf_output2
.Write( &ch
, 1 );
312 wxFileInputStream
file_input2( wxString(_T("test_wx2.dat")) );
313 for (ch2
= 0; ch2
< 10; ch2
++)
315 file_input2
.Read( &ch
, 1 );
316 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
318 textCtrl
.WriteText( _T("\n\n\n") );
320 // now append 2000 bytes to file (bigger than buffer)
321 buf_output2
.SeekO( 0, wxFromEnd
);
323 for (int i
= 0; i
< 2000; i
++)
324 buf_output2
.Write( &ch
, 1 );
327 textCtrl
.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") );
328 textCtrl
.WriteText( _T("seeking back to #3 and reading the 0:\n\n") );
330 wxFileInputStream
file_input3( wxString(_T("test_wx2.dat")) );
331 wxBufferedInputStream
buf_input3( file_input3
);
332 for (ch2
= 0; ch2
< 10; ch2
++)
334 buf_input3
.Read( &ch
, 1 );
335 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
337 for (int j
= 0; j
< 2000; j
++)
338 buf_input3
.Read( &ch
, 1 );
339 textCtrl
.WriteText( _T("\n") );
340 buf_input3
.SeekI( 3 );
341 buf_input3
.Read( &ch
, 1 );
342 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
343 textCtrl
.WriteText( _T("\n\n\n") );
347 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
349 wxTextCtrl
& textCtrl
= * GetTextCtrl();
352 textCtrl
<< _T("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n");
356 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
358 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
359 for (ch
= 0; ch
< 10; ch
++)
360 file_output
.Write( &ch
, 1 );
362 // Testing wxFileInputStream
364 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFileInputStream:\n\n") );
366 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
367 for (ch2
= 0; ch2
< 11; ch2
++)
369 file_input
.Read( &ch
, 1 );
370 textCtrl
.WriteText( _T("Value read: ") );
371 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
372 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
373 switch (file_input
.GetLastError())
375 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
376 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
377 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
378 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
379 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
382 textCtrl
.WriteText( _T("\n") );
384 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
385 file_input
.SeekI( 0 );
386 switch (file_input
.GetLastError())
388 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
389 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
390 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
391 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
392 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
394 textCtrl
.WriteText( _T("\n") );
396 file_input
.Read( &ch
, 1 );
397 textCtrl
.WriteText( _T("Value read: ") );
398 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
399 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
400 switch (file_input
.GetLastError())
402 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
403 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
404 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
405 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
406 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
408 textCtrl
.WriteText( _T("\n\n") );
411 // Testing wxFFileInputStream
413 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFFileInputStream:\n\n") );
415 wxFFileInputStream
ffile_input( wxString(_T("test_wx.dat")) );
416 for (ch2
= 0; ch2
< 11; ch2
++)
418 ffile_input
.Read( &ch
, 1 );
419 textCtrl
.WriteText( _T("Value read: ") );
420 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
421 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
422 switch (ffile_input
.GetLastError())
424 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
425 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
426 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
427 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
428 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
431 textCtrl
.WriteText( _T("\n") );
433 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
434 ffile_input
.SeekI( 0 );
435 switch (ffile_input
.GetLastError())
437 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
438 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
439 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
440 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
441 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
443 textCtrl
.WriteText( _T("\n") );
445 ffile_input
.Read( &ch
, 1 );
446 textCtrl
.WriteText( _T("Value read: ") );
447 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
448 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
449 switch (ffile_input
.GetLastError())
451 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
452 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
453 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
454 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
455 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
457 textCtrl
.WriteText( _T("\n\n") );
459 // Testing wxFFileInputStream
461 textCtrl
.WriteText( _T("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") );
463 wxFFileInputStream
ffile_input2( wxString(_T("test_wx.dat")) );
464 wxBufferedInputStream
buf_input( ffile_input2
);
465 for (ch2
= 0; ch2
< 11; ch2
++)
467 buf_input
.Read( &ch
, 1 );
468 textCtrl
.WriteText( _T("Value read: ") );
469 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
470 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
471 switch (buf_input
.GetLastError())
473 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
474 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
475 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
476 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
477 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
480 textCtrl
.WriteText( _T("\n") );
482 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
483 buf_input
.SeekI( 0 );
484 switch (buf_input
.GetLastError())
486 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
487 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
488 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
489 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
490 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
492 textCtrl
.WriteText( _T("\n") );
494 buf_input
.Read( &ch
, 1 );
495 textCtrl
.WriteText( _T("Value read: ") );
496 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
497 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
498 switch (buf_input
.GetLastError())
500 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
501 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
502 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
503 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
504 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
508 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
510 wxTextCtrl
& textCtrl
= * GetTextCtrl();
515 textCtrl
<< _T("\nTesting wxStreamBuffer:\n\n");
517 // bigger than buffer
518 textCtrl
.WriteText( _T("Writing 2000x 1 to wxFileOutputStream.\n\n") );
520 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
521 for (int i
= 0; i
< 2000; i
++)
524 file_output
.Write( &ch
, 1 );
527 textCtrl
.WriteText( _T("Opening with a buffered wxFileInputStream:\n\n") );
529 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
530 wxBufferedInputStream
buf_input( file_input
);
532 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
533 switch (buf_input
.GetLastError())
535 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
536 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
537 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
538 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
539 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
541 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
542 textCtrl
.WriteText( msg
);
543 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
544 textCtrl
.WriteText( msg
);
545 textCtrl
.WriteText( _T("\n\n") );
548 textCtrl
.WriteText( _T("Seeking to position 300:\n\n") );
550 buf_input
.SeekI( 300 );
552 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
553 switch (buf_input
.GetLastError())
555 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
556 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
557 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
558 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
559 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
561 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
562 textCtrl
.WriteText( msg
);
563 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
564 textCtrl
.WriteText( msg
);
565 textCtrl
.WriteText( _T("\n\n") );
570 textCtrl
.WriteText( _T("Reading 500 bytes:\n\n") );
572 buf_input
.Read( buf
, 500 );
574 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
575 switch (buf_input
.GetLastError())
577 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
578 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
579 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
580 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
581 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
583 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
584 textCtrl
.WriteText( msg
);
585 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
586 textCtrl
.WriteText( msg
);
587 textCtrl
.WriteText( _T("\n\n") );
589 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
591 buf_input
.Read( buf
, 500 );
593 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
594 switch (buf_input
.GetLastError())
596 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
597 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
598 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
599 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
600 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
602 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
603 textCtrl
.WriteText( msg
);
604 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
605 textCtrl
.WriteText( msg
);
606 textCtrl
.WriteText( _T("\n\n") );
608 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
610 buf_input
.Read( buf
, 500 );
612 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
613 switch (buf_input
.GetLastError())
615 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
616 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
617 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
618 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
619 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
621 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
622 textCtrl
.WriteText( msg
);
623 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
624 textCtrl
.WriteText( msg
);
625 textCtrl
.WriteText( _T("\n\n") );
627 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
629 buf_input
.Read( buf
, 500 );
631 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
632 switch (buf_input
.GetLastError())
634 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
635 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
636 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
637 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
638 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
640 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
641 textCtrl
.WriteText( msg
);
642 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
643 textCtrl
.WriteText( msg
);
644 textCtrl
.WriteText( _T("\n\n") );
647 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
649 wxTextCtrl
& textCtrl
= * GetTextCtrl();
652 textCtrl
<< _T("\nTesting wxFileInputStream's Peek():\n\n");
657 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
659 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
660 for (ch
= 0; ch
< 10; ch
++)
661 file_output
.Write( &ch
, 1 );
665 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
667 ch
= file_input
.Peek();
668 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
669 textCtrl
.WriteText( str
);
671 ch
= file_input
.GetC();
672 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
673 textCtrl
.WriteText( str
);
675 ch
= file_input
.Peek();
676 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
677 textCtrl
.WriteText( str
);
679 ch
= file_input
.GetC();
680 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
681 textCtrl
.WriteText( str
);
683 ch
= file_input
.Peek();
684 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
685 textCtrl
.WriteText( str
);
687 ch
= file_input
.GetC();
688 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
689 textCtrl
.WriteText( str
);
692 textCtrl
<< _T("\n\n\nTesting wxMemoryInputStream's Peek():\n\n");
694 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
695 wxMemoryInputStream
input( buf
, 10 );
698 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
699 textCtrl
.WriteText( str
);
702 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
703 textCtrl
.WriteText( str
);
706 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
707 textCtrl
.WriteText( str
);
710 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
711 textCtrl
.WriteText( str
);
714 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
715 textCtrl
.WriteText( str
);
718 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
719 textCtrl
.WriteText( str
);
722 void MyApp::DoStreamDemo6(wxCommandEvent
& WXUNUSED(event
))
724 wxTextCtrl
& textCtrl
= * GetTextCtrl();
727 textCtrl
.WriteText( _T("\nTesting Ungetch():\n\n") );
733 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
735 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
736 for (ch
= 0; ch
< 10; ch
++)
737 file_output
.Write( &ch
, 1 );
741 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
743 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
745 ch
= file_input
.GetC();
746 pos
= file_input
.TellI();
747 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
748 textCtrl
.WriteText( str
);
750 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
752 ch
= file_input
.GetC();
753 pos
= file_input
.TellI();
754 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
755 textCtrl
.WriteText( str
);
757 textCtrl
.WriteText( _T("Reading yet another char from wxFileInputStream:\n\n") );
759 ch
= file_input
.GetC();
760 pos
= file_input
.TellI();
761 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
762 textCtrl
.WriteText( str
);
764 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") );
766 file_input
.Ungetch( 5 );
767 pos
= file_input
.TellI();
768 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
769 textCtrl
.WriteText( str
);
771 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
773 ch
= file_input
.GetC();
774 pos
= file_input
.TellI();
775 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
776 textCtrl
.WriteText( str
);
778 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
780 ch
= file_input
.GetC();
781 pos
= file_input
.TellI();
782 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
783 textCtrl
.WriteText( str
);
785 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") );
787 file_input
.Ungetch( 5 );
788 pos
= file_input
.TellI();
789 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
790 textCtrl
.WriteText( str
);
792 textCtrl
.WriteText( _T("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") );
794 file_input
.SeekI( 3 );
796 ch
= file_input
.GetC();
797 pos
= file_input
.TellI();
798 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
799 textCtrl
.WriteText( str
);
802 void MyApp::DoStreamDemo7(wxCommandEvent
& WXUNUSED(event
))
804 wxTextCtrl
& textCtrl
= * GetTextCtrl();
807 textCtrl
.WriteText( _T("\nTesting Ungetch() in buffered input stream:\n\n") );
813 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
815 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
816 for (ch
= 0; ch
< 10; ch
++)
817 file_output
.Write( &ch
, 1 );
821 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") );
823 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
824 wxBufferedInputStream
buf_input( file_input
);
826 ch
= buf_input
.GetC();
827 pos
= buf_input
.TellI();
828 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
829 textCtrl
.WriteText( str
);
831 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
833 ch
= buf_input
.GetC();
834 pos
= buf_input
.TellI();
835 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
836 textCtrl
.WriteText( str
);
838 textCtrl
.WriteText( _T("Reading yet another char from wxBufferedInputStream:\n\n") );
840 ch
= buf_input
.GetC();
841 pos
= buf_input
.TellI();
842 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
843 textCtrl
.WriteText( str
);
845 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") );
847 buf_input
.Ungetch( 5 );
848 pos
= buf_input
.TellI();
849 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
850 textCtrl
.WriteText( str
);
852 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream:\n\n") );
854 ch
= buf_input
.GetC();
855 pos
= buf_input
.TellI();
856 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
857 textCtrl
.WriteText( str
);
859 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
861 ch
= buf_input
.GetC();
862 pos
= buf_input
.TellI();
863 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
864 textCtrl
.WriteText( str
);
866 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") );
868 buf_input
.Ungetch( 5 );
869 pos
= buf_input
.TellI();
870 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
871 textCtrl
.WriteText( str
);
873 textCtrl
.WriteText( _T("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") );
875 buf_input
.SeekI( 3 );
877 ch
= buf_input
.GetC();
878 pos
= buf_input
.TellI();
879 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
880 textCtrl
.WriteText( str
);
884 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
886 wxTextCtrl
& textCtrl
= * GetTextCtrl();
889 textCtrl
<< _T("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:");
892 str
= _T("Robert Röbling\n");
894 printf( "\n\nConversion with wxConvLocal:\n" );
895 wxConvCurrent
= &wxConvLocal
;
896 printf( (const char*) str
.mbc_str() );
897 #if defined(__WXGTK__)
898 printf( "\n\nConversion with wxConvGdk:\n" );
899 wxConvCurrent
= &wxConvGdk
;
900 printf( (const char*) str
.mbc_str() );
902 printf( "\n\nConversion with wxConvLibc:\n" );
903 wxConvCurrent
= &wxConvLibc
;
904 printf( (const char*) str
.mbc_str() );
909 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
911 static wxString s_defaultExt
= _T("xyz");
913 wxString ext
= wxGetTextFromUser(_T("Enter a file extension: "),
914 _T("MIME database test"),
920 // init MIME database if not done yet
921 if ( !m_mimeDatabase
)
923 m_mimeDatabase
= new wxMimeTypesManager
;
925 static const wxFileTypeInfo fallbacks
[] =
927 wxFileTypeInfo(_T("application/xyz"),
930 _T("The one and only XYZ format file"),
931 _T("xyz"), _T("123"), NULL
),
932 wxFileTypeInfo(_T("text/html"),
934 _T("lynx -dump %s | lpr"),
935 _T("HTML document (from fallback)"),
936 _T("htm"), _T("html"), NULL
),
938 // must terminate the table with this!
942 m_mimeDatabase
->AddFallbacks(fallbacks
);
945 wxTextCtrl
& textCtrl
= * GetTextCtrl();
947 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
950 textCtrl
<< _T("Unknown extension '") << ext
<< _T("'\n");
954 wxString type
, desc
, open
;
955 filetype
->GetMimeType(&type
);
956 filetype
->GetDescription(&desc
);
958 wxString filename
= _T("filename");
959 filename
<< _T(".") << ext
;
960 wxFileType::MessageParameters
params(filename
, type
);
961 filetype
->GetOpenCommand(&open
, params
);
963 textCtrl
<< _T("MIME information about extension '") << ext
<< _T("'\n")
964 << _T("\tMIME type: ") << ( !type
? wxT("unknown")
965 : type
.c_str() ) << '\n'
966 << _T("\tDescription: ") << ( !desc
? wxT("") : desc
.c_str() )
968 << _T("\tCommand to open: ") << ( !open
? wxT("no") : open
.c_str() )
974 //else: cancelled by user
977 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
979 wxTextCtrl
& textCtrl
= * GetTextCtrl();
982 textCtrl
<< _T("\nTest byte order macros:\n\n");
984 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
985 textCtrl
<< _T("This is a little endian system.\n\n");
987 textCtrl
<< _T("This is a big endian system.\n\n");
991 wxInt32 var
= 0xF1F2F3F4;
993 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
994 textCtrl
.WriteText( text
);
997 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
998 textCtrl
.WriteText( text
);
1001 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
1002 textCtrl
.WriteText( text
);
1005 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
1006 textCtrl
.WriteText( text
);
1009 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
1011 wxTextCtrl
& textCtrl
= * GetTextCtrl();
1013 wxVariant var1
= _T("String value");
1014 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1017 wxString str
= var1
.MakeString();
1020 textCtrl
<< _T("var1 = ") << var1
.GetReal() << _T("\n");
1022 // Implicit conversion
1026 textCtrl
<< _T("var1 = ") << var1
.GetLong() << _T("\n");
1028 // Implicit conversion
1031 // suppress compile warnings about unused variables
1037 wxStringList stringList
;
1038 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
1040 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1043 var1
.Append(wxVariant(1.2345));
1044 var1
.Append(wxVariant(_T("hello")));
1045 var1
.Append(wxVariant(54321L));
1047 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1049 size_t n
= var1
.GetCount();
1051 for (i
= (size_t) 0; i
< n
; i
++)
1053 textCtrl
<< _T("var1[") << (int) i
<< _T("] (type ") << var1
[i
].GetType() << _T(") = ") << var1
[i
].MakeString() << _T("\n");
1056 var1
= wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
)));
1057 textCtrl
<< _T("var1 = (wxfont)\"");
1058 wxFont
* font
= wxGetVariantCast(var1
,wxFont
);
1060 textCtrl
<< font
->GetNativeFontInfoDesc() << _T("\"\n");
1062 textCtrl
<< _T("(null)\"\n");
1066 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1067 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
1068 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
1071 // My frame constructor
1072 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
1073 const wxPoint
& pos
, const wxSize
& size
):
1074 wxFrame(parent
, -1, title
, pos
, size
)
1077 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1082 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1084 wxMessageDialog
dialog(this, _T("Tests various wxWindows types"),
1085 _T("About Types"), wxYES_NO
|wxCANCEL
);