1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWidgets 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
)
74 wxString file_name
= _T("test_wx.dat");
75 wxString file_name2
= wxString(_T("test_wx2.dat"));
79 // Create the main frame window
80 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Types Demo"),
81 wxPoint(50, 50), wxSize(450, 340));
84 frame
->SetIcon(wxICON(mondrian
));
87 wxMenu
*file_menu
= new wxMenu
;
89 file_menu
->Append(TYPES_ABOUT
, _T("&About"));
90 file_menu
->AppendSeparator();
91 file_menu
->Append(TYPES_QUIT
, _T("E&xit\tAlt-X"));
93 wxMenu
*test_menu
= new wxMenu
;
94 test_menu
->Append(TYPES_VARIANT
, _T("&Variant test"));
95 test_menu
->Append(TYPES_BYTEORDER
, _T("&Byteorder test"));
97 test_menu
->Append(TYPES_UNICODE
, _T("&Unicode test"));
98 #endif // wxUSE_UNICODE
99 test_menu
->Append(TYPES_STREAM
, _T("&Stream test"));
100 test_menu
->Append(TYPES_STREAM2
, _T("&Stream seek test"));
101 test_menu
->Append(TYPES_STREAM3
, _T("&Stream error test"));
102 test_menu
->Append(TYPES_STREAM4
, _T("&Stream buffer test"));
103 test_menu
->Append(TYPES_STREAM5
, _T("&Stream peek test"));
104 test_menu
->Append(TYPES_STREAM6
, _T("&Stream ungetch test"));
105 test_menu
->Append(TYPES_STREAM7
, _T("&Stream ungetch test for a buffered stream"));
106 test_menu
->AppendSeparator();
107 test_menu
->Append(TYPES_MIME
, _T("&MIME database test"));
109 wxMenuBar
*menu_bar
= new wxMenuBar
;
110 menu_bar
->Append(file_menu
, _T("&File"));
111 menu_bar
->Append(test_menu
, _T("&Tests"));
112 frame
->SetMenuBar(menu_bar
);
114 m_textCtrl
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
,
115 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
125 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
127 wxTextCtrl
& textCtrl
= * GetTextCtrl();
130 textCtrl
<< _T("\nTest fstream vs. wxFileStream:\n\n");
132 textCtrl
.WriteText( _T("Writing to ofstream and wxFileOutputStream:\n") );
134 wxSTD ofstream
std_file_output( "test_std.dat" );
135 wxFileOutputStream
file_output( file_name
);
136 wxBufferedOutputStream
buf_output( file_output
);
137 wxTextOutputStream
text_output( buf_output
);
140 signed int si
= 0xFFFFFFFF;
141 tmp
.Printf( _T("Signed int: %d\n"), si
);
142 textCtrl
.WriteText( tmp
);
143 text_output
<< si
<< _T("\n");
144 std_file_output
<< si
<< "\n";
146 unsigned int ui
= 0xFFFFFFFF;
147 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
148 textCtrl
.WriteText( tmp
);
149 text_output
<< ui
<< _T("\n");
150 std_file_output
<< ui
<< "\n";
152 double d
= 2.01234567890123456789;
153 tmp
.Printf( _T("Double: %f\n"), d
);
154 textCtrl
.WriteText( tmp
);
155 text_output
<< d
<< _T("\n");
156 std_file_output
<< d
<< "\n";
158 float f
= (float)0.00001;
159 tmp
.Printf( _T("Float: %f\n"), f
);
160 textCtrl
.WriteText( tmp
);
161 text_output
<< f
<< _T("\n");
162 std_file_output
<< f
<< "\n";
164 wxString
str( _T("Hello!") );
165 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
166 textCtrl
.WriteText( tmp
);
167 text_output
<< str
<< _T("\n");
168 std_file_output
<< str
.ToAscii() << "\n";
170 textCtrl
.WriteText( _T("\nReading from ifstream:\n") );
172 wxSTD ifstream
std_file_input( "test_std.dat" );
174 std_file_input
>> si
;
175 tmp
.Printf( _T("Signed int: %d\n"), si
);
176 textCtrl
.WriteText( tmp
);
178 std_file_input
>> ui
;
179 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
180 textCtrl
.WriteText( tmp
);
183 tmp
.Printf( _T("Double: %f\n"), d
);
184 textCtrl
.WriteText( tmp
);
187 tmp
.Printf( _T("Float: %f\n"), f
);
188 textCtrl
.WriteText( tmp
);
191 std_file_input
>> std_buf
;
192 str
= wxString::FromAscii(std_buf
);
193 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
194 textCtrl
.WriteText( tmp
);
196 textCtrl
.WriteText( _T("\nReading from wxFileInputStream:\n") );
200 wxFileInputStream
file_input( file_name
);
201 wxBufferedInputStream
buf_input( file_input
);
202 wxTextInputStream
text_input( file_input
);
205 tmp
.Printf( _T("Signed int: %d\n"), si
);
206 textCtrl
.WriteText( tmp
);
209 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
210 textCtrl
.WriteText( tmp
);
213 tmp
.Printf( _T("Double: %f\n"), d
);
214 textCtrl
.WriteText( tmp
);
217 tmp
.Printf( _T("Float: %f\n"), f
);
218 textCtrl
.WriteText( tmp
);
221 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
222 textCtrl
.WriteText( tmp
);
226 textCtrl
<< _T("\nTest for wxDataStream:\n\n");
228 textCtrl
.WriteText( _T("Writing to wxDataOutputStream:\n") );
230 file_output
.SeekO( 0 );
231 wxDataOutputStream
data_output( buf_output
);
233 wxInt16 i16
= (unsigned short)0xFFFF;
234 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
235 textCtrl
.WriteText( tmp
);
236 data_output
.Write16( i16
);
238 wxUint16 ui16
= 0xFFFF;
239 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
240 textCtrl
.WriteText( tmp
);
241 data_output
.Write16( ui16
);
243 d
= 2.01234567890123456789;
244 tmp
.Printf( _T("Double: %f\n"), d
);
245 textCtrl
.WriteText( tmp
);
246 data_output
.WriteDouble( d
);
249 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
250 textCtrl
.WriteText( tmp
);
251 data_output
.WriteString( str
);
255 textCtrl
.WriteText( _T("\nReading from wxDataInputStream:\n") );
257 file_input
.SeekI( 0 );
258 wxDataInputStream
data_input( buf_input
);
260 i16
= data_input
.Read16();
261 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
262 textCtrl
.WriteText( tmp
);
264 ui16
= data_input
.Read16();
265 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
266 textCtrl
.WriteText( tmp
);
268 d
= data_input
.ReadDouble();
269 tmp
.Printf( _T("Double: %f\n"), d
);
270 textCtrl
.WriteText( tmp
);
272 str
= data_input
.ReadString();
273 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
274 textCtrl
.WriteText( tmp
);
277 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
279 wxTextCtrl
& textCtrl
= * GetTextCtrl();
282 textCtrl
<< _T("\nTesting wxBufferedStream:\n\n");
286 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") );
288 wxFileOutputStream
file_output( file_name
);
289 wxBufferedOutputStream
buf_output( file_output
);
290 for (ch
= 0; ch
< 10; ch
++)
291 buf_output
.Write( &ch
, 1 );
294 wxFileInputStream
file_input( file_name
);
295 for (ch2
= 0; ch2
< 10; ch2
++)
297 file_input
.Read( &ch
, 1 );
298 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
300 textCtrl
.WriteText( _T("\n\n\n") );
302 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") );
303 textCtrl
.WriteText( _T("seeking back to #3 and writing 0:\n\n") );
305 wxFileOutputStream
file_output2( file_name2
);
306 wxBufferedOutputStream
buf_output2( file_output2
);
307 for (ch
= 0; ch
< 10; ch
++)
308 buf_output2
.Write( &ch
, 1 );
309 buf_output2
.SeekO( 3 );
311 buf_output2
.Write( &ch
, 1 );
314 wxFileInputStream
file_input2( file_name2
);
315 for (ch2
= 0; ch2
< 10; ch2
++)
317 file_input2
.Read( &ch
, 1 );
318 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
320 textCtrl
.WriteText( _T("\n\n\n") );
322 // now append 2000 bytes to file (bigger than buffer)
323 buf_output2
.SeekO( 0, wxFromEnd
);
325 for (int i
= 0; i
< 2000; i
++)
326 buf_output2
.Write( &ch
, 1 );
329 textCtrl
.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") );
330 textCtrl
.WriteText( _T("seeking back to #3 and reading the 0:\n\n") );
332 wxFileInputStream
file_input3( file_name2
);
333 wxBufferedInputStream
buf_input3( file_input3
);
334 for (ch2
= 0; ch2
< 10; ch2
++)
336 buf_input3
.Read( &ch
, 1 );
337 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
339 for (int j
= 0; j
< 2000; j
++)
340 buf_input3
.Read( &ch
, 1 );
341 textCtrl
.WriteText( _T("\n") );
342 buf_input3
.SeekI( 3 );
343 buf_input3
.Read( &ch
, 1 );
344 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
345 textCtrl
.WriteText( _T("\n\n\n") );
349 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
351 wxTextCtrl
& textCtrl
= * GetTextCtrl();
354 textCtrl
<< _T("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n");
358 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
360 wxFileOutputStream
file_output( file_name
);
361 for (ch
= 0; ch
< 10; ch
++)
362 file_output
.Write( &ch
, 1 );
364 // Testing wxFileInputStream
366 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFileInputStream:\n\n") );
368 wxFileInputStream
file_input( file_name
);
369 for (ch2
= 0; ch2
< 11; ch2
++)
371 file_input
.Read( &ch
, 1 );
372 textCtrl
.WriteText( _T("Value read: ") );
373 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
374 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
375 switch (file_input
.GetLastError())
377 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
378 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
379 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
380 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
381 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
384 textCtrl
.WriteText( _T("\n") );
386 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
387 file_input
.SeekI( 0 );
388 switch (file_input
.GetLastError())
390 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
391 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
392 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
393 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
394 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
396 textCtrl
.WriteText( _T("\n") );
398 file_input
.Read( &ch
, 1 );
399 textCtrl
.WriteText( _T("Value read: ") );
400 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
401 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
402 switch (file_input
.GetLastError())
404 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
405 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
406 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
407 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
408 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
410 textCtrl
.WriteText( _T("\n\n") );
413 // Testing wxFFileInputStream
415 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFFileInputStream:\n\n") );
417 wxFFileInputStream
ffile_input( file_name
);
418 for (ch2
= 0; ch2
< 11; ch2
++)
420 ffile_input
.Read( &ch
, 1 );
421 textCtrl
.WriteText( _T("Value read: ") );
422 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
423 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
424 switch (ffile_input
.GetLastError())
426 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
427 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
428 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
429 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
430 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
433 textCtrl
.WriteText( _T("\n") );
435 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
436 ffile_input
.SeekI( 0 );
437 switch (ffile_input
.GetLastError())
439 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
440 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
441 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
442 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
443 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
445 textCtrl
.WriteText( _T("\n") );
447 ffile_input
.Read( &ch
, 1 );
448 textCtrl
.WriteText( _T("Value read: ") );
449 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
450 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
451 switch (ffile_input
.GetLastError())
453 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
454 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
455 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
456 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
457 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
459 textCtrl
.WriteText( _T("\n\n") );
461 // Testing wxFFileInputStream
463 textCtrl
.WriteText( _T("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") );
465 wxFFileInputStream
ffile_input2( file_name
);
466 wxBufferedInputStream
buf_input( ffile_input2
);
467 for (ch2
= 0; ch2
< 11; ch2
++)
469 buf_input
.Read( &ch
, 1 );
470 textCtrl
.WriteText( _T("Value read: ") );
471 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
472 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
473 switch (buf_input
.GetLastError())
475 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
476 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
477 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
478 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
479 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
482 textCtrl
.WriteText( _T("\n") );
484 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
485 buf_input
.SeekI( 0 );
486 switch (buf_input
.GetLastError())
488 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
489 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
490 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
491 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
492 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
494 textCtrl
.WriteText( _T("\n") );
496 buf_input
.Read( &ch
, 1 );
497 textCtrl
.WriteText( _T("Value read: ") );
498 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
499 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
500 switch (buf_input
.GetLastError())
502 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
503 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
504 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
505 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
506 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
510 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
512 wxTextCtrl
& textCtrl
= * GetTextCtrl();
517 textCtrl
<< _T("\nTesting wxStreamBuffer:\n\n");
519 // bigger than buffer
520 textCtrl
.WriteText( _T("Writing 2000x 1 to wxFileOutputStream.\n\n") );
522 wxFileOutputStream
file_output( file_name
);
523 for (int i
= 0; i
< 2000; i
++)
526 file_output
.Write( &ch
, 1 );
529 textCtrl
.WriteText( _T("Opening with a buffered wxFileInputStream:\n\n") );
531 wxFileInputStream
file_input( file_name
);
532 wxBufferedInputStream
buf_input( file_input
);
534 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
535 switch (buf_input
.GetLastError())
537 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
538 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
539 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
540 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
541 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
543 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
544 textCtrl
.WriteText( msg
);
545 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
546 textCtrl
.WriteText( msg
);
547 textCtrl
.WriteText( _T("\n\n") );
550 textCtrl
.WriteText( _T("Seeking to position 300:\n\n") );
552 buf_input
.SeekI( 300 );
554 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
555 switch (buf_input
.GetLastError())
557 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
558 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
559 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
560 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
561 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
563 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
564 textCtrl
.WriteText( msg
);
565 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
566 textCtrl
.WriteText( msg
);
567 textCtrl
.WriteText( _T("\n\n") );
572 textCtrl
.WriteText( _T("Reading 500 bytes:\n\n") );
574 buf_input
.Read( buf
, 500 );
576 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
577 switch (buf_input
.GetLastError())
579 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
580 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
581 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
582 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
583 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
585 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
586 textCtrl
.WriteText( msg
);
587 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
588 textCtrl
.WriteText( msg
);
589 textCtrl
.WriteText( _T("\n\n") );
591 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
593 buf_input
.Read( buf
, 500 );
595 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
596 switch (buf_input
.GetLastError())
598 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
599 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
600 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
601 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
602 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
604 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
605 textCtrl
.WriteText( msg
);
606 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
607 textCtrl
.WriteText( msg
);
608 textCtrl
.WriteText( _T("\n\n") );
610 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
612 buf_input
.Read( buf
, 500 );
614 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
615 switch (buf_input
.GetLastError())
617 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
618 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
619 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
620 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
621 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
623 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
624 textCtrl
.WriteText( msg
);
625 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
626 textCtrl
.WriteText( msg
);
627 textCtrl
.WriteText( _T("\n\n") );
629 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
631 buf_input
.Read( buf
, 500 );
633 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
634 switch (buf_input
.GetLastError())
636 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
637 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
638 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
639 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
640 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
642 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
643 textCtrl
.WriteText( msg
);
644 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
645 textCtrl
.WriteText( msg
);
646 textCtrl
.WriteText( _T("\n\n") );
649 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
651 wxTextCtrl
& textCtrl
= * GetTextCtrl();
654 textCtrl
<< _T("\nTesting wxFileInputStream's Peek():\n\n");
659 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
661 wxFileOutputStream
file_output( file_name
);
662 for (ch
= 0; ch
< 10; ch
++)
663 file_output
.Write( &ch
, 1 );
667 wxFileInputStream
file_input( file_name
);
669 ch
= file_input
.Peek();
670 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
671 textCtrl
.WriteText( str
);
673 ch
= file_input
.GetC();
674 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
675 textCtrl
.WriteText( str
);
677 ch
= file_input
.Peek();
678 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
679 textCtrl
.WriteText( str
);
681 ch
= file_input
.GetC();
682 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
683 textCtrl
.WriteText( str
);
685 ch
= file_input
.Peek();
686 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
687 textCtrl
.WriteText( str
);
689 ch
= file_input
.GetC();
690 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
691 textCtrl
.WriteText( str
);
694 textCtrl
<< _T("\n\n\nTesting wxMemoryInputStream's Peek():\n\n");
696 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
697 wxMemoryInputStream
input( buf
, 10 );
700 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
701 textCtrl
.WriteText( str
);
704 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
705 textCtrl
.WriteText( str
);
708 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
709 textCtrl
.WriteText( str
);
712 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
713 textCtrl
.WriteText( str
);
716 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
717 textCtrl
.WriteText( str
);
720 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
721 textCtrl
.WriteText( str
);
724 void MyApp::DoStreamDemo6(wxCommandEvent
& WXUNUSED(event
))
726 wxTextCtrl
& textCtrl
= * GetTextCtrl();
729 textCtrl
.WriteText( _T("\nTesting Ungetch():\n\n") );
734 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
736 wxFileOutputStream
file_output( file_name
);
737 for (ch
= 0; ch
< 10; ch
++)
738 file_output
.Write( &ch
, 1 );
742 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
744 wxFileInputStream
file_input( file_name
);
746 ch
= file_input
.GetC();
747 size_t pos
= (size_t)file_input
.TellI();
748 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
749 textCtrl
.WriteText( str
);
751 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
753 ch
= file_input
.GetC();
754 pos
= (size_t)file_input
.TellI();
755 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
756 textCtrl
.WriteText( str
);
758 textCtrl
.WriteText( _T("Reading yet another char from wxFileInputStream:\n\n") );
760 ch
= file_input
.GetC();
761 pos
= (size_t)file_input
.TellI();
762 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
763 textCtrl
.WriteText( str
);
765 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") );
767 file_input
.Ungetch( 5 );
768 pos
= (size_t)file_input
.TellI();
769 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
770 textCtrl
.WriteText( str
);
772 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
774 ch
= file_input
.GetC();
775 pos
= (size_t)file_input
.TellI();
776 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
777 textCtrl
.WriteText( str
);
779 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
781 ch
= file_input
.GetC();
782 pos
= (size_t)file_input
.TellI();
783 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
784 textCtrl
.WriteText( str
);
786 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") );
788 file_input
.Ungetch( 5 );
789 pos
= (size_t)file_input
.TellI();
790 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
791 textCtrl
.WriteText( str
);
793 textCtrl
.WriteText( _T("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") );
795 file_input
.SeekI( 3 );
797 ch
= file_input
.GetC();
798 pos
= (size_t)file_input
.TellI();
799 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
800 textCtrl
.WriteText( str
);
803 void MyApp::DoStreamDemo7(wxCommandEvent
& WXUNUSED(event
))
805 wxTextCtrl
& textCtrl
= * GetTextCtrl();
808 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( file_name
);
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( file_name
);
824 wxBufferedInputStream
buf_input( file_input
);
826 ch
= buf_input
.GetC();
827 size_t pos
= (size_t)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
= (size_t)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
= (size_t)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
= (size_t)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
= (size_t)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
= (size_t)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
= (size_t)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
= (size_t)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 printf( "\n\nConversion with wxConvLibc:\n" );
898 wxConvCurrent
= &wxConvLibc
;
899 printf( (const char*) str
.mbc_str() );
904 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
906 static wxString s_defaultExt
= _T("xyz");
908 wxString ext
= wxGetTextFromUser(_T("Enter a file extension: "),
909 _T("MIME database test"),
915 // init MIME database if not done yet
916 if ( !m_mimeDatabase
)
918 m_mimeDatabase
= new wxMimeTypesManager
;
920 static const wxFileTypeInfo fallbacks
[] =
922 wxFileTypeInfo(_T("application/xyz"),
925 _T("The one and only XYZ format file"),
926 _T("xyz"), _T("123"), NULL
),
927 wxFileTypeInfo(_T("text/html"),
929 _T("lynx -dump %s | lpr"),
930 _T("HTML document (from fallback)"),
931 _T("htm"), _T("html"), NULL
),
933 // must terminate the table with this!
937 m_mimeDatabase
->AddFallbacks(fallbacks
);
940 wxTextCtrl
& textCtrl
= * GetTextCtrl();
942 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
945 textCtrl
<< _T("Unknown extension '") << ext
<< _T("'\n");
949 wxString type
, desc
, open
;
950 filetype
->GetMimeType(&type
);
951 filetype
->GetDescription(&desc
);
953 wxString filename
= _T("filename");
954 filename
<< _T(".") << ext
;
955 wxFileType::MessageParameters
params(filename
, type
);
956 filetype
->GetOpenCommand(&open
, params
);
958 textCtrl
<< _T("MIME information about extension '") << ext
<< _T('\n')
959 << _T("\tMIME type: ") << ( !type
? wxT("unknown")
960 : type
.c_str() ) << _T('\n')
961 << _T("\tDescription: ") << ( !desc
? wxEmptyString
: desc
.c_str() )
963 << _T("\tCommand to open: ") << ( !open
? wxT("no") : open
.c_str() )
969 //else: cancelled by user
972 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
974 wxTextCtrl
& textCtrl
= * GetTextCtrl();
977 textCtrl
<< _T("\nTest byte order macros:\n\n");
979 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
980 textCtrl
<< _T("This is a little endian system.\n\n");
982 textCtrl
<< _T("This is a big endian system.\n\n");
987 wxInt32 var
= 0xF1F2F3F4;
988 text
= wxEmptyString
;
989 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
990 textCtrl
.WriteText( text
);
992 text
= wxEmptyString
;
993 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
994 textCtrl
.WriteText( text
);
996 text
= wxEmptyString
;
997 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
998 textCtrl
.WriteText( text
);
1000 text
= wxEmptyString
;
1001 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
1002 textCtrl
.WriteText( text
);
1005 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
1007 wxTextCtrl
& textCtrl
= * GetTextCtrl();
1009 wxVariant var1
= _T("String value");
1010 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1013 wxString str
= var1
.MakeString();
1016 textCtrl
<< _T("var1 = ") << var1
.GetReal() << _T("\n");
1018 // Implicit conversion
1022 textCtrl
<< _T("var1 = ") << var1
.GetLong() << _T("\n");
1024 // Implicit conversion
1027 // suppress compile warnings about unused variables
1031 wxArrayString stringArray
;
1032 stringArray
.Add(_T("one")); stringArray
.Add(_T("two")); stringArray
.Add(_T("three"));
1034 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1037 var1
.Append(wxVariant(1.2345));
1038 var1
.Append(wxVariant(_T("hello")));
1039 var1
.Append(wxVariant(54321L));
1041 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1043 size_t n
= var1
.GetCount();
1045 for (i
= (size_t) 0; i
< n
; i
++)
1047 textCtrl
<< _T("var1[") << (int) i
<< _T("] (type ") << var1
[i
].GetType() << _T(") = ") << var1
[i
].MakeString() << _T("\n");
1050 var1
= wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
)));
1051 textCtrl
<< _T("var1 = (wxfont)\"");
1052 wxFont
* font
= wxGetVariantCast(var1
,wxFont
);
1055 textCtrl
<< font
->GetNativeFontInfoDesc() << _T("\"\n");
1059 textCtrl
<< _T("(null)\"\n");
1063 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1064 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
1065 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
1068 // My frame constructor
1069 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
1070 const wxPoint
& pos
, const wxSize
& size
)
1071 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
)
1074 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1079 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1081 wxMessageDialog
dialog(this, _T("Tests various wxWidgets types"),
1082 _T("About Types"), wxYES_NO
|wxCANCEL
);