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(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
29 #include "mondrian.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
= _T("test_wx.dat");
71 wxString file_name2
= wxString(_T("test_wx2.dat"));
75 // Create the main frame window
76 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Types Demo"),
77 wxPoint(50, 50), wxSize(450, 340));
80 frame
->SetIcon(wxICON(mondrian
));
83 wxMenu
*file_menu
= new wxMenu
;
85 file_menu
->Append(TYPES_ABOUT
, _T("&About"));
86 file_menu
->AppendSeparator();
87 file_menu
->Append(TYPES_QUIT
, _T("E&xit\tAlt-X"));
89 wxMenu
*test_menu
= new wxMenu
;
90 test_menu
->Append(TYPES_VARIANT
, _T("&Variant test"));
91 test_menu
->Append(TYPES_BYTEORDER
, _T("&Byteorder test"));
93 test_menu
->Append(TYPES_UNICODE
, _T("&Unicode test"));
94 #endif // wxUSE_UNICODE
95 test_menu
->Append(TYPES_STREAM
, _T("&Stream test"));
96 test_menu
->Append(TYPES_STREAM2
, _T("&Stream seek test"));
97 test_menu
->Append(TYPES_STREAM3
, _T("&Stream error test"));
98 test_menu
->Append(TYPES_STREAM4
, _T("&Stream buffer test"));
99 test_menu
->Append(TYPES_STREAM5
, _T("&Stream peek test"));
100 test_menu
->Append(TYPES_STREAM6
, _T("&Stream ungetch test"));
101 test_menu
->Append(TYPES_STREAM7
, _T("&Stream ungetch test for a buffered stream"));
102 test_menu
->AppendSeparator();
103 test_menu
->Append(TYPES_MIME
, _T("&MIME database test"));
105 wxMenuBar
*menu_bar
= new wxMenuBar
;
106 menu_bar
->Append(file_menu
, _T("&File"));
107 menu_bar
->Append(test_menu
, _T("&Tests"));
108 frame
->SetMenuBar(menu_bar
);
110 m_textCtrl
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
,
111 wxDefaultPosition
, 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( file_name
);
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
<< "\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
<< "\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
<< "\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
<< "\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
.ToAscii() << "\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
);
187 std_file_input
>> std_buf
;
188 str
= wxString::FromAscii(std_buf
);
189 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
190 textCtrl
.WriteText( tmp
);
192 textCtrl
.WriteText( _T("\nReading from wxFileInputStream:\n") );
196 wxFileInputStream
file_input( file_name
);
197 wxBufferedInputStream
buf_input( file_input
);
198 wxTextInputStream
text_input( file_input
);
201 tmp
.Printf( _T("Signed int: %d\n"), si
);
202 textCtrl
.WriteText( tmp
);
205 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
206 textCtrl
.WriteText( tmp
);
209 tmp
.Printf( _T("Double: %f\n"), d
);
210 textCtrl
.WriteText( tmp
);
213 tmp
.Printf( _T("Float: %f\n"), f
);
214 textCtrl
.WriteText( tmp
);
217 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
218 textCtrl
.WriteText( tmp
);
222 textCtrl
<< _T("\nTest for wxDataStream:\n\n");
224 textCtrl
.WriteText( _T("Writing to wxDataOutputStream:\n") );
226 file_output
.SeekO( 0 );
227 wxDataOutputStream
data_output( buf_output
);
229 wxInt16 i16
= (unsigned short)0xFFFF;
230 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
231 textCtrl
.WriteText( tmp
);
232 data_output
.Write16( i16
);
234 wxUint16 ui16
= 0xFFFF;
235 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
236 textCtrl
.WriteText( tmp
);
237 data_output
.Write16( ui16
);
239 d
= 2.01234567890123456789;
240 tmp
.Printf( _T("Double: %f\n"), d
);
241 textCtrl
.WriteText( tmp
);
242 data_output
.WriteDouble( d
);
245 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
246 textCtrl
.WriteText( tmp
);
247 data_output
.WriteString( str
);
251 textCtrl
.WriteText( _T("\nReading from wxDataInputStream:\n") );
253 file_input
.SeekI( 0 );
254 wxDataInputStream
data_input( buf_input
);
256 i16
= data_input
.Read16();
257 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
258 textCtrl
.WriteText( tmp
);
260 ui16
= data_input
.Read16();
261 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
262 textCtrl
.WriteText( tmp
);
264 d
= data_input
.ReadDouble();
265 tmp
.Printf( _T("Double: %f\n"), d
);
266 textCtrl
.WriteText( tmp
);
268 str
= data_input
.ReadString();
269 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
270 textCtrl
.WriteText( tmp
);
273 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
275 wxTextCtrl
& textCtrl
= * GetTextCtrl();
278 textCtrl
<< _T("\nTesting wxBufferedStream:\n\n");
282 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") );
284 wxFileOutputStream
file_output( file_name
);
285 wxBufferedOutputStream
buf_output( file_output
);
286 for (ch
= 0; ch
< 10; ch
++)
287 buf_output
.Write( &ch
, 1 );
290 wxFileInputStream
file_input( file_name
);
291 for (ch2
= 0; ch2
< 10; ch2
++)
293 file_input
.Read( &ch
, 1 );
294 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
296 textCtrl
.WriteText( _T("\n\n\n") );
298 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") );
299 textCtrl
.WriteText( _T("seeking back to #3 and writing 0:\n\n") );
301 wxFileOutputStream
file_output2( file_name2
);
302 wxBufferedOutputStream
buf_output2( file_output2
);
303 for (ch
= 0; ch
< 10; ch
++)
304 buf_output2
.Write( &ch
, 1 );
305 buf_output2
.SeekO( 3 );
307 buf_output2
.Write( &ch
, 1 );
310 wxFileInputStream
file_input2( file_name2
);
311 for (ch2
= 0; ch2
< 10; ch2
++)
313 file_input2
.Read( &ch
, 1 );
314 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
316 textCtrl
.WriteText( _T("\n\n\n") );
318 // now append 2000 bytes to file (bigger than buffer)
319 buf_output2
.SeekO( 0, wxFromEnd
);
321 for (int i
= 0; i
< 2000; i
++)
322 buf_output2
.Write( &ch
, 1 );
325 textCtrl
.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") );
326 textCtrl
.WriteText( _T("seeking back to #3 and reading the 0:\n\n") );
328 wxFileInputStream
file_input3( file_name2
);
329 wxBufferedInputStream
buf_input3( file_input3
);
330 for (ch2
= 0; ch2
< 10; ch2
++)
332 buf_input3
.Read( &ch
, 1 );
333 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
335 for (int j
= 0; j
< 2000; j
++)
336 buf_input3
.Read( &ch
, 1 );
337 textCtrl
.WriteText( _T("\n") );
338 buf_input3
.SeekI( 3 );
339 buf_input3
.Read( &ch
, 1 );
340 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
341 textCtrl
.WriteText( _T("\n\n\n") );
345 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
347 wxTextCtrl
& textCtrl
= * GetTextCtrl();
350 textCtrl
<< _T("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n");
354 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
356 wxFileOutputStream
file_output( file_name
);
357 for (ch
= 0; ch
< 10; ch
++)
358 file_output
.Write( &ch
, 1 );
360 // Testing wxFileInputStream
362 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFileInputStream:\n\n") );
364 wxFileInputStream
file_input( file_name
);
365 for (ch2
= 0; ch2
< 11; ch2
++)
367 file_input
.Read( &ch
, 1 );
368 textCtrl
.WriteText( _T("Value read: ") );
369 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
370 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
371 switch (file_input
.GetLastError())
373 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
374 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
375 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
376 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
377 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
380 textCtrl
.WriteText( _T("\n") );
382 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
383 file_input
.SeekI( 0 );
384 switch (file_input
.GetLastError())
386 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
387 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
388 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
389 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
390 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
392 textCtrl
.WriteText( _T("\n") );
394 file_input
.Read( &ch
, 1 );
395 textCtrl
.WriteText( _T("Value read: ") );
396 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
397 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
398 switch (file_input
.GetLastError())
400 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
401 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
402 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
403 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
404 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
406 textCtrl
.WriteText( _T("\n\n") );
409 // Testing wxFFileInputStream
411 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFFileInputStream:\n\n") );
413 wxFFileInputStream
ffile_input( file_name
);
414 for (ch2
= 0; ch2
< 11; ch2
++)
416 ffile_input
.Read( &ch
, 1 );
417 textCtrl
.WriteText( _T("Value read: ") );
418 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
419 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
420 switch (ffile_input
.GetLastError())
422 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
423 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
424 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
425 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
426 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
429 textCtrl
.WriteText( _T("\n") );
431 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
432 ffile_input
.SeekI( 0 );
433 switch (ffile_input
.GetLastError())
435 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
436 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
437 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
438 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
439 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
441 textCtrl
.WriteText( _T("\n") );
443 ffile_input
.Read( &ch
, 1 );
444 textCtrl
.WriteText( _T("Value read: ") );
445 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
446 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
447 switch (ffile_input
.GetLastError())
449 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
450 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
451 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
452 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
453 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
455 textCtrl
.WriteText( _T("\n\n") );
457 // Testing wxFFileInputStream
459 textCtrl
.WriteText( _T("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") );
461 wxFFileInputStream
ffile_input2( file_name
);
462 wxBufferedInputStream
buf_input( ffile_input2
);
463 for (ch2
= 0; ch2
< 11; ch2
++)
465 buf_input
.Read( &ch
, 1 );
466 textCtrl
.WriteText( _T("Value read: ") );
467 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
468 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
469 switch (buf_input
.GetLastError())
471 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
472 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
473 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
474 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
475 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
478 textCtrl
.WriteText( _T("\n") );
480 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
481 buf_input
.SeekI( 0 );
482 switch (buf_input
.GetLastError())
484 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
485 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
486 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
487 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
488 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
490 textCtrl
.WriteText( _T("\n") );
492 buf_input
.Read( &ch
, 1 );
493 textCtrl
.WriteText( _T("Value read: ") );
494 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
495 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
496 switch (buf_input
.GetLastError())
498 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
499 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
500 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
501 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
502 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
506 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
508 wxTextCtrl
& textCtrl
= * GetTextCtrl();
513 textCtrl
<< _T("\nTesting wxStreamBuffer:\n\n");
515 // bigger than buffer
516 textCtrl
.WriteText( _T("Writing 2000x 1 to wxFileOutputStream.\n\n") );
518 wxFileOutputStream
file_output( file_name
);
519 for (int i
= 0; i
< 2000; i
++)
522 file_output
.Write( &ch
, 1 );
525 textCtrl
.WriteText( _T("Opening with a buffered wxFileInputStream:\n\n") );
527 wxFileInputStream
file_input( file_name
);
528 wxBufferedInputStream
buf_input( file_input
);
530 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
531 switch (buf_input
.GetLastError())
533 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
534 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
535 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
536 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
537 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
539 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
540 textCtrl
.WriteText( msg
);
541 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
542 textCtrl
.WriteText( msg
);
543 textCtrl
.WriteText( _T("\n\n") );
546 textCtrl
.WriteText( _T("Seeking to position 300:\n\n") );
548 buf_input
.SeekI( 300 );
550 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
551 switch (buf_input
.GetLastError())
553 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
554 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
555 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
556 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
557 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
559 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
560 textCtrl
.WriteText( msg
);
561 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
562 textCtrl
.WriteText( msg
);
563 textCtrl
.WriteText( _T("\n\n") );
568 textCtrl
.WriteText( _T("Reading 500 bytes:\n\n") );
570 buf_input
.Read( buf
, 500 );
572 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
573 switch (buf_input
.GetLastError())
575 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
576 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
577 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
578 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
579 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
581 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
582 textCtrl
.WriteText( msg
);
583 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
584 textCtrl
.WriteText( msg
);
585 textCtrl
.WriteText( _T("\n\n") );
587 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
589 buf_input
.Read( buf
, 500 );
591 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
592 switch (buf_input
.GetLastError())
594 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
595 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
596 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
597 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
598 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
600 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
601 textCtrl
.WriteText( msg
);
602 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
603 textCtrl
.WriteText( msg
);
604 textCtrl
.WriteText( _T("\n\n") );
606 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
608 buf_input
.Read( buf
, 500 );
610 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
611 switch (buf_input
.GetLastError())
613 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
614 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
615 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
616 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
617 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
619 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
620 textCtrl
.WriteText( msg
);
621 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
622 textCtrl
.WriteText( msg
);
623 textCtrl
.WriteText( _T("\n\n") );
625 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
627 buf_input
.Read( buf
, 500 );
629 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
630 switch (buf_input
.GetLastError())
632 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
633 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
634 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
635 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
636 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
638 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
639 textCtrl
.WriteText( msg
);
640 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
641 textCtrl
.WriteText( msg
);
642 textCtrl
.WriteText( _T("\n\n") );
645 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
647 wxTextCtrl
& textCtrl
= * GetTextCtrl();
650 textCtrl
<< _T("\nTesting wxFileInputStream's Peek():\n\n");
655 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
657 wxFileOutputStream
file_output( file_name
);
658 for (ch
= 0; ch
< 10; ch
++)
659 file_output
.Write( &ch
, 1 );
663 wxFileInputStream
file_input( file_name
);
665 ch
= file_input
.Peek();
666 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
667 textCtrl
.WriteText( str
);
669 ch
= file_input
.GetC();
670 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
671 textCtrl
.WriteText( str
);
673 ch
= file_input
.Peek();
674 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
675 textCtrl
.WriteText( str
);
677 ch
= file_input
.GetC();
678 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
679 textCtrl
.WriteText( str
);
681 ch
= file_input
.Peek();
682 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
683 textCtrl
.WriteText( str
);
685 ch
= file_input
.GetC();
686 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
687 textCtrl
.WriteText( str
);
690 textCtrl
<< _T("\n\n\nTesting wxMemoryInputStream's Peek():\n\n");
692 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
693 wxMemoryInputStream
input( buf
, 10 );
696 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
697 textCtrl
.WriteText( str
);
700 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
701 textCtrl
.WriteText( str
);
704 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
705 textCtrl
.WriteText( str
);
708 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
709 textCtrl
.WriteText( str
);
712 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
713 textCtrl
.WriteText( str
);
716 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
717 textCtrl
.WriteText( str
);
720 void MyApp::DoStreamDemo6(wxCommandEvent
& WXUNUSED(event
))
722 wxTextCtrl
& textCtrl
= * GetTextCtrl();
725 textCtrl
.WriteText( _T("\nTesting Ungetch():\n\n") );
730 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
732 wxFileOutputStream
file_output( file_name
);
733 for (ch
= 0; ch
< 10; ch
++)
734 file_output
.Write( &ch
, 1 );
738 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
740 wxFileInputStream
file_input( file_name
);
742 ch
= file_input
.GetC();
743 size_t pos
= (size_t)file_input
.TellI();
744 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
745 textCtrl
.WriteText( str
);
747 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
749 ch
= file_input
.GetC();
750 pos
= (size_t)file_input
.TellI();
751 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
752 textCtrl
.WriteText( str
);
754 textCtrl
.WriteText( _T("Reading yet another char from wxFileInputStream:\n\n") );
756 ch
= file_input
.GetC();
757 pos
= (size_t)file_input
.TellI();
758 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
759 textCtrl
.WriteText( str
);
761 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") );
763 file_input
.Ungetch( 5 );
764 pos
= (size_t)file_input
.TellI();
765 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
766 textCtrl
.WriteText( str
);
768 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
770 ch
= file_input
.GetC();
771 pos
= (size_t)file_input
.TellI();
772 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
773 textCtrl
.WriteText( str
);
775 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
777 ch
= file_input
.GetC();
778 pos
= (size_t)file_input
.TellI();
779 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
780 textCtrl
.WriteText( str
);
782 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") );
784 file_input
.Ungetch( 5 );
785 pos
= (size_t)file_input
.TellI();
786 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
787 textCtrl
.WriteText( str
);
789 textCtrl
.WriteText( _T("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") );
791 file_input
.SeekI( 3 );
793 ch
= file_input
.GetC();
794 pos
= (size_t)file_input
.TellI();
795 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
796 textCtrl
.WriteText( str
);
799 void MyApp::DoStreamDemo7(wxCommandEvent
& WXUNUSED(event
))
801 wxTextCtrl
& textCtrl
= * GetTextCtrl();
804 textCtrl
.WriteText( _T("\nTesting Ungetch() in buffered input stream:\n\n") );
809 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
811 wxFileOutputStream
file_output( file_name
);
812 for (ch
= 0; ch
< 10; ch
++)
813 file_output
.Write( &ch
, 1 );
817 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") );
819 wxFileInputStream
file_input( file_name
);
820 wxBufferedInputStream
buf_input( file_input
);
822 ch
= buf_input
.GetC();
823 size_t pos
= (size_t)buf_input
.TellI();
824 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
825 textCtrl
.WriteText( str
);
827 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
829 ch
= buf_input
.GetC();
830 pos
= (size_t)buf_input
.TellI();
831 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
832 textCtrl
.WriteText( str
);
834 textCtrl
.WriteText( _T("Reading yet another char from wxBufferedInputStream:\n\n") );
836 ch
= buf_input
.GetC();
837 pos
= (size_t)buf_input
.TellI();
838 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
839 textCtrl
.WriteText( str
);
841 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") );
843 buf_input
.Ungetch( 5 );
844 pos
= (size_t)buf_input
.TellI();
845 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
846 textCtrl
.WriteText( str
);
848 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream:\n\n") );
850 ch
= buf_input
.GetC();
851 pos
= (size_t)buf_input
.TellI();
852 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
853 textCtrl
.WriteText( str
);
855 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
857 ch
= buf_input
.GetC();
858 pos
= (size_t)buf_input
.TellI();
859 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
860 textCtrl
.WriteText( str
);
862 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") );
864 buf_input
.Ungetch( 5 );
865 pos
= (size_t)buf_input
.TellI();
866 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
867 textCtrl
.WriteText( str
);
869 textCtrl
.WriteText( _T("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") );
871 buf_input
.SeekI( 3 );
873 ch
= buf_input
.GetC();
874 pos
= (size_t)buf_input
.TellI();
875 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
876 textCtrl
.WriteText( str
);
880 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
882 wxTextCtrl
& textCtrl
= * GetTextCtrl();
885 textCtrl
<< _T("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:");
888 str
= _T("Robert Röbling\n");
890 printf( "\n\nConversion with wxConvLocal:\n" );
891 wxConvCurrent
= &wxConvLocal
;
892 printf( (const char*) str
.mbc_str() );
893 printf( "\n\nConversion with wxConvLibc:\n" );
894 wxConvCurrent
= &wxConvLibc
;
895 printf( (const char*) str
.mbc_str() );
900 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
902 static wxString s_defaultExt
= _T("xyz");
904 wxString ext
= wxGetTextFromUser(_T("Enter a file extension: "),
905 _T("MIME database test"),
911 // init MIME database if not done yet
912 if ( !m_mimeDatabase
)
914 m_mimeDatabase
= new wxMimeTypesManager
;
916 static const wxFileTypeInfo fallbacks
[] =
918 wxFileTypeInfo(_T("application/xyz"),
921 _T("The one and only XYZ format file"),
922 _T("xyz"), _T("123"), NULL
),
923 wxFileTypeInfo(_T("text/html"),
925 _T("lynx -dump %s | lpr"),
926 _T("HTML document (from fallback)"),
927 _T("htm"), _T("html"), NULL
),
929 // must terminate the table with this!
933 m_mimeDatabase
->AddFallbacks(fallbacks
);
936 wxTextCtrl
& textCtrl
= * GetTextCtrl();
938 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
941 textCtrl
<< _T("Unknown extension '") << ext
<< _T("'\n");
945 wxString type
, desc
, open
;
946 filetype
->GetMimeType(&type
);
947 filetype
->GetDescription(&desc
);
949 wxString filename
= _T("filename");
950 filename
<< _T(".") << ext
;
951 wxFileType::MessageParameters
params(filename
, type
);
952 filetype
->GetOpenCommand(&open
, params
);
954 textCtrl
<< _T("MIME information about extension '") << ext
<< _T('\n')
955 << _T("\tMIME type: ") << ( !type
? wxT("unknown")
956 : type
.c_str() ) << _T('\n')
957 << _T("\tDescription: ") << ( !desc
? wxEmptyString
: desc
.c_str() )
959 << _T("\tCommand to open: ") << ( !open
? wxT("no") : open
.c_str() )
965 //else: cancelled by user
968 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
970 wxTextCtrl
& textCtrl
= * GetTextCtrl();
973 textCtrl
<< _T("\nTest byte order macros:\n\n");
975 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
976 textCtrl
<< _T("This is a little endian system.\n\n");
978 textCtrl
<< _T("This is a big endian system.\n\n");
983 wxInt32 var
= 0xF1F2F3F4;
984 text
= wxEmptyString
;
985 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
986 textCtrl
.WriteText( text
);
988 text
= wxEmptyString
;
989 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
990 textCtrl
.WriteText( text
);
992 text
= wxEmptyString
;
993 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
994 textCtrl
.WriteText( text
);
996 text
= wxEmptyString
;
997 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
998 textCtrl
.WriteText( text
);
1001 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
1003 wxTextCtrl
& textCtrl
= * GetTextCtrl();
1005 wxVariant var1
= _T("String value");
1006 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1009 wxString str
= var1
.MakeString();
1012 textCtrl
<< _T("var1 = ") << var1
.GetReal() << _T("\n");
1014 // Implicit conversion
1018 textCtrl
<< _T("var1 = ") << var1
.GetLong() << _T("\n");
1020 // Implicit conversion
1023 // suppress compile warnings about unused variables
1027 wxArrayString stringArray
;
1028 stringArray
.Add(_T("one")); stringArray
.Add(_T("two")); stringArray
.Add(_T("three"));
1030 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1033 var1
.Append(wxVariant(1.2345));
1034 var1
.Append(wxVariant(_T("hello")));
1035 var1
.Append(wxVariant(54321L));
1037 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1039 size_t n
= var1
.GetCount();
1041 for (i
= (size_t) 0; i
< n
; i
++)
1043 textCtrl
<< _T("var1[") << (int) i
<< _T("] (type ") << var1
[i
].GetType() << _T(") = ") << var1
[i
].MakeString() << _T("\n");
1046 var1
= wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
)));
1047 textCtrl
<< _T("var1 = (wxfont)\"");
1048 wxFont
* font
= wxGetVariantCast(var1
,wxFont
);
1051 textCtrl
<< font
->GetNativeFontInfoDesc() << _T("\"\n");
1055 textCtrl
<< _T("(null)\"\n");
1059 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1060 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
1061 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
1064 // My frame constructor
1065 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
1066 const wxPoint
& pos
, const wxSize
& size
)
1067 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
)
1070 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1075 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1077 wxMessageDialog
dialog(this, _T("Tests various wxWidgets types"),
1078 _T("About Types"), wxYES_NO
|wxCANCEL
);