Small changes for VC++ 4 compilation
[wxWidgets.git] / samples / typetest / typetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: typetest.cpp
3 // Purpose: Types wxWindows sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "typetest.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/wx.h"
25 #endif
26
27 #include "wx/time.h"
28 #include "wx/date.h"
29 #include "wx/variant.h"
30 #include "wx/mimetype.h"
31
32 #include "typetest.h"
33
34 #if defined(__WXGTK__) || defined(__WXMOTIF__)
35 #include "mondrian.xpm"
36 #endif
37
38 #include "wx/ioswrap.h"
39
40 #if wxUSE_IOSTREAMH
41 #include <fstream.h>
42 #else
43 #include <fstream>
44 #endif
45
46 #include "wx/wfstream.h"
47 #include "wx/datstrm.h"
48 #include "wx/txtstrm.h"
49
50 // Create a new application object
51 IMPLEMENT_APP (MyApp)
52
53 IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp)
54
55 BEGIN_EVENT_TABLE(MyApp, wxApp)
56 EVT_MENU(TYPES_DATE, MyApp::DoDateDemo)
57 EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo)
58 EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo)
59 EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo)
60 #if wxUSE_UNICODE
61 EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
62 #endif
63 EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
64 EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2)
65 EVT_MENU(TYPES_STREAM3, MyApp::DoStreamDemo3)
66 EVT_MENU(TYPES_STREAM4, MyApp::DoStreamDemo4)
67 EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo)
68 END_EVENT_TABLE()
69
70 bool MyApp::OnInit()
71 {
72 // Create the main frame window
73 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
74 wxPoint(50, 50), wxSize(450, 340));
75
76 // Give it an icon
77 frame->SetIcon(wxICON(mondrian));
78
79 // Make a menubar
80 wxMenu *file_menu = new wxMenu;
81
82 file_menu->Append(TYPES_ABOUT, "&About");
83 file_menu->AppendSeparator();
84 file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X");
85
86 wxMenu *test_menu = new wxMenu;
87 test_menu->Append(TYPES_DATE, "&Date test");
88 test_menu->Append(TYPES_TIME, "&Time test");
89 test_menu->Append(TYPES_VARIANT, "&Variant test");
90 test_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
91 #if wxUSE_UNICODE
92 test_menu->Append(TYPES_UNICODE, "&Unicode test");
93 #endif
94 test_menu->Append(TYPES_STREAM, "&Stream test");
95 test_menu->Append(TYPES_STREAM2, "&Stream seek test");
96 test_menu->Append(TYPES_STREAM3, "&Stream error test");
97 test_menu->Append(TYPES_STREAM4, "&Stream buffer test");
98 test_menu->AppendSeparator();
99 test_menu->Append(TYPES_MIME, "&MIME database test");
100
101 wxMenuBar *menu_bar = new wxMenuBar;
102 menu_bar->Append(file_menu, "&File");
103 menu_bar->Append(test_menu, "&Tests");
104 frame->SetMenuBar(menu_bar);
105
106 m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
107
108 // Show the frame
109 frame->Show(TRUE);
110
111 SetTopWindow(frame);
112
113 return TRUE;
114 }
115
116 void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
117 {
118 wxTextCtrl& textCtrl = * GetTextCtrl();
119
120 textCtrl.Clear();
121 textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n");
122
123 textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
124
125 ofstream std_file_output( "test_std.dat" );
126 wxFileOutputStream file_output( wxString("test_wx.dat") );
127 wxBufferedOutputStream buf_output( file_output );
128 wxTextOutputStream text_output( buf_output );
129
130 wxString tmp;
131 signed int si = 0xFFFFFFFF;
132 tmp.Printf( _T("Signed int: %d\n"), si );
133 textCtrl.WriteText( tmp );
134 text_output << si << "\n";
135 std_file_output << si << "\n";
136
137 unsigned int ui = 0xFFFFFFFF;
138 tmp.Printf( _T("Unsigned int: %u\n"), ui );
139 textCtrl.WriteText( tmp );
140 text_output << ui << "\n";
141 std_file_output << ui << "\n";
142
143 double d = 2.01234567890123456789;
144 tmp.Printf( _T("Double: %f\n"), d );
145 textCtrl.WriteText( tmp );
146 text_output << d << "\n";
147 std_file_output << d << "\n";
148
149 float f = (float)0.00001;
150 tmp.Printf( _T("Float: %f\n"), f );
151 textCtrl.WriteText( tmp );
152 text_output << f << "\n";
153 std_file_output << f << "\n";
154
155 wxString str( _T("Hello!") );
156 tmp.Printf( _T("String: %s\n"), str.c_str() );
157 textCtrl.WriteText( tmp );
158 text_output << str << "\n";
159 std_file_output << str.c_str() << "\n";
160
161 textCtrl.WriteText( "\nReading from ifstream:\n" );
162
163 ifstream std_file_input( "test_std.dat" );
164
165 std_file_input >> si;
166 tmp.Printf( _T("Signed int: %d\n"), si );
167 textCtrl.WriteText( tmp );
168
169 std_file_input >> ui;
170 tmp.Printf( _T("Unsigned int: %u\n"), ui );
171 textCtrl.WriteText( tmp );
172
173 std_file_input >> d;
174 tmp.Printf( _T("Double: %f\n"), d );
175 textCtrl.WriteText( tmp );
176
177 std_file_input >> f;
178 tmp.Printf( _T("Float: %f\n"), f );
179 textCtrl.WriteText( tmp );
180
181 // This doesn't compile (at least with VC++ 4)
182 #if 0
183 std_file_input >> str;
184 tmp.Printf( _T("String: %s\n"), str.c_str() );
185 textCtrl.WriteText( tmp );
186 #endif
187
188 textCtrl.WriteText( "\nReading from wxFileInputStream:\n" );
189
190 buf_output.Sync();
191
192 wxFileInputStream file_input( wxString("test_wx.dat") );
193 wxBufferedInputStream buf_input( file_input );
194 wxTextInputStream text_input( file_input );
195
196 text_input >> si;
197 tmp.Printf( _T("Signed int: %d\n"), si );
198 textCtrl.WriteText( tmp );
199
200 text_input >> ui;
201 tmp.Printf( _T("Unsigned int: %u\n"), ui );
202 textCtrl.WriteText( tmp );
203
204 text_input >> d;
205 tmp.Printf( _T("Double: %f\n"), d );
206 textCtrl.WriteText( tmp );
207
208 text_input >> f;
209 tmp.Printf( _T("Float: %f\n"), f );
210 textCtrl.WriteText( tmp );
211
212 text_input >> str;
213 tmp.Printf( _T("String: %s\n"), str.c_str() );
214 textCtrl.WriteText( tmp );
215
216
217 textCtrl << "\nTest for wxDataStream:\n\n";
218
219 textCtrl.WriteText( "Writing to wxDataOutputStream:\n" );
220
221 file_output.SeekO( 0 );
222 wxDataOutputStream data_output( buf_output );
223
224 wxInt16 i16 = (short)0xFFFF;
225 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
226 textCtrl.WriteText( tmp );
227 data_output.Write16( i16 );
228
229 wxUint16 ui16 = 0xFFFF;
230 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
231 textCtrl.WriteText( tmp );
232 data_output.Write16( ui16 );
233
234 d = 2.01234567890123456789;
235 tmp.Printf( _T("Double: %f\n"), d );
236 textCtrl.WriteText( tmp );
237 data_output.WriteDouble( d );
238
239 str = "Hello!";
240 tmp.Printf( _T("String: %s\n"), str.c_str() );
241 textCtrl.WriteText( tmp );
242 data_output.WriteString( str );
243
244 buf_output.Sync();
245
246 textCtrl.WriteText( "\nReading from wxDataInputStream:\n" );
247
248 file_input.SeekI( 0 );
249 wxDataInputStream data_input( buf_input );
250
251 i16 = data_input.Read16();
252 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
253 textCtrl.WriteText( tmp );
254
255 ui16 = data_input.Read16();
256 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
257 textCtrl.WriteText( tmp );
258
259 d = data_input.ReadDouble();
260 tmp.Printf( _T("Double: %f\n"), d );
261 textCtrl.WriteText( tmp );
262
263 str = data_input.ReadString();
264 tmp.Printf( _T("String: %s\n"), str.c_str() );
265 textCtrl.WriteText( tmp );
266 }
267
268 void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event))
269 {
270 wxTextCtrl& textCtrl = * GetTextCtrl();
271
272 textCtrl.Clear();
273 textCtrl << _T("\nTesting wxBufferedStream:\n\n");
274
275 char ch,ch2;
276
277 textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" );
278
279 wxFileOutputStream file_output( wxString("test_wx.dat") );
280 wxBufferedOutputStream buf_output( file_output );
281 for (ch = 0; ch < 10; ch++)
282 buf_output.Write( &ch, 1 );
283 buf_output.Sync();
284
285 wxFileInputStream file_input( wxString("test_wx.dat") );
286 for (ch2 = 0; ch2 < 10; ch2++)
287 {
288 file_input.Read( &ch, 1 );
289 textCtrl.WriteText( (char)(ch + '0') );
290 }
291 textCtrl.WriteText( "\n\n\n" );
292
293 textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" );
294 textCtrl.WriteText( "seeking back to #3 and writing 3:\n\n" );
295
296 wxFileOutputStream file_output2( wxString("test_wx2.dat") );
297 wxBufferedOutputStream buf_output2( file_output2 );
298 for (ch = 0; ch < 10; ch++)
299 buf_output2.Write( &ch, 1 );
300 buf_output2.SeekO( 3 );
301 ch = 3;
302 buf_output2.Write( &ch, 1 );
303 buf_output2.Sync();
304
305 wxFileInputStream file_input2( wxString("test_wx2.dat") );
306 for (ch2 = 0; ch2 < 10; ch2++)
307 {
308 file_input2.Read( &ch, 1 );
309 textCtrl.WriteText( (char)(ch + '0') );
310 }
311 textCtrl.WriteText( "\n\n\n" );
312
313 // now append 2000 bytes to file (bigger than buffer)
314 buf_output2.SeekO( 0, wxFromEnd );
315 ch = 1;
316 for (int i = 0; i < 2000; i++)
317 buf_output2.Write( &ch, 1 );
318 buf_output2.Sync();
319
320 textCtrl.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" );
321 textCtrl.WriteText( "seeking back to #3 and reading 3:\n\n" );
322
323 wxFileInputStream file_input3( wxString("test_wx2.dat") );
324 wxBufferedInputStream buf_input3( file_input3 );
325 for (ch2 = 0; ch2 < 10; ch2++)
326 {
327 buf_input3.Read( &ch, 1 );
328 textCtrl.WriteText( (char)(ch + '0') );
329 }
330 for (int j = 0; j < 2000; j++)
331 buf_input3.Read( &ch, 1 );
332 textCtrl.WriteText( "\n" );
333 buf_input3.SeekI( 3 );
334 buf_input3.Read( &ch, 1 );
335 textCtrl.WriteText( (char)(ch + '0') );
336 textCtrl.WriteText( "\n\n\n" );
337
338 }
339
340 void MyApp::DoStreamDemo3(wxCommandEvent& WXUNUSED(event))
341 {
342 wxTextCtrl& textCtrl = * GetTextCtrl();
343
344 textCtrl.Clear();
345 textCtrl << "\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n";
346
347 char ch,ch2;
348
349 textCtrl.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" );
350
351 wxFileOutputStream file_output( wxString("test_wx.dat") );
352 for (ch = 0; ch < 10; ch++)
353 file_output.Write( &ch, 1 );
354
355 // Testing wxFileInputStream
356
357 textCtrl.WriteText( "Reading 0 to 10 to wxFileInputStream:\n\n" );
358
359 wxFileInputStream file_input( wxString("test_wx.dat") );
360 for (ch2 = 0; ch2 < 11; ch2++)
361 {
362 file_input.Read( &ch, 1 );
363 textCtrl.WriteText( "Value read: " );
364 textCtrl.WriteText( (char)(ch + '0') );
365 textCtrl.WriteText( "; stream.LastError() returns: " );
366 switch (file_input.LastError())
367 {
368 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
369 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
370 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
371 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
372 default: textCtrl.WriteText( "Huh?\n" ); break;
373 }
374 }
375 textCtrl.WriteText( "\n" );
376
377 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
378 file_input.SeekI( 0 );
379 switch (file_input.LastError())
380 {
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;
386 }
387 textCtrl.WriteText( "\n" );
388
389 file_input.Read( &ch, 1 );
390 textCtrl.WriteText( "Value read: " );
391 textCtrl.WriteText( (char)(ch + '0') );
392 textCtrl.WriteText( "; stream.LastError() returns: " );
393 switch (file_input.LastError())
394 {
395 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
396 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
397 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
398 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
399 default: textCtrl.WriteText( "Huh?\n" ); break;
400 }
401 textCtrl.WriteText( "\n\n" );
402
403
404 // Testing wxFFileInputStream
405
406 textCtrl.WriteText( "Reading 0 to 10 to wxFFileInputStream:\n\n" );
407
408 wxFFileInputStream ffile_input( wxString("test_wx.dat") );
409 for (ch2 = 0; ch2 < 11; ch2++)
410 {
411 ffile_input.Read( &ch, 1 );
412 textCtrl.WriteText( "Value read: " );
413 textCtrl.WriteText( (char)(ch + '0') );
414 textCtrl.WriteText( "; stream.LastError() returns: " );
415 switch (ffile_input.LastError())
416 {
417 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
418 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
419 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
420 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
421 default: textCtrl.WriteText( "Huh?\n" ); break;
422 }
423 }
424 textCtrl.WriteText( "\n" );
425
426 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
427 ffile_input.SeekI( 0 );
428 switch (ffile_input.LastError())
429 {
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;
435 }
436 textCtrl.WriteText( "\n" );
437
438 ffile_input.Read( &ch, 1 );
439 textCtrl.WriteText( "Value read: " );
440 textCtrl.WriteText( (char)(ch + '0') );
441 textCtrl.WriteText( "; stream.LastError() returns: " );
442 switch (ffile_input.LastError())
443 {
444 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
445 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
446 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
447 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
448 default: textCtrl.WriteText( "Huh?\n" ); break;
449 }
450 textCtrl.WriteText( "\n\n" );
451
452 // Testing wxFFileInputStream
453
454 textCtrl.WriteText( "Reading 0 to 10 to buffered wxFFileInputStream:\n\n" );
455
456 wxFFileInputStream ffile_input2( wxString("test_wx.dat") );
457 wxBufferedInputStream buf_input( ffile_input2 );
458 for (ch2 = 0; ch2 < 11; ch2++)
459 {
460 buf_input.Read( &ch, 1 );
461 textCtrl.WriteText( "Value read: " );
462 textCtrl.WriteText( (char)(ch + '0') );
463 textCtrl.WriteText( "; stream.LastError() returns: " );
464 switch (buf_input.LastError())
465 {
466 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
467 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
468 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
469 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
470 default: textCtrl.WriteText( "Huh?\n" ); break;
471 }
472 }
473 textCtrl.WriteText( "\n" );
474
475 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
476 buf_input.SeekI( 0 );
477 switch (buf_input.LastError())
478 {
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;
484 }
485 textCtrl.WriteText( "\n" );
486
487 buf_input.Read( &ch, 1 );
488 textCtrl.WriteText( "Value read: " );
489 textCtrl.WriteText( (char)(ch + '0') );
490 textCtrl.WriteText( "; stream.LastError() returns: " );
491 switch (buf_input.LastError())
492 {
493 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
494 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
495 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
496 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
497 default: textCtrl.WriteText( "Huh?\n" ); break;
498 }
499 }
500
501 void MyApp::DoStreamDemo4(wxCommandEvent& WXUNUSED(event))
502 {
503 wxTextCtrl& textCtrl = * GetTextCtrl();
504
505 wxString msg;
506
507 textCtrl.Clear();
508 textCtrl << "\nTesting wxStreamBuffer:\n\n";
509
510 // bigger than buffer
511 textCtrl.WriteText( "Writing 2000x 1 to wxFileOutputStream.\n\n" );
512
513 wxFileOutputStream file_output( wxString("test_wx.dat") );
514 for (int i = 0; i < 2000; i++)
515 {
516 char ch = 1;
517 file_output.Write( &ch, 1 );
518 }
519
520 textCtrl.WriteText( "Opening with a buffered wxFileInputStream:\n\n" );
521
522 wxFileInputStream file_input( wxString("test_wx.dat") );
523 wxBufferedInputStream buf_input( file_input );
524
525 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
526 switch (buf_input.LastError())
527 {
528 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
529 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
530 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
531 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
532 default: textCtrl.WriteText( "Huh?\n" ); break;
533 }
534 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
535 textCtrl.WriteText( msg );
536 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
537 textCtrl.WriteText( msg );
538 textCtrl.WriteText( "\n\n" );
539
540
541 textCtrl.WriteText( "Seeking to position 300:\n\n" );
542
543 buf_input.SeekI( 300 );
544
545 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
546 switch (buf_input.LastError())
547 {
548 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
549 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
550 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
551 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
552 default: textCtrl.WriteText( "Huh?\n" ); break;
553 }
554 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
555 textCtrl.WriteText( msg );
556 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
557 textCtrl.WriteText( msg );
558 textCtrl.WriteText( "\n\n" );
559
560
561 char buf[2000];
562
563 textCtrl.WriteText( "Reading 500 bytes:\n\n" );
564
565 buf_input.Read( buf, 500 );
566
567 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
568 switch (buf_input.LastError())
569 {
570 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
571 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
572 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
573 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
574 default: textCtrl.WriteText( "Huh?\n" ); break;
575 }
576 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
577 textCtrl.WriteText( msg );
578 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
579 textCtrl.WriteText( msg );
580 textCtrl.WriteText( "\n\n" );
581
582 textCtrl.WriteText( "Reading another 500 bytes:\n\n" );
583
584 buf_input.Read( buf, 500 );
585
586 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
587 switch (buf_input.LastError())
588 {
589 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
590 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
591 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
592 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
593 default: textCtrl.WriteText( "Huh?\n" ); break;
594 }
595 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
596 textCtrl.WriteText( msg );
597 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
598 textCtrl.WriteText( msg );
599 textCtrl.WriteText( "\n\n" );
600
601 textCtrl.WriteText( "Reading another 500 bytes:\n\n" );
602
603 buf_input.Read( buf, 500 );
604
605 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
606 switch (buf_input.LastError())
607 {
608 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
609 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
610 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
611 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
612 default: textCtrl.WriteText( "Huh?\n" ); break;
613 }
614 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
615 textCtrl.WriteText( msg );
616 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
617 textCtrl.WriteText( msg );
618 textCtrl.WriteText( "\n\n" );
619
620 textCtrl.WriteText( "Reading another 500 bytes:\n\n" );
621
622 buf_input.Read( buf, 500 );
623
624 textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " );
625 switch (buf_input.LastError())
626 {
627 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
628 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
629 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
630 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
631 default: textCtrl.WriteText( "Huh?\n" ); break;
632 }
633 msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() );
634 textCtrl.WriteText( msg );
635 msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() );
636 textCtrl.WriteText( msg );
637 textCtrl.WriteText( "\n\n" );
638 }
639
640 #if wxUSE_UNICODE
641 void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
642 {
643 wxTextCtrl& textCtrl = * GetTextCtrl();
644
645 textCtrl.Clear();
646 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
647
648 wxString str;
649 str = _T("Robert Röbling\n");
650
651 printf( "\n\nConversion with wxConvLocal:\n" );
652 wxConvCurrent = &wxConvLocal;
653 printf( (const char*) str.mbc_str() );
654
655 printf( "\n\nConversion with wxConvGdk:\n" );
656 wxConvCurrent = &wxConvGdk;
657 printf( (const char*) str.mbc_str() );
658
659 printf( "\n\nConversion with wxConvLibc:\n" );
660 wxConvCurrent = &wxConvLibc;
661 printf( (const char*) str.mbc_str() );
662
663 }
664 #endif
665
666 void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
667 {
668 static wxString s_defaultExt = "xyz";
669
670 wxString ext = wxGetTextFromUser("Enter a file extension: ",
671 "MIME database test",
672 s_defaultExt);
673 if ( !!ext )
674 {
675 s_defaultExt = ext;
676
677 // init MIME database if not done yet
678 if ( !m_mimeDatabase )
679 {
680 m_mimeDatabase = new wxMimeTypesManager;
681
682 static const wxFileTypeInfo fallbacks[] =
683 {
684 wxFileTypeInfo("application/xyz",
685 "XyZ %s",
686 "XyZ -p %s",
687 "The one and only XYZ format file",
688 "xyz", "123", NULL),
689 wxFileTypeInfo("text/html",
690 "lynx %s",
691 "lynx -dump %s | lpr",
692 "HTML document (from fallback)",
693 "htm", "html", NULL),
694
695 // must terminate the table with this!
696 wxFileTypeInfo()
697 };
698
699 m_mimeDatabase->AddFallbacks(fallbacks);
700 }
701
702 wxTextCtrl& textCtrl = * GetTextCtrl();
703
704 wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext);
705 if ( !filetype )
706 {
707 textCtrl << "Unknown extension '" << ext << "'\n";
708 }
709 else
710 {
711 wxString type, desc, open;
712 filetype->GetMimeType(&type);
713 filetype->GetDescription(&desc);
714
715 wxString filename = "filename";
716 filename << "." << ext;
717 wxFileType::MessageParameters params(filename, type);
718 filetype->GetOpenCommand(&open, params);
719
720 textCtrl << "MIME information about extension '" << ext << "'\n"
721 << "\tMIME type: " << ( !type ? "unknown"
722 : type.c_str() ) << '\n'
723 << "\tDescription: " << ( !desc ? "" : desc.c_str() )
724 << '\n'
725 << "\tCommand to open: " << ( !open ? "no" : open.c_str() )
726 << '\n';
727
728 delete filetype;
729 }
730 }
731 //else: cancelled by user
732 }
733
734 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
735 {
736 wxTextCtrl& textCtrl = * GetTextCtrl();
737
738 textCtrl.Clear();
739 textCtrl << "\nTest byte order macros:\n\n";
740
741 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
742 textCtrl << "This is a little endian system.\n\n";
743 else
744 textCtrl << "This is a big endian system.\n\n";
745
746 wxString text;
747
748 wxInt32 var = 0xF1F2F3F4;
749 text = "";
750 text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var );
751 textCtrl.WriteText( text );
752
753 text = "";
754 text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) );
755 textCtrl.WriteText( text );
756
757 text = "";
758 text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) );
759 textCtrl.WriteText( text );
760
761 text = "";
762 text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) );
763 textCtrl.WriteText( text );
764 }
765
766 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
767 {
768 wxTextCtrl& textCtrl = * GetTextCtrl();
769
770 textCtrl.Clear();
771 textCtrl << "\nTest class wxTime:\n";
772 wxTime now;
773 textCtrl << "It is now " << (wxString) now << "\n";
774 }
775
776 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
777 {
778 wxTextCtrl& textCtrl = * GetTextCtrl();
779
780 textCtrl.Clear();
781 textCtrl << "\nTest class wxDate" << "\n";
782
783 // Various versions of the constructors
784 // and various output
785
786 wxDate x(10,20,1962);
787
788 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
789
790 // constuctor with a string, just printing the day of the week
791 wxDate y("8/8/1988");
792
793 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
794
795 // constructor with a julian
796 wxDate z( 2450000L );
797 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
798
799 // using date addition and subtraction
800 wxDate a = x + 10;
801 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
802 a = a - 25;
803 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
804
805 // Using subtraction of two date objects
806 wxDate a1 = wxString("7/13/1991");
807 wxDate a2 = a1 + 14;
808 textCtrl << (a1-a2) << "\n";
809 textCtrl << (a2+=10) << "\n";
810
811 a1++;
812 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
813
814 wxDate tmpDate1("08/01/1991");
815 wxDate tmpDate2("07/14/1991");
816 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
817 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
818 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
819
820 wxDate a3 = a1;
821 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
822 wxDate a4 = a1;
823 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
824
825 wxDate a5 = wxString("today");
826 textCtrl << "Today is: " << a5 << "\n";
827 a4 = "TODAY";
828 textCtrl << "Today (a4) is: " << a4 << "\n";
829
830 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
831 a4 = "TODAY";
832 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
833
834 textCtrl << "=========== Leap Year Test ===========\n";
835 a1 = "1/15/1992";
836 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
837 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
838
839 a1 = "2/16/1993";
840 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
841 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
842
843 textCtrl << "================== string assignment test ====================\n";
844 wxString date_string=a1;
845 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
846
847 textCtrl << "================== SetFormat test ============================\n";
848 a1.SetFormat(wxFULL);
849 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
850 a1.SetFormat(wxEUROPEAN);
851 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
852
853 textCtrl << "================== SetOption test ============================\n";
854 textCtrl << "Date abbreviation ON\n";
855
856 a1.SetOption(wxDATE_ABBR);
857 a1.SetFormat(wxMONTH);
858 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
859 a1.SetFormat(wxDAY);
860 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
861 a1.SetFormat(wxFULL);
862 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
863 a1.SetFormat(wxEUROPEAN);
864 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
865 textCtrl << "Century suppression ON\n";
866 a1.SetOption(wxNO_CENTURY);
867 a1.SetFormat(wxMDY);
868 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
869 textCtrl << "Century suppression OFF\n";
870 a1.SetOption(wxNO_CENTURY,FALSE);
871 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
872 textCtrl << "Century suppression ON\n";
873 a1.SetOption(wxNO_CENTURY);
874 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
875 a1.SetFormat(wxFULL);
876 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
877
878 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
879
880 wxDate v4("11/26/1966");
881 textCtrl << "\n---------- Set Stuff -----------\n";
882 textCtrl << "First, 'Set' to today..." << "\n";
883 textCtrl << "Before 'Set' => " << v4 << "\n";
884 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
885
886 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
887 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
888 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
889 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
890
891 textCtrl << "---------- Add Stuff -----------\n";
892 textCtrl << "Start => " << v4 << "\n";
893 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
894 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
895 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
896
897 textCtrl << "---------- Misc Stuff -----------\n";
898 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
899 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
900 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
901 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
902 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
903 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
904 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
905 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
906 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
907 textCtrl << "The year alone is " << v4.GetYear() << "\n";
908
909 textCtrl << "---------- First and Last Stuff -----------\n";
910 v4.Set();
911 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
912 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
913 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
914 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
915 }
916
917 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
918 {
919 wxTextCtrl& textCtrl = * GetTextCtrl();
920
921 wxVariant var1 = "String value";
922 textCtrl << "var1 = " << var1.MakeString() << "\n";
923
924 // Conversion
925 wxString str = var1.MakeString();
926
927 var1 = 123.456;
928 textCtrl << "var1 = " << var1.GetReal() << "\n";
929
930 // Implicit conversion
931 double v = var1;
932
933 var1 = 9876L;
934 textCtrl << "var1 = " << var1.GetLong() << "\n";
935
936 // Implicit conversion
937 long l = var1;
938
939 // suppress compile warnings about unused variables
940 if ( l < v )
941 {
942 ;
943 }
944
945 wxStringList stringList;
946 stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
947 var1 = stringList;
948 textCtrl << "var1 = " << var1.MakeString() << "\n";
949
950 var1.ClearList();
951 var1.Append(wxVariant(1.2345));
952 var1.Append(wxVariant("hello"));
953 var1.Append(wxVariant(54321L));
954
955 textCtrl << "var1 = " << var1.MakeString() << "\n";
956
957 size_t n = var1.GetCount();
958 size_t i;
959 for (i = (size_t) 0; i < n; i++)
960 {
961 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
962 }
963 }
964
965 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
966 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
967 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
968 END_EVENT_TABLE()
969
970 // My frame constructor
971 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
972 const wxPoint& pos, const wxSize& size):
973 wxFrame(parent, -1, title, pos, size)
974 {}
975
976 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
977 {
978 Close(TRUE);
979 }
980
981 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
982 {
983 wxMessageDialog dialog(this, "Tests various wxWindows types",
984 "About Types", wxYES_NO|wxCANCEL);
985
986 dialog.ShowModal();
987 }
988
989