]>
git.saurik.com Git - wxWidgets.git/blob - samples/typetest/typetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWidgets sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "typetest.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
27 #include "wx/variant.h"
28 #include "wx/mimetype.h"
32 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__)
33 #include "mondrian.xpm"
40 #include "wx/ioswrap.h"
48 #include "wx/wfstream.h"
49 #include "wx/datstrm.h"
50 #include "wx/txtstrm.h"
51 #include "wx/mstream.h"
53 // Create a new application object
56 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
58 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
59 EVT_MENU(TYPES_VARIANT
, MyApp::DoVariantDemo
)
60 EVT_MENU(TYPES_BYTEORDER
, MyApp::DoByteOrderDemo
)
62 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
63 #endif // wxUSE_UNICODE
64 EVT_MENU(TYPES_STREAM
, MyApp::DoStreamDemo
)
65 EVT_MENU(TYPES_STREAM2
, MyApp::DoStreamDemo2
)
66 EVT_MENU(TYPES_STREAM3
, MyApp::DoStreamDemo3
)
67 EVT_MENU(TYPES_STREAM4
, MyApp::DoStreamDemo4
)
68 EVT_MENU(TYPES_STREAM5
, MyApp::DoStreamDemo5
)
69 EVT_MENU(TYPES_STREAM6
, MyApp::DoStreamDemo6
)
70 EVT_MENU(TYPES_STREAM7
, MyApp::DoStreamDemo7
)
71 EVT_MENU(TYPES_MIME
, MyApp::DoMIMEDemo
)
76 // Create the main frame window
77 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, _T("wxWidgets Types Demo"),
78 wxPoint(50, 50), wxSize(450, 340));
81 frame
->SetIcon(wxICON(mondrian
));
84 wxMenu
*file_menu
= new wxMenu
;
86 file_menu
->Append(TYPES_ABOUT
, _T("&About"));
87 file_menu
->AppendSeparator();
88 file_menu
->Append(TYPES_QUIT
, _T("E&xit\tAlt-X"));
90 wxMenu
*test_menu
= new wxMenu
;
91 test_menu
->Append(TYPES_VARIANT
, _T("&Variant test"));
92 test_menu
->Append(TYPES_BYTEORDER
, _T("&Byteorder test"));
94 test_menu
->Append(TYPES_UNICODE
, _T("&Unicode test"));
95 #endif // wxUSE_UNICODE
96 test_menu
->Append(TYPES_STREAM
, _T("&Stream test"));
97 test_menu
->Append(TYPES_STREAM2
, _T("&Stream seek test"));
98 test_menu
->Append(TYPES_STREAM3
, _T("&Stream error test"));
99 test_menu
->Append(TYPES_STREAM4
, _T("&Stream buffer test"));
100 test_menu
->Append(TYPES_STREAM5
, _T("&Stream peek test"));
101 test_menu
->Append(TYPES_STREAM6
, _T("&Stream ungetch test"));
102 test_menu
->Append(TYPES_STREAM7
, _T("&Stream ungetch test for a buffered stream"));
103 test_menu
->AppendSeparator();
104 test_menu
->Append(TYPES_MIME
, _T("&MIME database test"));
106 wxMenuBar
*menu_bar
= new wxMenuBar
;
107 menu_bar
->Append(file_menu
, _T("&File"));
108 menu_bar
->Append(test_menu
, _T("&Tests"));
109 frame
->SetMenuBar(menu_bar
);
111 m_textCtrl
= new wxTextCtrl(frame
, wxID_ANY
, wxEmptyString
,
112 wxDefaultPosition
, wxDefaultSize
, wxTE_MULTILINE
);
122 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
124 wxTextCtrl
& textCtrl
= * GetTextCtrl();
127 textCtrl
<< _T("\nTest fstream vs. wxFileStream:\n\n");
129 textCtrl
.WriteText( _T("Writing to ofstream and wxFileOutputStream:\n") );
131 wxSTD ofstream
std_file_output( "test_std.dat" );
132 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
133 wxBufferedOutputStream
buf_output( file_output
);
134 wxTextOutputStream
text_output( buf_output
);
137 signed int si
= 0xFFFFFFFF;
138 tmp
.Printf( _T("Signed int: %d\n"), si
);
139 textCtrl
.WriteText( tmp
);
140 text_output
<< si
<< _T("\n");
141 std_file_output
<< si
<< "\n";
143 unsigned int ui
= 0xFFFFFFFF;
144 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
145 textCtrl
.WriteText( tmp
);
146 text_output
<< ui
<< _T("\n");
147 std_file_output
<< ui
<< "\n";
149 double d
= 2.01234567890123456789;
150 tmp
.Printf( _T("Double: %f\n"), d
);
151 textCtrl
.WriteText( tmp
);
152 text_output
<< d
<< _T("\n");
153 std_file_output
<< d
<< "\n";
155 float f
= (float)0.00001;
156 tmp
.Printf( _T("Float: %f\n"), f
);
157 textCtrl
.WriteText( tmp
);
158 text_output
<< f
<< _T("\n");
159 std_file_output
<< f
<< "\n";
161 wxString
str( _T("Hello!") );
162 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
163 textCtrl
.WriteText( tmp
);
164 text_output
<< str
<< _T("\n");
165 std_file_output
<< str
.ToAscii() << "\n";
167 textCtrl
.WriteText( _T("\nReading from ifstream:\n") );
169 wxSTD ifstream
std_file_input( "test_std.dat" );
171 std_file_input
>> si
;
172 tmp
.Printf( _T("Signed int: %d\n"), si
);
173 textCtrl
.WriteText( tmp
);
175 std_file_input
>> ui
;
176 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
177 textCtrl
.WriteText( tmp
);
180 tmp
.Printf( _T("Double: %f\n"), d
);
181 textCtrl
.WriteText( tmp
);
184 tmp
.Printf( _T("Float: %f\n"), f
);
185 textCtrl
.WriteText( tmp
);
188 std_file_input
>> std_buf
;
189 str
.FromAscii(std_buf
);
190 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
191 textCtrl
.WriteText( tmp
);
193 textCtrl
.WriteText( _T("\nReading from wxFileInputStream:\n") );
197 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
198 wxBufferedInputStream
buf_input( file_input
);
199 wxTextInputStream
text_input( file_input
);
202 tmp
.Printf( _T("Signed int: %d\n"), si
);
203 textCtrl
.WriteText( tmp
);
206 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
207 textCtrl
.WriteText( tmp
);
210 tmp
.Printf( _T("Double: %f\n"), d
);
211 textCtrl
.WriteText( tmp
);
214 tmp
.Printf( _T("Float: %f\n"), f
);
215 textCtrl
.WriteText( tmp
);
218 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
219 textCtrl
.WriteText( tmp
);
223 textCtrl
<< _T("\nTest for wxDataStream:\n\n");
225 textCtrl
.WriteText( _T("Writing to wxDataOutputStream:\n") );
227 file_output
.SeekO( 0 );
228 wxDataOutputStream
data_output( buf_output
);
230 wxInt16 i16
= (unsigned short)0xFFFF;
231 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
232 textCtrl
.WriteText( tmp
);
233 data_output
.Write16( i16
);
235 wxUint16 ui16
= 0xFFFF;
236 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
237 textCtrl
.WriteText( tmp
);
238 data_output
.Write16( ui16
);
240 d
= 2.01234567890123456789;
241 tmp
.Printf( _T("Double: %f\n"), d
);
242 textCtrl
.WriteText( tmp
);
243 data_output
.WriteDouble( d
);
246 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
247 textCtrl
.WriteText( tmp
);
248 data_output
.WriteString( str
);
252 textCtrl
.WriteText( _T("\nReading from wxDataInputStream:\n") );
254 file_input
.SeekI( 0 );
255 wxDataInputStream
data_input( buf_input
);
257 i16
= data_input
.Read16();
258 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
259 textCtrl
.WriteText( tmp
);
261 ui16
= data_input
.Read16();
262 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
263 textCtrl
.WriteText( tmp
);
265 d
= data_input
.ReadDouble();
266 tmp
.Printf( _T("Double: %f\n"), d
);
267 textCtrl
.WriteText( tmp
);
269 str
= data_input
.ReadString();
270 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
271 textCtrl
.WriteText( tmp
);
274 void MyApp::DoStreamDemo2(wxCommandEvent
& WXUNUSED(event
))
276 wxTextCtrl
& textCtrl
= * GetTextCtrl();
279 textCtrl
<< _T("\nTesting wxBufferedStream:\n\n");
283 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") );
285 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
286 wxBufferedOutputStream
buf_output( file_output
);
287 for (ch
= 0; ch
< 10; ch
++)
288 buf_output
.Write( &ch
, 1 );
291 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
292 for (ch2
= 0; ch2
< 10; ch2
++)
294 file_input
.Read( &ch
, 1 );
295 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
297 textCtrl
.WriteText( _T("\n\n\n") );
299 textCtrl
.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") );
300 textCtrl
.WriteText( _T("seeking back to #3 and writing 0:\n\n") );
302 wxFileOutputStream
file_output2( wxString(_T("test_wx2.dat")) );
303 wxBufferedOutputStream
buf_output2( file_output2
);
304 for (ch
= 0; ch
< 10; ch
++)
305 buf_output2
.Write( &ch
, 1 );
306 buf_output2
.SeekO( 3 );
308 buf_output2
.Write( &ch
, 1 );
311 wxFileInputStream
file_input2( wxString(_T("test_wx2.dat")) );
312 for (ch2
= 0; ch2
< 10; ch2
++)
314 file_input2
.Read( &ch
, 1 );
315 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
317 textCtrl
.WriteText( _T("\n\n\n") );
319 // now append 2000 bytes to file (bigger than buffer)
320 buf_output2
.SeekO( 0, wxFromEnd
);
322 for (int i
= 0; i
< 2000; i
++)
323 buf_output2
.Write( &ch
, 1 );
326 textCtrl
.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") );
327 textCtrl
.WriteText( _T("seeking back to #3 and reading the 0:\n\n") );
329 wxFileInputStream
file_input3( wxString(_T("test_wx2.dat")) );
330 wxBufferedInputStream
buf_input3( file_input3
);
331 for (ch2
= 0; ch2
< 10; ch2
++)
333 buf_input3
.Read( &ch
, 1 );
334 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
336 for (int j
= 0; j
< 2000; j
++)
337 buf_input3
.Read( &ch
, 1 );
338 textCtrl
.WriteText( _T("\n") );
339 buf_input3
.SeekI( 3 );
340 buf_input3
.Read( &ch
, 1 );
341 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
342 textCtrl
.WriteText( _T("\n\n\n") );
346 void MyApp::DoStreamDemo3(wxCommandEvent
& WXUNUSED(event
))
348 wxTextCtrl
& textCtrl
= * GetTextCtrl();
351 textCtrl
<< _T("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n");
355 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
357 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
358 for (ch
= 0; ch
< 10; ch
++)
359 file_output
.Write( &ch
, 1 );
361 // Testing wxFileInputStream
363 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFileInputStream:\n\n") );
365 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
366 for (ch2
= 0; ch2
< 11; ch2
++)
368 file_input
.Read( &ch
, 1 );
369 textCtrl
.WriteText( _T("Value read: ") );
370 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
371 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
372 switch (file_input
.GetLastError())
374 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
375 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
376 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
377 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
378 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
381 textCtrl
.WriteText( _T("\n") );
383 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
384 file_input
.SeekI( 0 );
385 switch (file_input
.GetLastError())
387 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
388 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
389 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
390 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
391 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
393 textCtrl
.WriteText( _T("\n") );
395 file_input
.Read( &ch
, 1 );
396 textCtrl
.WriteText( _T("Value read: ") );
397 textCtrl
.WriteText( (wxChar
)(ch
+ _T('0')) );
398 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
399 switch (file_input
.GetLastError())
401 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
402 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
403 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
404 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
405 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
407 textCtrl
.WriteText( _T("\n\n") );
410 // Testing wxFFileInputStream
412 textCtrl
.WriteText( _T("Reading 0 to 10 to wxFFileInputStream:\n\n") );
414 wxFFileInputStream
ffile_input( wxString(_T("test_wx.dat")) );
415 for (ch2
= 0; ch2
< 11; ch2
++)
417 ffile_input
.Read( &ch
, 1 );
418 textCtrl
.WriteText( _T("Value read: ") );
419 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
420 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
421 switch (ffile_input
.GetLastError())
423 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
424 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
425 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
426 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
427 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
430 textCtrl
.WriteText( _T("\n") );
432 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
433 ffile_input
.SeekI( 0 );
434 switch (ffile_input
.GetLastError())
436 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
437 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
438 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
439 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
440 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
442 textCtrl
.WriteText( _T("\n") );
444 ffile_input
.Read( &ch
, 1 );
445 textCtrl
.WriteText( _T("Value read: ") );
446 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
447 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
448 switch (ffile_input
.GetLastError())
450 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
451 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
452 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
453 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
454 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
456 textCtrl
.WriteText( _T("\n\n") );
458 // Testing wxFFileInputStream
460 textCtrl
.WriteText( _T("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") );
462 wxFFileInputStream
ffile_input2( wxString(_T("test_wx.dat")) );
463 wxBufferedInputStream
buf_input( ffile_input2
);
464 for (ch2
= 0; ch2
< 11; ch2
++)
466 buf_input
.Read( &ch
, 1 );
467 textCtrl
.WriteText( _T("Value read: ") );
468 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
469 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
470 switch (buf_input
.GetLastError())
472 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
473 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
474 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
475 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
476 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
479 textCtrl
.WriteText( _T("\n") );
481 textCtrl
.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") );
482 buf_input
.SeekI( 0 );
483 switch (buf_input
.GetLastError())
485 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
486 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
487 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
488 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
489 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
491 textCtrl
.WriteText( _T("\n") );
493 buf_input
.Read( &ch
, 1 );
494 textCtrl
.WriteText( _T("Value read: ") );
495 textCtrl
.WriteText( (wxChar
)(ch
+ '0') );
496 textCtrl
.WriteText( _T("; stream.GetLastError() returns: ") );
497 switch (buf_input
.GetLastError())
499 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
500 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
501 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
502 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
503 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
507 void MyApp::DoStreamDemo4(wxCommandEvent
& WXUNUSED(event
))
509 wxTextCtrl
& textCtrl
= * GetTextCtrl();
514 textCtrl
<< _T("\nTesting wxStreamBuffer:\n\n");
516 // bigger than buffer
517 textCtrl
.WriteText( _T("Writing 2000x 1 to wxFileOutputStream.\n\n") );
519 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
520 for (int i
= 0; i
< 2000; i
++)
523 file_output
.Write( &ch
, 1 );
526 textCtrl
.WriteText( _T("Opening with a buffered wxFileInputStream:\n\n") );
528 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
529 wxBufferedInputStream
buf_input( file_input
);
531 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
532 switch (buf_input
.GetLastError())
534 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
535 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
536 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
537 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
538 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
540 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
541 textCtrl
.WriteText( msg
);
542 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
543 textCtrl
.WriteText( msg
);
544 textCtrl
.WriteText( _T("\n\n") );
547 textCtrl
.WriteText( _T("Seeking to position 300:\n\n") );
549 buf_input
.SeekI( 300 );
551 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
552 switch (buf_input
.GetLastError())
554 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
555 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
556 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
557 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
558 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
560 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
561 textCtrl
.WriteText( msg
);
562 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
563 textCtrl
.WriteText( msg
);
564 textCtrl
.WriteText( _T("\n\n") );
569 textCtrl
.WriteText( _T("Reading 500 bytes:\n\n") );
571 buf_input
.Read( buf
, 500 );
573 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
574 switch (buf_input
.GetLastError())
576 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
577 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
578 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
579 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
580 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
582 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
583 textCtrl
.WriteText( msg
);
584 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
585 textCtrl
.WriteText( msg
);
586 textCtrl
.WriteText( _T("\n\n") );
588 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
590 buf_input
.Read( buf
, 500 );
592 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
593 switch (buf_input
.GetLastError())
595 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
596 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
597 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
598 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
599 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
601 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
602 textCtrl
.WriteText( msg
);
603 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
604 textCtrl
.WriteText( msg
);
605 textCtrl
.WriteText( _T("\n\n") );
607 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
609 buf_input
.Read( buf
, 500 );
611 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
612 switch (buf_input
.GetLastError())
614 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
615 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
616 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
617 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
618 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
620 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
621 textCtrl
.WriteText( msg
);
622 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
623 textCtrl
.WriteText( msg
);
624 textCtrl
.WriteText( _T("\n\n") );
626 textCtrl
.WriteText( _T("Reading another 500 bytes:\n\n") );
628 buf_input
.Read( buf
, 500 );
630 textCtrl
.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") );
631 switch (buf_input
.GetLastError())
633 case wxSTREAM_NO_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break;
634 case wxSTREAM_EOF
: textCtrl
.WriteText( _T("wxSTREAM_EOF\n") ); break;
635 case wxSTREAM_READ_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break;
636 case wxSTREAM_WRITE_ERROR
: textCtrl
.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break;
637 default: textCtrl
.WriteText( _T("Huh?\n") ); break;
639 msg
.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input
.LastRead() );
640 textCtrl
.WriteText( msg
);
641 msg
.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input
.TellI() );
642 textCtrl
.WriteText( msg
);
643 textCtrl
.WriteText( _T("\n\n") );
646 void MyApp::DoStreamDemo5(wxCommandEvent
& WXUNUSED(event
))
648 wxTextCtrl
& textCtrl
= * GetTextCtrl();
651 textCtrl
<< _T("\nTesting wxFileInputStream's Peek():\n\n");
656 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") );
658 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
659 for (ch
= 0; ch
< 10; ch
++)
660 file_output
.Write( &ch
, 1 );
664 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
666 ch
= file_input
.Peek();
667 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
668 textCtrl
.WriteText( str
);
670 ch
= file_input
.GetC();
671 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
672 textCtrl
.WriteText( str
);
674 ch
= file_input
.Peek();
675 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
676 textCtrl
.WriteText( str
);
678 ch
= file_input
.GetC();
679 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
680 textCtrl
.WriteText( str
);
682 ch
= file_input
.Peek();
683 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
684 textCtrl
.WriteText( str
);
686 ch
= file_input
.GetC();
687 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
688 textCtrl
.WriteText( str
);
691 textCtrl
<< _T("\n\n\nTesting wxMemoryInputStream's Peek():\n\n");
693 char buf
[] = { 0,1,2,3,4,5,6,7,8,9,10 };
694 wxMemoryInputStream
input( buf
, 10 );
697 str
.Printf( wxT("First char peeked: %d\n"), (int) ch
);
698 textCtrl
.WriteText( str
);
701 str
.Printf( wxT("First char read: %d\n"), (int) ch
);
702 textCtrl
.WriteText( str
);
705 str
.Printf( wxT("Second char peeked: %d\n"), (int) ch
);
706 textCtrl
.WriteText( str
);
709 str
.Printf( wxT("Second char read: %d\n"), (int) ch
);
710 textCtrl
.WriteText( str
);
713 str
.Printf( wxT("Third char peeked: %d\n"), (int) ch
);
714 textCtrl
.WriteText( str
);
717 str
.Printf( wxT("Third char read: %d\n"), (int) ch
);
718 textCtrl
.WriteText( str
);
721 void MyApp::DoStreamDemo6(wxCommandEvent
& WXUNUSED(event
))
723 wxTextCtrl
& textCtrl
= * GetTextCtrl();
726 textCtrl
.WriteText( _T("\nTesting Ungetch():\n\n") );
731 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
733 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
734 for (ch
= 0; ch
< 10; ch
++)
735 file_output
.Write( &ch
, 1 );
739 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
741 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
743 ch
= file_input
.GetC();
744 size_t pos
= file_input
.TellI();
745 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
746 textCtrl
.WriteText( str
);
748 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
750 ch
= file_input
.GetC();
751 pos
= file_input
.TellI();
752 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
753 textCtrl
.WriteText( str
);
755 textCtrl
.WriteText( _T("Reading yet another char from wxFileInputStream:\n\n") );
757 ch
= file_input
.GetC();
758 pos
= file_input
.TellI();
759 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
760 textCtrl
.WriteText( str
);
762 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") );
764 file_input
.Ungetch( 5 );
765 pos
= file_input
.TellI();
766 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
767 textCtrl
.WriteText( str
);
769 textCtrl
.WriteText( _T("Reading char from wxFileInputStream:\n\n") );
771 ch
= file_input
.GetC();
772 pos
= file_input
.TellI();
773 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
774 textCtrl
.WriteText( str
);
776 textCtrl
.WriteText( _T("Reading another char from wxFileInputStream:\n\n") );
778 ch
= file_input
.GetC();
779 pos
= file_input
.TellI();
780 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
781 textCtrl
.WriteText( str
);
783 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") );
785 file_input
.Ungetch( 5 );
786 pos
= file_input
.TellI();
787 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
788 textCtrl
.WriteText( str
);
790 textCtrl
.WriteText( _T("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") );
792 file_input
.SeekI( 3 );
794 ch
= file_input
.GetC();
795 pos
= file_input
.TellI();
796 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
797 textCtrl
.WriteText( str
);
800 void MyApp::DoStreamDemo7(wxCommandEvent
& WXUNUSED(event
))
802 wxTextCtrl
& textCtrl
= * GetTextCtrl();
805 textCtrl
.WriteText( _T("\nTesting Ungetch() in buffered input stream:\n\n") );
810 textCtrl
.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") );
812 wxFileOutputStream
file_output( wxString(_T("test_wx.dat")) );
813 for (ch
= 0; ch
< 10; ch
++)
814 file_output
.Write( &ch
, 1 );
818 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") );
820 wxFileInputStream
file_input( wxString(_T("test_wx.dat")) );
821 wxBufferedInputStream
buf_input( file_input
);
823 ch
= buf_input
.GetC();
824 size_t pos
= buf_input
.TellI();
825 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
826 textCtrl
.WriteText( str
);
828 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
830 ch
= buf_input
.GetC();
831 pos
= buf_input
.TellI();
832 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
833 textCtrl
.WriteText( str
);
835 textCtrl
.WriteText( _T("Reading yet another char from wxBufferedInputStream:\n\n") );
837 ch
= buf_input
.GetC();
838 pos
= buf_input
.TellI();
839 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
840 textCtrl
.WriteText( str
);
842 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") );
844 buf_input
.Ungetch( 5 );
845 pos
= buf_input
.TellI();
846 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
847 textCtrl
.WriteText( str
);
849 textCtrl
.WriteText( _T("Reading char from wxBufferedInputStream:\n\n") );
851 ch
= buf_input
.GetC();
852 pos
= buf_input
.TellI();
853 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
854 textCtrl
.WriteText( str
);
856 textCtrl
.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") );
858 ch
= buf_input
.GetC();
859 pos
= buf_input
.TellI();
860 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
861 textCtrl
.WriteText( str
);
863 textCtrl
.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") );
865 buf_input
.Ungetch( 5 );
866 pos
= buf_input
.TellI();
867 str
.Printf( wxT("Now at position %d\n\n"), (int) pos
);
868 textCtrl
.WriteText( str
);
870 textCtrl
.WriteText( _T("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") );
872 buf_input
.SeekI( 3 );
874 ch
= buf_input
.GetC();
875 pos
= buf_input
.TellI();
876 str
.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch
, (int) pos
);
877 textCtrl
.WriteText( str
);
881 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
883 wxTextCtrl
& textCtrl
= * GetTextCtrl();
886 textCtrl
<< _T("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:");
889 str
= _T("Robert Röbling\n");
891 printf( "\n\nConversion with wxConvLocal:\n" );
892 wxConvCurrent
= &wxConvLocal
;
893 printf( (const char*) str
.mbc_str() );
894 printf( "\n\nConversion with wxConvLibc:\n" );
895 wxConvCurrent
= &wxConvLibc
;
896 printf( (const char*) str
.mbc_str() );
901 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
903 static wxString s_defaultExt
= _T("xyz");
905 wxString ext
= wxGetTextFromUser(_T("Enter a file extension: "),
906 _T("MIME database test"),
912 // init MIME database if not done yet
913 if ( !m_mimeDatabase
)
915 m_mimeDatabase
= new wxMimeTypesManager
;
917 static const wxFileTypeInfo fallbacks
[] =
919 wxFileTypeInfo(_T("application/xyz"),
922 _T("The one and only XYZ format file"),
923 _T("xyz"), _T("123"), NULL
),
924 wxFileTypeInfo(_T("text/html"),
926 _T("lynx -dump %s | lpr"),
927 _T("HTML document (from fallback)"),
928 _T("htm"), _T("html"), NULL
),
930 // must terminate the table with this!
934 m_mimeDatabase
->AddFallbacks(fallbacks
);
937 wxTextCtrl
& textCtrl
= * GetTextCtrl();
939 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
942 textCtrl
<< _T("Unknown extension '") << ext
<< _T("'\n");
946 wxString type
, desc
, open
;
947 filetype
->GetMimeType(&type
);
948 filetype
->GetDescription(&desc
);
950 wxString filename
= _T("filename");
951 filename
<< _T(".") << ext
;
952 wxFileType::MessageParameters
params(filename
, type
);
953 filetype
->GetOpenCommand(&open
, params
);
955 textCtrl
<< _T("MIME information about extension '") << ext
<< _T("'\n")
956 << _T("\tMIME type: ") << ( !type
? wxT("unknown")
957 : type
.c_str() ) << '\n'
958 << _T("\tDescription: ") << ( !desc
? wxEmptyString
: desc
.c_str() )
960 << _T("\tCommand to open: ") << ( !open
? wxT("no") : open
.c_str() )
966 //else: cancelled by user
969 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
971 wxTextCtrl
& textCtrl
= * GetTextCtrl();
974 textCtrl
<< _T("\nTest byte order macros:\n\n");
976 #if wxBYTE_ORDER == wxLITTLE_ENDIAN
977 textCtrl
<< _T("This is a little endian system.\n\n");
979 textCtrl
<< _T("This is a big endian system.\n\n");
984 wxInt32 var
= 0xF1F2F3F4;
985 text
= wxEmptyString
;
986 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
987 textCtrl
.WriteText( text
);
989 text
= wxEmptyString
;
990 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
991 textCtrl
.WriteText( text
);
993 text
= wxEmptyString
;
994 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
995 textCtrl
.WriteText( text
);
997 text
= wxEmptyString
;
998 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
999 textCtrl
.WriteText( text
);
1002 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
1004 wxTextCtrl
& textCtrl
= * GetTextCtrl();
1006 wxVariant var1
= _T("String value");
1007 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1010 wxString str
= var1
.MakeString();
1013 textCtrl
<< _T("var1 = ") << var1
.GetReal() << _T("\n");
1015 // Implicit conversion
1019 textCtrl
<< _T("var1 = ") << var1
.GetLong() << _T("\n");
1021 // Implicit conversion
1024 // suppress compile warnings about unused variables
1028 wxStringList stringList
;
1029 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
1031 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1034 var1
.Append(wxVariant(1.2345));
1035 var1
.Append(wxVariant(_T("hello")));
1036 var1
.Append(wxVariant(54321L));
1038 textCtrl
<< _T("var1 = ") << var1
.MakeString() << _T("\n");
1040 size_t n
= var1
.GetCount();
1042 for (i
= (size_t) 0; i
< n
; i
++)
1044 textCtrl
<< _T("var1[") << (int) i
<< _T("] (type ") << var1
[i
].GetType() << _T(") = ") << var1
[i
].MakeString() << _T("\n");
1047 var1
= wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT
)));
1048 textCtrl
<< _T("var1 = (wxfont)\"");
1049 wxFont
* font
= wxGetVariantCast(var1
,wxFont
);
1052 textCtrl
<< font
->GetNativeFontInfoDesc() << _T("\"\n");
1056 textCtrl
<< _T("(null)\"\n");
1060 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
1061 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
1062 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
1065 // My frame constructor
1066 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
1067 const wxPoint
& pos
, const wxSize
& size
)
1068 : wxFrame(parent
, wxID_ANY
, title
, pos
, size
)
1071 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
1076 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
1078 wxMessageDialog
dialog(this, _T("Tests various wxWidgets types"),
1079 _T("About Types"), wxYES_NO
|wxCANCEL
);