]>
git.saurik.com Git - wxWidgets.git/blob - samples/typetest/typetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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"
29 #include "wx/variant.h"
30 #include "wx/mimetype.h"
34 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
35 #include "mondrian.xpm"
42 #include "wx/ioswrap.h"
50 #include "wx/wfstream.h"
51 #include "wx/datstrm.h"
52 #include "wx/txtstrm.h"
53 #include "wx/mstream.h"
55 // Create a new application object
58 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
60 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
62 EVT_MENU(TYPES_DATE
, MyApp::DoDateDemo
)
63 EVT_MENU(TYPES_TIME
, MyApp::DoTimeDemo
)
64 #endif // wxUSE_TIMEDATE
65 EVT_MENU(TYPES_VARIANT
, MyApp::DoVariantDemo
)
66 EVT_MENU(TYPES_BYTEORDER
, MyApp::DoByteOrderDemo
)
68 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
69 #endif // wxUSE_UNICODE
70 EVT_MENU(TYPES_STREAM
, MyApp::DoStreamDemo
)
71 EVT_MENU(TYPES_STREAM2
, MyApp::DoStreamDemo2
)
72 EVT_MENU(TYPES_STREAM3
, MyApp::DoStreamDemo3
)
73 EVT_MENU(TYPES_STREAM4
, MyApp::DoStreamDemo4
)
74 EVT_MENU(TYPES_STREAM5
, MyApp::DoStreamDemo5
)
75 EVT_MENU(TYPES_MIME
, MyApp::DoMIMEDemo
)
80 // Create the main frame window
81 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxWindows Types Demo",
82 wxPoint(50, 50), wxSize(450, 340));
85 frame
->SetIcon(wxICON(mondrian
));
88 wxMenu
*file_menu
= new wxMenu
;
90 file_menu
->Append(TYPES_ABOUT
, "&About");
91 file_menu
->AppendSeparator();
92 file_menu
->Append(TYPES_QUIT
, "E&xit\tAlt-X");
94 wxMenu
*test_menu
= new wxMenu
;
96 test_menu
->Append(TYPES_DATE
, "&Date test");
97 #endif // wxUSE_TIMEDATE
98 test_menu
->Append(TYPES_TIME
, "&Time test");
99 test_menu
->Append(TYPES_VARIANT
, "&Variant test");
100 test_menu
->Append(TYPES_BYTEORDER
, "&Byteorder test");
102 test_menu
->Append(TYPES_UNICODE
, "&Unicode test");
103 #endif // wxUSE_UNICODE
104 test_menu
->Append(TYPES_STREAM
, "&Stream test");
105 test_menu
->Append(TYPES_STREAM2
, "&Stream seek test");
106 test_menu
->Append(TYPES_STREAM3
, "&Stream error test");
107 test_menu
->Append(TYPES_STREAM4
, "&Stream buffer test");
108 test_menu
->Append(TYPES_STREAM5
, "&Stream peek test");
109 test_menu
->AppendSeparator();
110 test_menu
->Append(TYPES_MIME
, "&MIME database test");
112 wxMenuBar
*menu_bar
= new wxMenuBar
;
113 menu_bar
->Append(file_menu
, "&File");
114 menu_bar
->Append(test_menu
, "&Tests");
115 frame
->SetMenuBar(menu_bar
);
117 m_textCtrl
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxDefaultSize
, wxTE_MULTILINE
);
127 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
129 wxTextCtrl
& textCtrl
= * GetTextCtrl();
132 textCtrl
<< _T("\nTest fstream vs. wxFileStream:\n\n");
134 textCtrl
.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
136 wxSTD ofstream
std_file_output( "test_std.dat" );
137 wxFileOutputStream
file_output( wxString("test_wx.dat") );
138 wxBufferedOutputStream
buf_output( file_output
);
139 wxTextOutputStream
text_output( buf_output
);
142 signed int si
= 0xFFFFFFFF;
143 tmp
.Printf( _T("Signed int: %d\n"), si
);
144 textCtrl
.WriteText( tmp
);
145 text_output
<< si
<< "\n";
146 std_file_output
<< si
<< "\n";
148 unsigned int ui
= 0xFFFFFFFF;
149 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
150 textCtrl
.WriteText( tmp
);
151 text_output
<< ui
<< "\n";
152 std_file_output
<< ui
<< "\n";
154 double d
= 2.01234567890123456789;
155 tmp
.Printf( _T("Double: %f\n"), d
);
156 textCtrl
.WriteText( tmp
);
157 text_output
<< d
<< "\n";
158 std_file_output
<< d
<< "\n";
160 float f
= (float)0.00001;
161 tmp
.Printf( _T("Float: %f\n"), f
);
162 textCtrl
.WriteText( tmp
);
163 text_output
<< f
<< "\n";
164 std_file_output
<< f
<< "\n";
166 wxString
str( _T("Hello!") );
167 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
168 textCtrl
.WriteText( tmp
);
169 text_output
<< str
<< "\n";
170 std_file_output
<< str
.c_str() << "\n";
172 textCtrl
.WriteText( "\nReading from ifstream:\n" );
174 wxSTD ifstream
std_file_input( "test_std.dat" );
176 std_file_input
>> si
;
177 tmp
.Printf( _T("Signed int: %d\n"), si
);
178 textCtrl
.WriteText( tmp
);
180 std_file_input
>> ui
;
181 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
182 textCtrl
.WriteText( tmp
);
185 tmp
.Printf( _T("Double: %f\n"), d
);
186 textCtrl
.WriteText( tmp
);
189 tmp
.Printf( _T("Float: %f\n"), f
);
190 textCtrl
.WriteText( tmp
);
192 // Why doesn't this work?
195 std_file_input
>> std_buf
;
196 tmp
.Printf( _T("String: %s\n"), std_buf
);
197 textCtrl
.WriteText( tmp
);
200 textCtrl
.WriteText( "\nReading from wxFileInputStream:\n" );
204 wxFileInputStream
file_input( wxString("test_wx.dat") );
205 wxBufferedInputStream
buf_input( file_input
);
206 wxTextInputStream
text_input( file_input
);
209 tmp
.Printf( _T("Signed int: %d\n"), si
);
210 textCtrl
.WriteText( tmp
);
213 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
214 textCtrl
.WriteText( tmp
);
217 tmp
.Printf( _T("Double: %f\n"), d
);
218 textCtrl
.WriteText( tmp
);
221 tmp
.Printf( _T("Float: %f\n"), f
);
222 textCtrl
.WriteText( tmp
);
225 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
226 textCtrl
.WriteText( tmp
);
230 textCtrl
<< "\nTest for wxDataStream:\n\n";
232 textCtrl
.WriteText( "Writing to wxDataOutputStream:\n" );
234 file_output
.SeekO( 0 );
235 wxDataOutputStream
data_output( buf_output
);
237 wxInt16 i16
= (unsigned short)0xFFFF;
238 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
239 textCtrl
.WriteText( tmp
);
240 data_output
.Write16( i16
);
242 wxUint16 ui16
= 0xFFFF;
243 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
244 textCtrl
.WriteText( tmp
);
245 data_output
.Write16( ui16
);
247 d
= 2.01234567890123456789;
248 tmp
.Printf( _T("Double: %f\n"), d
);
249 textCtrl
.WriteText( tmp
);
250 data_output
.WriteDouble( d
);
253 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
254 textCtrl
.WriteText( tmp
);
255 data_output
.WriteString( str
);
259 textCtrl
.WriteText( "\nReading from wxDataInputStream:\n" );
261 file_input
.SeekI( 0 );
262 wxDataInputStream
data_input( buf_input
);
264 i16
= data_input
.Read16();
265 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
266 textCtrl
.WriteText( tmp
);
268 ui16
= data_input
.Read16();
269 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
270 textCtrl
.WriteText( tmp
);
272 d
= data_input
.ReadDouble();
273 tmp
.Printf( _T("Double: %f\n"), d
);
274 textCtrl
.WriteText( tmp
);
276 str
= data_input
.ReadString();
277 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
278 textCtrl
.WriteText( tmp
);
281 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
283 wxTextCtrl
& textCtrl
= * GetTextCtrl();
286 textCtrl
<< _T("\nTesting wxBufferedStream:\n\n");
290 textCtrl
.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" );
292 wxFileOutputStream
file_output( wxString("test_wx.dat") );
293 wxBufferedOutputStream
buf_output( file_output
);
294 for (ch
= 0; ch
< 10; ch
++)
295 buf_output
.Write( &ch
, 1 );
298 wxFileInputStream
file_input( wxString("test_wx.dat") );
299 for (ch2
= 0; ch2
< 10; ch2
++)
301 file_input
.Read( &ch
, 1 );
302 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
304 textCtrl
.WriteText( "\n\n\n" );
306 textCtrl
.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" );
307 textCtrl
.WriteText( "seeking back to #3 and writing 3:\n\n" );
309 wxFileOutputStream
file_output2( wxString("test_wx2.dat") );
310 wxBufferedOutputStream
buf_output2( file_output2
);
311 for (ch
= 0; ch
< 10; ch
++)
312 buf_output2
.Write( &ch
, 1 );
313 buf_output2
.SeekO( 3 );
315 buf_output2
.Write( &ch
, 1 );
318 wxFileInputStream
file_input2( wxString("test_wx2.dat") );
319 for (ch2
= 0; ch2
< 10; ch2
++)
321 file_input2
.Read( &ch
, 1 );
322 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
324 textCtrl
.WriteText( "\n\n\n" );
326 // now append 2000 bytes to file (bigger than buffer)
327 buf_output2
.SeekO( 0, wxFromEnd
);
329 for (int i
= 0; i
< 2000; i
++)
330 buf_output2
.Write( &ch
, 1 );
333 textCtrl
.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" );
334 textCtrl
.WriteText( "seeking back to #3 and reading 3:\n\n" );
336 wxFileInputStream
file_input3( wxString("test_wx2.dat") );
337 wxBufferedInputStream
buf_input3( file_input3
);
338 for (ch2
= 0; ch2
< 10; ch2
++)
340 buf_input3
.Read( &ch
, 1 );
341 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
343 for (int j
= 0; j
< 2000; j
++)
344 buf_input3
.Read( &ch
, 1 );
345 textCtrl
.WriteText( "\n" );
346 buf_input3
.SeekI( 3 );
347 buf_input3
.Read( &ch
, 1 );
348 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
349 textCtrl
.WriteText( "\n\n\n" );
353 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
355 wxTextCtrl
& textCtrl
= * GetTextCtrl();
358 textCtrl
<< "\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n";
362 textCtrl
.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" );
364 wxFileOutputStream
file_output( wxString("test_wx.dat") );
365 for (ch
= 0; ch
< 10; ch
++)
366 file_output
.Write( &ch
, 1 );
368 // Testing wxFileInputStream
370 textCtrl
.WriteText( "Reading 0 to 10 to wxFileInputStream:\n\n" );
372 wxFileInputStream
file_input( wxString("test_wx.dat") );
373 for (ch2
= 0; ch2
< 11; ch2
++)
375 file_input
.Read( &ch
, 1 );
376 textCtrl
.WriteText( "Value read: " );
377 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
378 textCtrl
.WriteText( "; stream.LastError() returns: " );
379 switch (file_input
.LastError())
381 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
382 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
383 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
384 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
385 default: textCtrl
.WriteText( "Huh?\n" ); break;
388 textCtrl
.WriteText( "\n" );
390 textCtrl
.WriteText( "Seeking to 0; stream.LastError() returns: " );
391 file_input
.SeekI( 0 );
392 switch (file_input
.LastError())
394 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
395 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
396 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
397 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
398 default: textCtrl
.WriteText( "Huh?\n" ); break;
400 textCtrl
.WriteText( "\n" );
402 file_input
.Read( &ch
, 1 );
403 textCtrl
.WriteText( "Value read: " );
404 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
405 textCtrl
.WriteText( "; stream.LastError() returns: " );
406 switch (file_input
.LastError())
408 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
409 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
410 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
411 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
412 default: textCtrl
.WriteText( "Huh?\n" ); break;
414 textCtrl
.WriteText( "\n\n" );
417 // Testing wxFFileInputStream
419 textCtrl
.WriteText( "Reading 0 to 10 to wxFFileInputStream:\n\n" );
421 wxFFileInputStream
ffile_input( wxString("test_wx.dat") );
422 for (ch2
= 0; ch2
< 11; ch2
++)
424 ffile_input
.Read( &ch
, 1 );
425 textCtrl
.WriteText( "Value read: " );
426 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
427 textCtrl
.WriteText( "; stream.LastError() returns: " );
428 switch (ffile_input
.LastError())
430 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
431 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
432 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
433 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
434 default: textCtrl
.WriteText( "Huh?\n" ); break;
437 textCtrl
.WriteText( "\n" );
439 textCtrl
.WriteText( "Seeking to 0; stream.LastError() returns: " );
440 ffile_input
.SeekI( 0 );
441 switch (ffile_input
.LastError())
443 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
444 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
445 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
446 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
447 default: textCtrl
.WriteText( "Huh?\n" ); break;
449 textCtrl
.WriteText( "\n" );
451 ffile_input
.Read( &ch
, 1 );
452 textCtrl
.WriteText( "Value read: " );
453 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
454 textCtrl
.WriteText( "; stream.LastError() returns: " );
455 switch (ffile_input
.LastError())
457 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
458 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
459 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
460 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
461 default: textCtrl
.WriteText( "Huh?\n" ); break;
463 textCtrl
.WriteText( "\n\n" );
465 // Testing wxFFileInputStream
467 textCtrl
.WriteText( "Reading 0 to 10 to buffered wxFFileInputStream:\n\n" );
469 wxFFileInputStream
ffile_input2( wxString("test_wx.dat") );
470 wxBufferedInputStream
buf_input( ffile_input2
);
471 for (ch2
= 0; ch2
< 11; ch2
++)
473 buf_input
.Read( &ch
, 1 );
474 textCtrl
.WriteText( "Value read: " );
475 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
476 textCtrl
.WriteText( "; stream.LastError() returns: " );
477 switch (buf_input
.LastError())
479 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
480 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
481 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
482 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
483 default: textCtrl
.WriteText( "Huh?\n" ); break;
486 textCtrl
.WriteText( "\n" );
488 textCtrl
.WriteText( "Seeking to 0; stream.LastError() returns: " );
489 buf_input
.SeekI( 0 );
490 switch (buf_input
.LastError())
492 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
493 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
494 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
495 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
496 default: textCtrl
.WriteText( "Huh?\n" ); break;
498 textCtrl
.WriteText( "\n" );
500 buf_input
.Read( &ch
, 1 );
501 textCtrl
.WriteText( "Value read: " );
502 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
503 textCtrl
.WriteText( "; stream.LastError() returns: " );
504 switch (buf_input
.LastError())
506 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
507 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
508 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
509 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
510 default: textCtrl
.WriteText( "Huh?\n" ); break;
514 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
516 wxTextCtrl
& textCtrl
= * GetTextCtrl();
521 textCtrl
<< "\nTesting wxStreamBuffer:\n\n";
523 // bigger than buffer
524 textCtrl
.WriteText( "Writing 2000x 1 to wxFileOutputStream.\n\n" );
526 wxFileOutputStream
file_output( wxString("test_wx.dat") );
527 for (int i
= 0; i
< 2000; i
++)
530 file_output
.Write( &ch
, 1 );
533 textCtrl
.WriteText( "Opening with a buffered wxFileInputStream:\n\n" );
535 wxFileInputStream
file_input( wxString("test_wx.dat") );
536 wxBufferedInputStream
buf_input( file_input
);
538 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
539 switch (buf_input
.LastError())
541 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
542 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
543 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
544 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
545 default: textCtrl
.WriteText( "Huh?\n" ); break;
547 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
548 textCtrl
.WriteText( msg
);
549 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
550 textCtrl
.WriteText( msg
);
551 textCtrl
.WriteText( "\n\n" );
554 textCtrl
.WriteText( "Seeking to position 300:\n\n" );
556 buf_input
.SeekI( 300 );
558 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
559 switch (buf_input
.LastError())
561 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
562 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
563 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
564 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
565 default: textCtrl
.WriteText( "Huh?\n" ); break;
567 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
568 textCtrl
.WriteText( msg
);
569 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
570 textCtrl
.WriteText( msg
);
571 textCtrl
.WriteText( "\n\n" );
576 textCtrl
.WriteText( "Reading 500 bytes:\n\n" );
578 buf_input
.Read( buf
, 500 );
580 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
581 switch (buf_input
.LastError())
583 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
584 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
585 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
586 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
587 default: textCtrl
.WriteText( "Huh?\n" ); break;
589 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
590 textCtrl
.WriteText( msg
);
591 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
592 textCtrl
.WriteText( msg
);
593 textCtrl
.WriteText( "\n\n" );
595 textCtrl
.WriteText( "Reading another 500 bytes:\n\n" );
597 buf_input
.Read( buf
, 500 );
599 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
600 switch (buf_input
.LastError())
602 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
603 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
604 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
605 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
606 default: textCtrl
.WriteText( "Huh?\n" ); break;
608 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
609 textCtrl
.WriteText( msg
);
610 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
611 textCtrl
.WriteText( msg
);
612 textCtrl
.WriteText( "\n\n" );
614 textCtrl
.WriteText( "Reading another 500 bytes:\n\n" );
616 buf_input
.Read( buf
, 500 );
618 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
619 switch (buf_input
.LastError())
621 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
622 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
623 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
624 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
625 default: textCtrl
.WriteText( "Huh?\n" ); break;
627 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
628 textCtrl
.WriteText( msg
);
629 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
630 textCtrl
.WriteText( msg
);
631 textCtrl
.WriteText( "\n\n" );
633 textCtrl
.WriteText( "Reading another 500 bytes:\n\n" );
635 buf_input
.Read( buf
, 500 );
637 textCtrl
.WriteText( "wxBufferedInputStream.LastError() returns: " );
638 switch (buf_input
.LastError())
640 case wxSTREAM_NOERROR
: textCtrl
.WriteText( "wxSTREAM_NOERROR\n" ); break;
641 case wxSTREAM_EOF
: textCtrl
.WriteText( "wxSTREAM_EOF\n" ); break;
642 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
643 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
644 default: textCtrl
.WriteText( "Huh?\n" ); break;
646 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
647 textCtrl
.WriteText( msg
);
648 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
649 textCtrl
.WriteText( msg
);
650 textCtrl
.WriteText( "\n\n" );
653 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
655 wxTextCtrl
& textCtrl
= * GetTextCtrl();
658 textCtrl
<< "\nTesting wxFileInputStream's Peek():\n\n";
663 textCtrl
.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" );
665 wxFileOutputStream
file_output( wxString("test_wx.dat") );
666 for (ch
= 0; ch
< 10; ch
++)
667 file_output
.Write( &ch
, 1 );
671 wxFileInputStream
file_input( wxString("test_wx.dat") );
673 ch
= file_input
.Peek();
674 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
675 textCtrl
.WriteText( str
);
677 ch
= file_input
.GetC();
678 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
679 textCtrl
.WriteText( str
);
681 ch
= file_input
.Peek();
682 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
683 textCtrl
.WriteText( str
);
685 ch
= file_input
.GetC();
686 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
687 textCtrl
.WriteText( str
);
689 ch
= file_input
.Peek();
690 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
691 textCtrl
.WriteText( str
);
693 ch
= file_input
.GetC();
694 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
695 textCtrl
.WriteText( str
);
698 textCtrl
<< "\n\n\nTesting wxMemoryInputStream's Peek():\n\n";
700 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
701 wxMemoryInputStream
input( buf
, 10 );
704 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
705 textCtrl
.WriteText( str
);
708 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
709 textCtrl
.WriteText( str
);
712 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
713 textCtrl
.WriteText( str
);
716 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
717 textCtrl
.WriteText( str
);
720 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
721 textCtrl
.WriteText( str
);
724 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
725 textCtrl
.WriteText( str
);
729 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
731 wxTextCtrl
& textCtrl
= * GetTextCtrl();
734 textCtrl
<< "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
737 str
= _T("Robert Röbling\n");
739 printf( "\n\nConversion with wxConvLocal:\n" );
740 wxConvCurrent
= &wxConvLocal
;
741 printf( (const char*) str
.mbc_str() );
742 #if defined(__WXGTK__)
743 printf( "\n\nConversion with wxConvGdk:\n" );
744 wxConvCurrent
= &wxConvGdk
;
745 printf( (const char*) str
.mbc_str() );
747 printf( "\n\nConversion with wxConvLibc:\n" );
748 wxConvCurrent
= &wxConvLibc
;
749 printf( (const char*) str
.mbc_str() );
754 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
756 static wxString s_defaultExt
= "xyz";
758 wxString ext
= wxGetTextFromUser("Enter a file extension: ",
759 "MIME database test",
765 // init MIME database if not done yet
766 if ( !m_mimeDatabase
)
768 m_mimeDatabase
= new wxMimeTypesManager
;
770 static const wxFileTypeInfo fallbacks
[] =
772 wxFileTypeInfo("application/xyz",
775 "The one and only XYZ format file",
777 wxFileTypeInfo("text/html",
779 "lynx -dump %s | lpr",
780 "HTML document (from fallback)",
781 "htm", "html", NULL
),
783 // must terminate the table with this!
787 m_mimeDatabase
->AddFallbacks(fallbacks
);
790 wxTextCtrl
& textCtrl
= * GetTextCtrl();
792 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
795 textCtrl
<< "Unknown extension '" << ext
<< "'\n";
799 wxString type
, desc
, open
;
800 filetype
->GetMimeType(&type
);
801 filetype
->GetDescription(&desc
);
803 wxString filename
= "filename";
804 filename
<< "." << ext
;
805 wxFileType::MessageParameters
params(filename
, type
);
806 filetype
->GetOpenCommand(&open
, params
);
808 textCtrl
<< "MIME information about extension '" << ext
<< "'\n"
809 << "\tMIME type: " << ( !type
? wxT("unknown")
810 : type
.c_str() ) << '\n'
811 << "\tDescription: " << ( !desc
? wxT("") : desc
.c_str() )
813 << "\tCommand to open: " << ( !open
? wxT("no") : open
.c_str() )
819 //else: cancelled by user
822 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
824 wxTextCtrl
& textCtrl
= * GetTextCtrl();
827 textCtrl
<< "\nTest byte order macros:\n\n";
829 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
830 textCtrl
<< "This is a little endian system.\n\n";
832 textCtrl
<< "This is a big endian system.\n\n";
836 wxInt32 var
= 0xF1F2F3F4;
838 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
839 textCtrl
.WriteText( text
);
842 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
843 textCtrl
.WriteText( text
);
846 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
847 textCtrl
.WriteText( text
);
850 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
851 textCtrl
.WriteText( text
);
856 void MyApp::DoTimeDemo(wxCommandEvent
& WXUNUSED(event
))
858 wxTextCtrl
& textCtrl
= * GetTextCtrl();
861 textCtrl
<< "\nTest class wxTime:\n";
863 textCtrl
<< "It is now " << (wxString
) now
<< "\n";
866 void MyApp::DoDateDemo(wxCommandEvent
& WXUNUSED(event
))
868 wxTextCtrl
& textCtrl
= * GetTextCtrl();
871 textCtrl
<< "\nTest class wxDate" << "\n";
873 // Various versions of the constructors
874 // and various output
876 wxDate
x(10,20,1962);
878 textCtrl
<< x
.FormatDate(wxFULL
) << " (full)\n";
880 // constuctor with a string, just printing the day of the week
881 wxDate
y("8/8/1988");
883 textCtrl
<< y
.FormatDate(wxDAY
) << " (just day)\n";
885 // constructor with a julian
886 wxDate
z( 2450000L );
887 textCtrl
<< z
.FormatDate(wxFULL
) << " (full)\n";
889 // using date addition and subtraction
891 textCtrl
<< a
.FormatDate(wxFULL
) << " (full)\n";
893 textCtrl
<< a
.FormatDate(wxEUROPEAN
) << " (European)\n";
895 // Using subtraction of two date objects
896 wxDate a1
= wxString("7/13/1991");
898 textCtrl
<< (a1
-a2
) << "\n";
899 textCtrl
<< (a2
+=10) << "\n";
902 textCtrl
<< "Tomorrow= " << a1
.FormatDate(wxFULL
) << "\n";
904 wxDate
tmpDate1("08/01/1991");
905 wxDate
tmpDate2("07/14/1991");
906 textCtrl
<< "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1
< tmpDate1
) ? "TRUE" : "FALSE") << "\n";
907 textCtrl
<< "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1
> tmpDate1
) ? "TRUE" : "FALSE") << "\n";
908 textCtrl
<< "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1
==tmpDate2
) ? "TRUE" : "FALSE") << "\n";
911 textCtrl
<< "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1
==a3
) ? "TRUE" : "FALSE") << "\n";
913 textCtrl
<< "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1
==++a4
) ? "TRUE" : "FALSE") << "\n";
915 wxDate a5
= wxString("today");
916 textCtrl
<< "Today is: " << a5
<< "\n";
918 textCtrl
<< "Today (a4) is: " << a4
<< "\n";
920 textCtrl
<< "Today + 4 is: " << (a4
+=4) << "\n";
922 textCtrl
<< "Today - 4 is: " << (a4
-=4) << "\n";
924 textCtrl
<< "=========== Leap Year Test ===========\n";
926 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
927 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
930 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
931 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
933 textCtrl
<< "================== string assignment test ====================\n";
934 wxString date_string
=a1
;
935 textCtrl
<< "a1 as a string (s/b 2/16/1993) ==> " << date_string
<< "\n";
937 textCtrl
<< "================== SetFormat test ============================\n";
938 a1
.SetFormat(wxFULL
);
939 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
940 a1
.SetFormat(wxEUROPEAN
);
941 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
943 textCtrl
<< "================== SetOption test ============================\n";
944 textCtrl
<< "Date abbreviation ON\n";
946 a1
.SetOption(wxDATE_ABBR
);
947 a1
.SetFormat(wxMONTH
);
948 textCtrl
<< "a1 (s/b MONTH format) ==> " << a1
<< "\n";
950 textCtrl
<< "a1 (s/b DAY format) ==> " << a1
<< "\n";
951 a1
.SetFormat(wxFULL
);
952 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
953 a1
.SetFormat(wxEUROPEAN
);
954 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
955 textCtrl
<< "Century suppression ON\n";
956 a1
.SetOption(wxNO_CENTURY
);
958 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
959 textCtrl
<< "Century suppression OFF\n";
960 a1
.SetOption(wxNO_CENTURY
,FALSE
);
961 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
962 textCtrl
<< "Century suppression ON\n";
963 a1
.SetOption(wxNO_CENTURY
);
964 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
965 a1
.SetFormat(wxFULL
);
966 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
968 textCtrl
<< "\n=============== Version 4.0 Enhancement Test =================\n";
970 wxDate
v4("11/26/1966");
971 textCtrl
<< "\n---------- Set Stuff -----------\n";
972 textCtrl
<< "First, 'Set' to today..." << "\n";
973 textCtrl
<< "Before 'Set' => " << v4
<< "\n";
974 textCtrl
<< "After 'Set' => " << v4
.Set() << "\n\n";
976 textCtrl
<< "Set to 11/26/66 => " << v4
.Set(11,26,1966) << "\n";
977 textCtrl
<< "Current Julian => " << v4
.GetJulianDate() << "\n";
978 textCtrl
<< "Set to Julian 2450000L => " << v4
.Set(2450000L) << "\n";
979 textCtrl
<< "See! => " << v4
.GetJulianDate() << "\n";
981 textCtrl
<< "---------- Add Stuff -----------\n";
982 textCtrl
<< "Start => " << v4
<< "\n";
983 textCtrl
<< "Add 4 Weeks => " << v4
.AddWeeks(4) << "\n";
984 textCtrl
<< "Sub 1 Month => " << v4
.AddMonths(-1) << "\n";
985 textCtrl
<< "Add 2 Years => " << v4
.AddYears(2) << "\n";
987 textCtrl
<< "---------- Misc Stuff -----------\n";
988 textCtrl
<< "The date aboves' day of the month is => " << v4
.GetDay() << "\n";
989 textCtrl
<< "There are " << v4
.GetDaysInMonth() << " days in this month.\n";
990 textCtrl
<< "The first day of this month lands on " << v4
.GetFirstDayOfMonth() << "\n";
991 textCtrl
<< "This day happens to be " << v4
.GetDayOfWeekName() << "\n";
992 textCtrl
<< "the " << v4
.GetDayOfWeek() << " day of the week," << "\n";
993 textCtrl
<< "on the " << v4
.GetWeekOfYear() << " week of the year," << "\n";
994 textCtrl
<< "on the " << v4
.GetWeekOfMonth() << " week of the month, " << "\n";
995 textCtrl
<< "(which is " << v4
.GetMonthName() << ")\n";
996 textCtrl
<< "the "<< v4
.GetMonth() << "th month in the year.\n";
997 textCtrl
<< "The year alone is " << v4
.GetYear() << "\n";
999 textCtrl
<< "---------- First and Last Stuff -----------\n";
1001 textCtrl
<< "The first date of this month is " << v4
.GetMonthStart() << "\n";
1002 textCtrl
<< "The last date of this month is " << v4
.GetMonthEnd() << "\n";
1003 textCtrl
<< "The first date of this year is " << v4
.GetYearStart() << "\n";
1004 textCtrl
<< "The last date of this year is " << v4
.GetYearEnd() << "\n";
1007 #endif // wxUSE_TIMEDATE
1009 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
1011 wxTextCtrl
& textCtrl
= * GetTextCtrl();
1013 wxVariant var1
= "String value";
1014 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
1017 wxString str
= var1
.MakeString();
1020 textCtrl
<< "var1 = " << var1
.GetReal() << "\n";
1022 // Implicit conversion
1026 textCtrl
<< "var1 = " << var1
.GetLong() << "\n";
1028 // Implicit conversion
1031 // suppress compile warnings about unused variables
1037 wxStringList stringList
;
1038 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
1040 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
1043 var1
.Append(wxVariant(1.2345));
1044 var1
.Append(wxVariant("hello"));
1045 var1
.Append(wxVariant(54321L));
1047 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
1049 size_t n
= var1
.GetCount();
1051 for (i
= (size_t) 0; i
< n
; i
++)
1053 textCtrl
<< "var1[" << (int) i
<< "] (type " << var1
[i
].GetType() << ") = " << var1
[i
].MakeString() << "\n";
1057 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1058 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
1059 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
1062 // My frame constructor
1063 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
1064 const wxPoint
& pos
, const wxSize
& size
):
1065 wxFrame(parent
, -1, title
, pos
, size
)
1068 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1073 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1075 wxMessageDialog
dialog(this, "Tests various wxWindows types",
1076 "About Types", wxYES_NO
|wxCANCEL
);