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