1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/variant.h"
24 #include "wx/mimetype.h"
28 #if !defined(__WXMSW__) && !defined(__WXPM__)
29 #include "../sample.xpm"
36 #include "wx/ioswrap.h"
44 #include "wx/wfstream.h"
45 #include "wx/datstrm.h"
46 #include "wx/txtstrm.h"
47 #include "wx/mstream.h"
49 // Create a new application object
52 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
54 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
55 EVT_MENU(TYPES_VARIANT
, MyApp::DoVariantDemo
)
56 EVT_MENU(TYPES_BYTEORDER
, MyApp::DoByteOrderDemo
)
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
)
70 wxString file_name
= wxT("test_wx.dat");
71 wxString file_name2
= wxString(wxT("test_wx2.dat"));
75 if ( !wxApp::OnInit() )
78 // Create the main frame window
79 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, wxT("wxWidgets Types Demo"),
80 wxPoint(50, 50), wxSize(450, 340));
83 frame
->SetIcon(wxICON(sample
));
86 wxMenu
*file_menu
= new wxMenu
;
88 file_menu
->Append(TYPES_ABOUT
, wxT("&About"));
89 file_menu
->AppendSeparator();
90 file_menu
->Append(TYPES_QUIT
, wxT("E&xit\tAlt-X"));
92 wxMenu
*test_menu
= new wxMenu
;
93 test_menu
->Append(TYPES_VARIANT
, wxT("&Variant test"));
94 test_menu
->Append(TYPES_BYTEORDER
, wxT("&Byteorder test"));
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"));
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
);
113 m_textCtrl
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
,
114 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
124 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
126 wxTextCtrl
& textCtrl
= * GetTextCtrl();
129 textCtrl
<< wxT("\nTest fstream vs. wxFileStream:\n\n");
131 textCtrl
.WriteText( wxT("Writing to ofstream and wxFileOutputStream:\n") );
133 wxSTD ofstream
std_file_output( "test_std.dat" );
134 wxFileOutputStream
file_output( file_name
);
135 wxBufferedOutputStream
buf_output( file_output
);
136 wxTextOutputStream
text_output( buf_output
);
139 signed int si
= 0xFFFFFFFF;
140 tmp
.Printf( wxT("Signed int: %d\n"), si
);
141 textCtrl
.WriteText( tmp
);
142 text_output
<< si
<< wxT("\n");
143 std_file_output
<< si
<< "\n";
145 unsigned int ui
= 0xFFFFFFFF;
146 tmp
.Printf( wxT("Unsigned int: %u\n"), ui
);
147 textCtrl
.WriteText( tmp
);
148 text_output
<< ui
<< wxT("\n");
149 std_file_output
<< ui
<< "\n";
151 double d
= 2.01234567890123456789;
152 tmp
.Printf( wxT("Double: %f\n"), d
);
153 textCtrl
.WriteText( tmp
);
154 text_output
<< d
<< wxT("\n");
155 std_file_output
<< d
<< "\n";
157 float f
= (float)0.00001;
158 tmp
.Printf( wxT("Float: %f\n"), f
);
159 textCtrl
.WriteText( tmp
);
160 text_output
<< f
<< wxT("\n");
161 std_file_output
<< f
<< "\n";
163 wxString
str( wxT("Hello!") );
164 tmp
.Printf( wxT("String: %s\n"), str
.c_str() );
165 textCtrl
.WriteText( tmp
);
166 text_output
<< str
<< wxT("\n");
167 std_file_output
<< str
.ToAscii() << "\n";
169 std_file_output
.close();
171 textCtrl
.WriteText( wxT("\nReading from ifstream:\n") );
173 wxSTD ifstream
std_file_input( "test_std.dat" );
175 std_file_input
>> si
;
176 tmp
.Printf( wxT("Signed int: %d\n"), si
);
177 textCtrl
.WriteText( tmp
);
179 std_file_input
>> ui
;
180 tmp
.Printf( wxT("Unsigned int: %u\n"), ui
);
181 textCtrl
.WriteText( tmp
);
184 tmp
.Printf( wxT("Double: %f\n"), d
);
185 textCtrl
.WriteText( tmp
);
188 tmp
.Printf( wxT("Float: %f\n"), f
);
189 textCtrl
.WriteText( tmp
);
192 std_file_input
>> std_buf
;
193 str
= wxString::FromAscii(std_buf
);
194 tmp
.Printf( wxT("String: %s\n"), str
.c_str() );
195 textCtrl
.WriteText( tmp
);
197 textCtrl
.WriteText( wxT("\nReading from wxFileInputStream:\n") );
201 wxFileInputStream
file_input( file_name
);
202 wxBufferedInputStream
buf_input( file_input
);
203 wxTextInputStream
text_input( file_input
);
206 tmp
.Printf( wxT("Signed int: %d\n"), si
);
207 textCtrl
.WriteText( tmp
);
210 tmp
.Printf( wxT("Unsigned int: %u\n"), ui
);
211 textCtrl
.WriteText( tmp
);
214 tmp
.Printf( wxT("Double: %f\n"), d
);
215 textCtrl
.WriteText( tmp
);
218 tmp
.Printf( wxT("Float: %f\n"), f
);
219 textCtrl
.WriteText( tmp
);
222 tmp
.Printf( wxT("String: %s\n"), str
.c_str() );
223 textCtrl
.WriteText( tmp
);
227 textCtrl
<< wxT("\nTest for wxDataStream:\n\n");
229 textCtrl
.WriteText( wxT("Writing to wxDataOutputStream:\n") );
231 file_output
.SeekO( 0 );
232 wxDataOutputStream
data_output( buf_output
);
234 wxInt16 i16
= (unsigned short)0xFFFF;
235 tmp
.Printf( wxT("Signed int16: %d\n"), (int)i16
);
236 textCtrl
.WriteText( tmp
);
237 data_output
.Write16( i16
);
239 wxUint16 ui16
= 0xFFFF;
240 tmp
.Printf( wxT("Unsigned int16: %u\n"), (unsigned int) ui16
);
241 textCtrl
.WriteText( tmp
);
242 data_output
.Write16( ui16
);
244 d
= 2.01234567890123456789;
245 tmp
.Printf( wxT("Double: %f\n"), d
);
246 textCtrl
.WriteText( tmp
);
247 data_output
.WriteDouble( d
);
250 tmp
.Printf( wxT("String: %s\n"), str
.c_str() );
251 textCtrl
.WriteText( tmp
);
252 data_output
.WriteString( str
);
256 textCtrl
.WriteText( wxT("\nReading from wxDataInputStream:\n") );
258 file_input
.SeekI( 0 );
259 wxDataInputStream
data_input( buf_input
);
261 i16
= data_input
.Read16();
262 tmp
.Printf( wxT("Signed int16: %d\n"), (int)i16
);
263 textCtrl
.WriteText( tmp
);
265 ui16
= data_input
.Read16();
266 tmp
.Printf( wxT("Unsigned int16: %u\n"), (unsigned int) ui16
);
267 textCtrl
.WriteText( tmp
);
269 d
= data_input
.ReadDouble();
270 tmp
.Printf( wxT("Double: %f\n"), d
);
271 textCtrl
.WriteText( tmp
);
273 str
= data_input
.ReadString();
274 tmp
.Printf( wxT("String: %s\n"), str
.c_str() );
275 textCtrl
.WriteText( tmp
);
278 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
280 wxTextCtrl
& textCtrl
= * GetTextCtrl();
283 textCtrl
<< wxT("\nTesting wxBufferedStream:\n\n");
287 textCtrl
.WriteText( wxT("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") );
289 wxFileOutputStream
file_output( file_name
);
290 wxBufferedOutputStream
buf_output( file_output
);
291 for (ch
= 0; ch
< 10; ch
++)
292 buf_output
.Write( &ch
, 1 );
295 wxFileInputStream
file_input( file_name
);
296 for (ch2
= 0; ch2
< 10; ch2
++)
298 file_input
.Read( &ch
, 1 );
299 textCtrl
.WriteText( (wxChar
)(ch
+ wxT('0')) );
301 textCtrl
.WriteText( wxT("\n\n\n") );
303 textCtrl
.WriteText( wxT("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") );
304 textCtrl
.WriteText( wxT("seeking back to #3 and writing 0:\n\n") );
306 wxFileOutputStream
file_output2( file_name2
);
307 wxBufferedOutputStream
buf_output2( file_output2
);
308 for (ch
= 0; ch
< 10; ch
++)
309 buf_output2
.Write( &ch
, 1 );
310 buf_output2
.SeekO( 3 );
312 buf_output2
.Write( &ch
, 1 );
315 wxFileInputStream
file_input2( file_name2
);
316 for (ch2
= 0; ch2
< 10; ch2
++)
318 file_input2
.Read( &ch
, 1 );
319 textCtrl
.WriteText( (wxChar
)(ch
+ wxT('0')) );
321 textCtrl
.WriteText( wxT("\n\n\n") );
323 // now append 2000 bytes to file (bigger than buffer)
324 buf_output2
.SeekO( 0, wxFromEnd
);
326 for (int i
= 0; i
< 2000; i
++)
327 buf_output2
.Write( &ch
, 1 );
330 textCtrl
.WriteText( wxT("Reading number 0 to 9 from buffered wxFileInputStream, then\n") );
331 textCtrl
.WriteText( wxT("seeking back to #3 and reading the 0:\n\n") );
333 wxFileInputStream
file_input3( file_name2
);
334 wxBufferedInputStream
buf_input3( file_input3
);
335 for (ch2
= 0; ch2
< 10; ch2
++)
337 buf_input3
.Read( &ch
, 1 );
338 textCtrl
.WriteText( (wxChar
)(ch
+ wxT('0')) );
340 for (int j
= 0; j
< 2000; j
++)
341 buf_input3
.Read( &ch
, 1 );
342 textCtrl
.WriteText( wxT("\n") );
343 buf_input3
.SeekI( 3 );
344 buf_input3
.Read( &ch
, 1 );
345 textCtrl
.WriteText( (wxChar
)(ch
+ wxT('0')) );
346 textCtrl
.WriteText( wxT("\n\n\n") );
350 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
352 wxTextCtrl
& textCtrl
= * GetTextCtrl();
355 textCtrl
<< wxT("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n");
359 textCtrl
.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
361 wxFileOutputStream
file_output( file_name
);
362 for (ch
= 0; ch
< 10; ch
++)
363 file_output
.Write( &ch
, 1 );
365 // Testing wxFileInputStream
367 textCtrl
.WriteText( wxT("Reading 0 to 10 to wxFileInputStream:\n\n") );
369 wxFileInputStream
file_input( file_name
);
370 for (ch2
= 0; ch2
< 11; ch2
++)
372 file_input
.Read( &ch
, 1 );
373 textCtrl
.WriteText( wxT("Value read: ") );
374 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
375 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
376 switch (file_input
.GetLastError())
378 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
379 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
380 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
381 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
382 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
385 textCtrl
.WriteText( wxT("\n") );
387 textCtrl
.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") );
388 file_input
.SeekI( 0 );
389 switch (file_input
.GetLastError())
391 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
392 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
393 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
394 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
395 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
397 textCtrl
.WriteText( wxT("\n") );
399 file_input
.Read( &ch
, 1 );
400 textCtrl
.WriteText( wxT("Value read: ") );
401 textCtrl
.WriteText( (wxChar
)(ch
+ wxT('0')) );
402 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
403 switch (file_input
.GetLastError())
405 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
406 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
407 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
408 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
409 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
411 textCtrl
.WriteText( wxT("\n\n") );
414 // Testing wxFFileInputStream
416 textCtrl
.WriteText( wxT("Reading 0 to 10 to wxFFileInputStream:\n\n") );
418 wxFFileInputStream
ffile_input( file_name
);
419 for (ch2
= 0; ch2
< 11; ch2
++)
421 ffile_input
.Read( &ch
, 1 );
422 textCtrl
.WriteText( wxT("Value read: ") );
423 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
424 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
425 switch (ffile_input
.GetLastError())
427 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
428 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
429 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
430 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
431 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
434 textCtrl
.WriteText( wxT("\n") );
436 textCtrl
.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") );
437 ffile_input
.SeekI( 0 );
438 switch (ffile_input
.GetLastError())
440 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
441 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
442 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
443 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
444 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
446 textCtrl
.WriteText( wxT("\n") );
448 ffile_input
.Read( &ch
, 1 );
449 textCtrl
.WriteText( wxT("Value read: ") );
450 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
451 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
452 switch (ffile_input
.GetLastError())
454 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
455 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
456 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
457 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
458 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
460 textCtrl
.WriteText( wxT("\n\n") );
462 // Testing wxFFileInputStream
464 textCtrl
.WriteText( wxT("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") );
466 wxFFileInputStream
ffile_input2( file_name
);
467 wxBufferedInputStream
buf_input( ffile_input2
);
468 for (ch2
= 0; ch2
< 11; ch2
++)
470 buf_input
.Read( &ch
, 1 );
471 textCtrl
.WriteText( wxT("Value read: ") );
472 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
473 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
474 switch (buf_input
.GetLastError())
476 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
477 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
478 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
479 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
480 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
483 textCtrl
.WriteText( wxT("\n") );
485 textCtrl
.WriteText( wxT("Seeking to 0; stream.GetLastError() returns: ") );
486 buf_input
.SeekI( 0 );
487 switch (buf_input
.GetLastError())
489 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
490 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
491 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
492 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
493 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
495 textCtrl
.WriteText( wxT("\n") );
497 buf_input
.Read( &ch
, 1 );
498 textCtrl
.WriteText( wxT("Value read: ") );
499 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
500 textCtrl
.WriteText( wxT("; stream.GetLastError() returns: ") );
501 switch (buf_input
.GetLastError())
503 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
504 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
505 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
506 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
507 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
511 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
513 wxTextCtrl
& textCtrl
= * GetTextCtrl();
518 textCtrl
<< wxT("\nTesting wxStreamBuffer:\n\n");
520 // bigger than buffer
521 textCtrl
.WriteText( wxT("Writing 2000x 1 to wxFileOutputStream.\n\n") );
523 wxFileOutputStream
file_output( file_name
);
524 for (int i
= 0; i
< 2000; i
++)
527 file_output
.Write( &ch
, 1 );
530 textCtrl
.WriteText( wxT("Opening with a buffered wxFileInputStream:\n\n") );
532 wxFileInputStream
file_input( file_name
);
533 wxBufferedInputStream
buf_input( file_input
);
535 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
536 switch (buf_input
.GetLastError())
538 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
539 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
540 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
541 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
542 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
544 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
545 textCtrl
.WriteText( msg
);
546 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
547 textCtrl
.WriteText( msg
);
548 textCtrl
.WriteText( wxT("\n\n") );
551 textCtrl
.WriteText( wxT("Seeking to position 300:\n\n") );
553 buf_input
.SeekI( 300 );
555 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
556 switch (buf_input
.GetLastError())
558 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
559 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
560 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
561 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
562 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
564 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
565 textCtrl
.WriteText( msg
);
566 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
567 textCtrl
.WriteText( msg
);
568 textCtrl
.WriteText( wxT("\n\n") );
573 textCtrl
.WriteText( wxT("Reading 500 bytes:\n\n") );
575 buf_input
.Read( buf
, 500 );
577 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
578 switch (buf_input
.GetLastError())
580 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
581 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
582 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
583 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
584 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
586 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
587 textCtrl
.WriteText( msg
);
588 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
589 textCtrl
.WriteText( msg
);
590 textCtrl
.WriteText( wxT("\n\n") );
592 textCtrl
.WriteText( wxT("Reading another 500 bytes:\n\n") );
594 buf_input
.Read( buf
, 500 );
596 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
597 switch (buf_input
.GetLastError())
599 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
600 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
601 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
602 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
603 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
605 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
606 textCtrl
.WriteText( msg
);
607 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
608 textCtrl
.WriteText( msg
);
609 textCtrl
.WriteText( wxT("\n\n") );
611 textCtrl
.WriteText( wxT("Reading another 500 bytes:\n\n") );
613 buf_input
.Read( buf
, 500 );
615 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
616 switch (buf_input
.GetLastError())
618 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
619 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
620 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
621 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
622 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
624 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
625 textCtrl
.WriteText( msg
);
626 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
627 textCtrl
.WriteText( msg
);
628 textCtrl
.WriteText( wxT("\n\n") );
630 textCtrl
.WriteText( wxT("Reading another 500 bytes:\n\n") );
632 buf_input
.Read( buf
, 500 );
634 textCtrl
.WriteText( wxT("wxBufferedInputStream.GetLastError() returns: ") );
635 switch (buf_input
.GetLastError())
637 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_NO_ERROR\n") ); break;
638 case wxSTREAM_EOF
: textCtrl
.WriteText( wxT("wxSTREAM_EOF\n") ); break;
639 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_READ_ERROR\n") ); break;
640 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( wxT("wxSTREAM_WRITE_ERROR\n") ); break;
641 default: textCtrl
.WriteText( wxT("Huh?\n") ); break;
643 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
644 textCtrl
.WriteText( msg
);
645 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
646 textCtrl
.WriteText( msg
);
647 textCtrl
.WriteText( wxT("\n\n") );
650 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
652 wxTextCtrl
& textCtrl
= * GetTextCtrl();
655 textCtrl
<< wxT("\nTesting wxFileInputStream's Peek():\n\n");
660 textCtrl
.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
662 wxFileOutputStream
file_output( file_name
);
663 for (ch
= 0; ch
< 10; ch
++)
664 file_output
.Write( &ch
, 1 );
668 wxFileInputStream
file_input( file_name
);
670 ch
= file_input
.Peek();
671 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
672 textCtrl
.WriteText( str
);
674 ch
= file_input
.GetC();
675 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
676 textCtrl
.WriteText( str
);
678 ch
= file_input
.Peek();
679 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
680 textCtrl
.WriteText( str
);
682 ch
= file_input
.GetC();
683 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
684 textCtrl
.WriteText( str
);
686 ch
= file_input
.Peek();
687 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
688 textCtrl
.WriteText( str
);
690 ch
= file_input
.GetC();
691 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
692 textCtrl
.WriteText( str
);
695 textCtrl
<< wxT("\n\n\nTesting wxMemoryInputStream's Peek():\n\n");
697 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
698 wxMemoryInputStream
input( buf
, 10 );
701 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
702 textCtrl
.WriteText( str
);
705 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
706 textCtrl
.WriteText( str
);
709 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
710 textCtrl
.WriteText( str
);
713 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
714 textCtrl
.WriteText( str
);
717 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
718 textCtrl
.WriteText( str
);
721 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
722 textCtrl
.WriteText( str
);
725 void MyApp::DoStreamDemo6(wxCommandEvent
& WXUNUSED(event
))
727 wxTextCtrl
& textCtrl
= * GetTextCtrl();
730 textCtrl
.WriteText( wxT("\nTesting Ungetch():\n\n") );
735 textCtrl
.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
737 wxFileOutputStream
file_output( file_name
);
738 for (ch
= 0; ch
< 10; ch
++)
739 file_output
.Write( &ch
, 1 );
743 textCtrl
.WriteText( wxT("Reading char from wxFileInputStream:\n\n") );
745 wxFileInputStream
file_input( file_name
);
747 ch
= file_input
.GetC();
748 size_t pos
= (size_t)file_input
.TellI();
749 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
750 textCtrl
.WriteText( str
);
752 textCtrl
.WriteText( wxT("Reading another char from wxFileInputStream:\n\n") );
754 ch
= file_input
.GetC();
755 pos
= (size_t)file_input
.TellI();
756 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
757 textCtrl
.WriteText( str
);
759 textCtrl
.WriteText( wxT("Reading yet another char from wxFileInputStream:\n\n") );
761 ch
= file_input
.GetC();
762 pos
= (size_t)file_input
.TellI();
763 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
764 textCtrl
.WriteText( str
);
766 textCtrl
.WriteText( wxT("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") );
768 file_input
.Ungetch( 5 );
769 pos
= (size_t)file_input
.TellI();
770 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
771 textCtrl
.WriteText( str
);
773 textCtrl
.WriteText( wxT("Reading char from wxFileInputStream:\n\n") );
775 ch
= file_input
.GetC();
776 pos
= (size_t)file_input
.TellI();
777 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
778 textCtrl
.WriteText( str
);
780 textCtrl
.WriteText( wxT("Reading another char from wxFileInputStream:\n\n") );
782 ch
= file_input
.GetC();
783 pos
= (size_t)file_input
.TellI();
784 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
785 textCtrl
.WriteText( str
);
787 textCtrl
.WriteText( wxT("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") );
789 file_input
.Ungetch( 5 );
790 pos
= (size_t)file_input
.TellI();
791 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
792 textCtrl
.WriteText( str
);
794 textCtrl
.WriteText( wxT("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") );
796 file_input
.SeekI( 3 );
798 ch
= file_input
.GetC();
799 pos
= (size_t)file_input
.TellI();
800 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
801 textCtrl
.WriteText( str
);
804 void MyApp::DoStreamDemo7(wxCommandEvent
& WXUNUSED(event
))
806 wxTextCtrl
& textCtrl
= * GetTextCtrl();
809 textCtrl
.WriteText( wxT("\nTesting Ungetch() in buffered input stream:\n\n") );
814 textCtrl
.WriteText( wxT("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
816 wxFileOutputStream
file_output( file_name
);
817 for (ch
= 0; ch
< 10; ch
++)
818 file_output
.Write( &ch
, 1 );
822 textCtrl
.WriteText( wxT("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") );
824 wxFileInputStream
file_input( file_name
);
825 wxBufferedInputStream
buf_input( file_input
);
827 ch
= buf_input
.GetC();
828 size_t pos
= (size_t)buf_input
.TellI();
829 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
830 textCtrl
.WriteText( str
);
832 textCtrl
.WriteText( wxT("Reading another char from wxBufferedInputStream:\n\n") );
834 ch
= buf_input
.GetC();
835 pos
= (size_t)buf_input
.TellI();
836 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
837 textCtrl
.WriteText( str
);
839 textCtrl
.WriteText( wxT("Reading yet another char from wxBufferedInputStream:\n\n") );
841 ch
= buf_input
.GetC();
842 pos
= (size_t)buf_input
.TellI();
843 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
844 textCtrl
.WriteText( str
);
846 textCtrl
.WriteText( wxT("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") );
848 buf_input
.Ungetch( 5 );
849 pos
= (size_t)buf_input
.TellI();
850 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
851 textCtrl
.WriteText( str
);
853 textCtrl
.WriteText( wxT("Reading char from wxBufferedInputStream:\n\n") );
855 ch
= buf_input
.GetC();
856 pos
= (size_t)buf_input
.TellI();
857 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
858 textCtrl
.WriteText( str
);
860 textCtrl
.WriteText( wxT("Reading another char from wxBufferedInputStream:\n\n") );
862 ch
= buf_input
.GetC();
863 pos
= (size_t)buf_input
.TellI();
864 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
865 textCtrl
.WriteText( str
);
867 textCtrl
.WriteText( wxT("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") );
869 buf_input
.Ungetch( 5 );
870 pos
= (size_t)buf_input
.TellI();
871 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
872 textCtrl
.WriteText( str
);
874 textCtrl
.WriteText( wxT("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") );
876 buf_input
.SeekI( 3 );
878 ch
= buf_input
.GetC();
879 pos
= (size_t)buf_input
.TellI();
880 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
881 textCtrl
.WriteText( str
);
885 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
887 wxTextCtrl
& textCtrl
= * GetTextCtrl();
890 textCtrl
<< wxT("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:");
893 str
= wxT("Robert R\366bling\n");
895 printf( "\n\nConversion with wxConvLocal:\n" );
896 wxConvCurrent
= &wxConvLocal
;
897 printf( (const char*) str
.mbc_str() );
898 printf( "\n\nConversion with wxConvLibc:\n" );
899 wxConvCurrent
= &wxConvLibc
;
900 printf( (const char*) str
.mbc_str() );
905 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
907 static wxString s_defaultExt
= wxT("xyz");
909 wxString ext
= wxGetTextFromUser(wxT("Enter a file extension: "),
910 wxT("MIME database test"),
916 // init MIME database if not done yet
917 if ( !m_mimeDatabase
)
919 m_mimeDatabase
= new wxMimeTypesManager
;
921 static const wxFileTypeInfo fallbacks
[] =
923 wxFileTypeInfo(wxT("application/xyz"),
926 wxT("The one and only XYZ format file"),
927 wxT("xyz"), wxT("123"), wxNullPtr
),
928 wxFileTypeInfo(wxT("text/html"),
930 wxT("lynx -dump %s | lpr"),
931 wxT("HTML document (from fallback)"),
932 wxT("htm"), wxT("html"), wxNullPtr
),
934 // must terminate the table with this!
938 m_mimeDatabase
->AddFallbacks(fallbacks
);
941 wxTextCtrl
& textCtrl
= * GetTextCtrl();
943 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
946 textCtrl
<< wxT("Unknown extension '") << ext
<< wxT("'\n");
950 wxString type
, desc
, open
;
951 filetype
->GetMimeType(&type
);
952 filetype
->GetDescription(&desc
);
954 wxString filename
= wxT("filename");
955 filename
<< wxT(".") << ext
;
956 wxFileType::MessageParameters
params(filename
, type
);
957 filetype
->GetOpenCommand(&open
, params
);
959 textCtrl
<< wxT("MIME information about extension '") << ext
<< wxT('\n')
960 << wxT("\tMIME type: ") << ( !type
? wxString("unknown") : type
) << wxT('\n')
961 << wxT("\tDescription: ") << ( !desc
? wxString(wxEmptyString
) : desc
)
963 << wxT("\tCommand to open: ") << ( !open
? wxString("no") : open
)
969 //else: cancelled by user
972 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
974 wxTextCtrl
& textCtrl
= * GetTextCtrl();
977 textCtrl
<< wxT("\nTest byte order macros:\n\n");
979 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
980 textCtrl
<< wxT("This is a little endian system.\n\n");
982 textCtrl
<< wxT("This is a big endian system.\n\n");
987 wxInt32 var
= 0xF1F2F3F4;
988 text
= wxEmptyString
;
989 text
.Printf( wxT("Value of wxInt32 is now: %#x.\n\n"), var
);
990 textCtrl
.WriteText( text
);
992 text
= wxEmptyString
;
993 text
.Printf( wxT("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
994 textCtrl
.WriteText( text
);
996 text
= wxEmptyString
;
997 text
.Printf( wxT("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( wxT("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
= wxT("String value");
1010 textCtrl
<< wxT("var1 = ") << var1
.MakeString() << wxT("\n");
1013 wxString str
= var1
.MakeString();
1016 textCtrl
<< wxT("var1 = ") << var1
.GetReal() << wxT("\n");
1018 // Implicit conversion
1022 textCtrl
<< wxT("var1 = ") << var1
.GetLong() << wxT("\n");
1024 // Implicit conversion
1027 // suppress compile warnings about unused variables
1031 wxArrayString stringArray
;
1032 stringArray
.Add(wxT("one")); stringArray
.Add(wxT("two")); stringArray
.Add(wxT("three"));
1034 textCtrl
<< wxT("var1 = ") << var1
.MakeString() << wxT("\n");
1037 var1
.Append(wxVariant(1.2345));
1038 var1
.Append(wxVariant(wxT("hello")));
1039 var1
.Append(wxVariant(54321L));
1041 textCtrl
<< wxT("var1 = ") << var1
.MakeString() << wxT("\n");
1043 size_t n
= var1
.GetCount();
1045 for (i
= (size_t) 0; i
< n
; i
++)
1047 textCtrl
<< wxT("var1[") << (int) i
<< wxT("] (type ") << var1
[i
].GetType() << wxT(") = ") << var1
[i
].MakeString() << wxT("\n");
1050 var1
= wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
)));
1051 textCtrl
<< wxT("var1 = (wxfont)\"");
1052 wxFont
* font
= wxGetVariantCast(var1
,wxFont
);
1055 textCtrl
<< font
->GetNativeFontInfoDesc() << wxT("\"\n");
1059 textCtrl
<< wxT("(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, wxT("Tests various wxWidgets types"),
1082 wxT("About Types"), wxYES_NO
|wxCANCEL
);