Stream error testing.
[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_MIME, MyApp::DoMIMEDemo)
67 END_EVENT_TABLE()
68
69 bool MyApp::OnInit()
70 {
71 // Create the main frame window
72 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
73 wxPoint(50, 50), wxSize(450, 340));
74
75 // Give it an icon
76 frame->SetIcon(wxICON(mondrian));
77
78 // Make a menubar
79 wxMenu *file_menu = new wxMenu;
80
81 file_menu->Append(TYPES_ABOUT, "&About");
82 file_menu->AppendSeparator();
83 file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X");
84
85 wxMenu *test_menu = new wxMenu;
86 test_menu->Append(TYPES_DATE, "&Date test");
87 test_menu->Append(TYPES_TIME, "&Time test");
88 test_menu->Append(TYPES_VARIANT, "&Variant test");
89 test_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
90 #if wxUSE_UNICODE
91 test_menu->Append(TYPES_UNICODE, "&Unicode test");
92 #endif
93 test_menu->Append(TYPES_STREAM, "&Stream test");
94 test_menu->Append(TYPES_STREAM2, "&Stream seek test");
95 test_menu->Append(TYPES_STREAM3, "&Stream error test");
96 test_menu->AppendSeparator();
97 test_menu->Append(TYPES_MIME, "&MIME database test");
98
99 wxMenuBar *menu_bar = new wxMenuBar;
100 menu_bar->Append(file_menu, "&File");
101 menu_bar->Append(test_menu, "&Tests");
102 frame->SetMenuBar(menu_bar);
103
104 m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
105
106 // Show the frame
107 frame->Show(TRUE);
108
109 SetTopWindow(frame);
110
111 return TRUE;
112 }
113
114 void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
115 {
116 wxTextCtrl& textCtrl = * GetTextCtrl();
117
118 textCtrl.Clear();
119 textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n");
120
121 textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
122
123 ofstream std_file_output( "test_std.dat" );
124 wxFileOutputStream file_output( "test_wx.dat" );
125 wxBufferedOutputStream buf_output( file_output );
126 wxTextOutputStream text_output( buf_output );
127
128 wxString tmp;
129 signed int si = 0xFFFFFFFF;
130 tmp.Printf( _T("Signed int: %d\n"), si );
131 textCtrl.WriteText( tmp );
132 text_output << si << "\n";
133 std_file_output << si << "\n";
134
135 unsigned int ui = 0xFFFFFFFF;
136 tmp.Printf( _T("Unsigned int: %u\n"), ui );
137 textCtrl.WriteText( tmp );
138 text_output << ui << "\n";
139 std_file_output << ui << "\n";
140
141 double d = 2.01234567890123456789;
142 tmp.Printf( _T("Double: %f\n"), d );
143 textCtrl.WriteText( tmp );
144 text_output << d << "\n";
145 std_file_output << d << "\n";
146
147 float f = (float)0.00001;
148 tmp.Printf( _T("Float: %f\n"), f );
149 textCtrl.WriteText( tmp );
150 text_output << f << "\n";
151 std_file_output << f << "\n";
152
153 wxString str( _T("Hello!") );
154 tmp.Printf( _T("String: %s\n"), str.c_str() );
155 textCtrl.WriteText( tmp );
156 text_output << str << "\n";
157 std_file_output << str.c_str() << "\n";
158
159 textCtrl.WriteText( "\nReading from ifstream:\n" );
160
161 ifstream std_file_input( "test_std.dat" );
162
163 std_file_input >> si;
164 tmp.Printf( _T("Signed int: %d\n"), si );
165 textCtrl.WriteText( tmp );
166
167 std_file_input >> ui;
168 tmp.Printf( _T("Unsigned int: %u\n"), ui );
169 textCtrl.WriteText( tmp );
170
171 std_file_input >> d;
172 tmp.Printf( _T("Double: %f\n"), d );
173 textCtrl.WriteText( tmp );
174
175 std_file_input >> f;
176 tmp.Printf( _T("Float: %f\n"), f );
177 textCtrl.WriteText( tmp );
178
179 std_file_input >> str;
180 tmp.Printf( _T("String: %s\n"), str.c_str() );
181 textCtrl.WriteText( tmp );
182
183 textCtrl.WriteText( "\nReading from wxFileInputStream:\n" );
184
185 buf_output.Sync();
186
187 wxFileInputStream file_input( "test_wx.dat" );
188 wxBufferedInputStream buf_input( file_input );
189 wxTextInputStream text_input( buf_input );
190
191 text_input >> si;
192 tmp.Printf( _T("Signed int: %d\n"), si );
193 textCtrl.WriteText( tmp );
194
195 text_input >> ui;
196 tmp.Printf( _T("Unsigned int: %u\n"), ui );
197 textCtrl.WriteText( tmp );
198
199 text_input >> d;
200 tmp.Printf( _T("Double: %f\n"), d );
201 textCtrl.WriteText( tmp );
202
203 text_input >> f;
204 tmp.Printf( _T("Float: %f\n"), f );
205 textCtrl.WriteText( tmp );
206
207 text_input >> str;
208 tmp.Printf( _T("String: %s\n"), str.c_str() );
209 textCtrl.WriteText( tmp );
210
211
212 textCtrl << "\nTest for wxDataStream:\n\n";
213
214 textCtrl.WriteText( "Writing to wxDataOutputStream:\n" );
215
216 file_output.SeekO( 0 );
217 wxDataOutputStream data_output( buf_output );
218
219 wxInt16 i16 = (short)0xFFFF;
220 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
221 textCtrl.WriteText( tmp );
222 data_output.Write16( i16 );
223
224 wxUint16 ui16 = 0xFFFF;
225 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
226 textCtrl.WriteText( tmp );
227 data_output.Write16( ui16 );
228
229 d = 2.01234567890123456789;
230 tmp.Printf( _T("Double: %f\n"), d );
231 textCtrl.WriteText( tmp );
232 data_output.WriteDouble( d );
233
234 str = "Hello!";
235 tmp.Printf( _T("String: %s\n"), str.c_str() );
236 textCtrl.WriteText( tmp );
237 data_output.WriteString( str );
238
239 buf_output.Sync();
240
241 textCtrl.WriteText( "\nReading from wxDataInputStream:\n" );
242
243 file_input.SeekI( 0 );
244 wxDataInputStream data_input( buf_input );
245
246 i16 = data_input.Read16();
247 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
248 textCtrl.WriteText( tmp );
249
250 ui16 = data_input.Read16();
251 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
252 textCtrl.WriteText( tmp );
253
254 d = data_input.ReadDouble();
255 tmp.Printf( _T("Double: %f\n"), d );
256 textCtrl.WriteText( tmp );
257
258 str = data_input.ReadString();
259 tmp.Printf( _T("String: %s\n"), str.c_str() );
260 textCtrl.WriteText( tmp );
261 }
262
263 void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event))
264 {
265 wxTextCtrl& textCtrl = * GetTextCtrl();
266
267 textCtrl.Clear();
268 textCtrl << _T("\nTesting wxBufferedStream:\n\n");
269
270 char ch,ch2;
271
272 textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" );
273
274 wxFileOutputStream file_output( "test_wx.dat" );
275 wxBufferedOutputStream buf_output( file_output );
276 for (ch = 0; ch < 10; ch++)
277 buf_output.Write( &ch, 1 );
278 buf_output.Sync();
279
280 wxFileInputStream file_input( "test_wx.dat" );
281 for (ch2 = 0; ch2 < 10; ch2++)
282 {
283 file_input.Read( &ch, 1 );
284 textCtrl.WriteText( (char)(ch + '0') );
285 }
286 textCtrl.WriteText( "\n\n\n" );
287
288 textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" );
289 textCtrl.WriteText( "seeking back to #3 and writing 3:\n\n" );
290
291 wxFileOutputStream file_output2( "test_wx2.dat" );
292 wxBufferedOutputStream buf_output2( file_output2 );
293 for (ch = 0; ch < 10; ch++)
294 buf_output2.Write( &ch, 1 );
295 buf_output2.SeekO( 3 );
296 ch = 3;
297 buf_output2.Write( &ch, 1 );
298 buf_output2.Sync();
299
300 wxFileInputStream file_input2( "test_wx2.dat" );
301 for (ch2 = 0; ch2 < 10; ch2++)
302 {
303 file_input2.Read( &ch, 1 );
304 textCtrl.WriteText( (char)(ch + '0') );
305 }
306 textCtrl.WriteText( "\n\n\n" );
307
308 // now append 2000 bytes to file (bigger than buffer)
309 buf_output2.SeekO( 0, wxFromEnd );
310 ch = 1;
311 for (int i = 0; i < 2000; i++)
312 buf_output2.Write( &ch, 1 );
313 buf_output2.Sync();
314
315 textCtrl.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" );
316 textCtrl.WriteText( "seeking back to #3 and reading 3:\n\n" );
317
318 wxFileInputStream file_input3( "test_wx2.dat" );
319 wxBufferedInputStream buf_input3( file_input3 );
320 for (ch2 = 0; ch2 < 10; ch2++)
321 {
322 buf_input3.Read( &ch, 1 );
323 textCtrl.WriteText( (char)(ch + '0') );
324 }
325 for (int j = 0; j < 2000; j++)
326 buf_input3.Read( &ch, 1 );
327 textCtrl.WriteText( "\n" );
328 buf_input3.SeekI( 3 );
329 buf_input3.Read( &ch, 1 );
330 textCtrl.WriteText( (char)(ch + '0') );
331 textCtrl.WriteText( "\n\n\n" );
332
333 }
334
335 void MyApp::DoStreamDemo3(wxCommandEvent& WXUNUSED(event))
336 {
337 wxTextCtrl& textCtrl = * GetTextCtrl();
338
339 textCtrl.Clear();
340 textCtrl << "\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n";
341
342 char ch,ch2;
343
344 textCtrl.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" );
345
346 wxFileOutputStream file_output( "test_wx.dat" );
347 for (ch = 0; ch < 10; ch++)
348 file_output.Write( &ch, 1 );
349
350 // Testing wxFileInoutStream
351
352 textCtrl.WriteText( "Reading 0 to 10 to wxFileInputStream:\n\n" );
353
354 wxFileInputStream file_input( "test_wx.dat" );
355 for (ch2 = 0; ch2 < 11; ch2++)
356 {
357 file_input.Read( &ch, 1 );
358 textCtrl.WriteText( "Value read: " );
359 textCtrl.WriteText( (char)(ch + '0') );
360 textCtrl.WriteText( "; stream.LastError() returns: " );
361 switch (file_input.LastError())
362 {
363 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
364 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
365 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
366 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
367 default: textCtrl.WriteText( "Huh?\n" ); break;
368 }
369 }
370 textCtrl.WriteText( "\n" );
371
372 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
373 file_input.SeekI( 0 );
374 switch (file_input.LastError())
375 {
376 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
377 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
378 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
379 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
380 default: textCtrl.WriteText( "Huh?\n" ); break;
381 }
382 textCtrl.WriteText( "\n" );
383
384 file_input.Read( &ch, 1 );
385 textCtrl.WriteText( "Value read: " );
386 textCtrl.WriteText( (char)(ch + '0') );
387 textCtrl.WriteText( "; stream.LastError() returns: " );
388 switch (file_input.LastError())
389 {
390 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
391 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
392 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
393 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
394 default: textCtrl.WriteText( "Huh?\n" ); break;
395 }
396 textCtrl.WriteText( "\n\n" );
397
398
399 // Testing wxFFileInoutStream
400
401 textCtrl.WriteText( "Reading 0 to 10 to wxFFileInputStream:\n\n" );
402
403 wxFFileInputStream ffile_input( "test_wx.dat" );
404 for (ch2 = 0; ch2 < 11; ch2++)
405 {
406 ffile_input.Read( &ch, 1 );
407 textCtrl.WriteText( "Value read: " );
408 textCtrl.WriteText( (char)(ch + '0') );
409 textCtrl.WriteText( "; stream.LastError() returns: " );
410 switch (ffile_input.LastError())
411 {
412 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
413 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
414 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
415 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
416 default: textCtrl.WriteText( "Huh?\n" ); break;
417 }
418 }
419 textCtrl.WriteText( "\n" );
420
421 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
422 ffile_input.SeekI( 0 );
423 switch (ffile_input.LastError())
424 {
425 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
426 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
427 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
428 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
429 default: textCtrl.WriteText( "Huh?\n" ); break;
430 }
431 textCtrl.WriteText( "\n" );
432
433 ffile_input.Read( &ch, 1 );
434 textCtrl.WriteText( "Value read: " );
435 textCtrl.WriteText( (char)(ch + '0') );
436 textCtrl.WriteText( "; stream.LastError() returns: " );
437 switch (ffile_input.LastError())
438 {
439 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
440 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
441 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
442 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
443 default: textCtrl.WriteText( "Huh?\n" ); break;
444 }
445
446 }
447
448 #if wxUSE_UNICODE
449 void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
450 {
451 wxTextCtrl& textCtrl = * GetTextCtrl();
452
453 textCtrl.Clear();
454 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
455
456 wxString str;
457 str = _T("Robert Röbling\n");
458
459 printf( "\n\nConversion with wxConvLocal:\n" );
460 wxConvCurrent = &wxConvLocal;
461 printf( (const char*) str.mbc_str() );
462
463 printf( "\n\nConversion with wxConvGdk:\n" );
464 wxConvCurrent = &wxConvGdk;
465 printf( (const char*) str.mbc_str() );
466
467 printf( "\n\nConversion with wxConvLibc:\n" );
468 wxConvCurrent = &wxConvLibc;
469 printf( (const char*) str.mbc_str() );
470
471 }
472 #endif
473
474 void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
475 {
476 static wxString s_defaultExt = "xyz";
477
478 wxString ext = wxGetTextFromUser("Enter a file extension: ",
479 "MIME database test",
480 s_defaultExt);
481 if ( !!ext )
482 {
483 s_defaultExt = ext;
484
485 // init MIME database if not done yet
486 if ( !m_mimeDatabase )
487 {
488 m_mimeDatabase = new wxMimeTypesManager;
489
490 static const wxFileTypeInfo fallbacks[] =
491 {
492 wxFileTypeInfo("application/xyz",
493 "XyZ %s",
494 "XyZ -p %s",
495 "The one and only XYZ format file",
496 "xyz", "123", NULL),
497 wxFileTypeInfo("text/html",
498 "lynx %s",
499 "lynx -dump %s | lpr",
500 "HTML document (from fallback)",
501 "htm", "html", NULL),
502
503 // must terminate the table with this!
504 wxFileTypeInfo()
505 };
506
507 m_mimeDatabase->AddFallbacks(fallbacks);
508 }
509
510 wxTextCtrl& textCtrl = * GetTextCtrl();
511
512 wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext);
513 if ( !filetype )
514 {
515 textCtrl << "Unknown extension '" << ext << "'\n";
516 }
517 else
518 {
519 wxString type, desc, open;
520 filetype->GetMimeType(&type);
521 filetype->GetDescription(&desc);
522
523 wxString filename = "filename";
524 filename << "." << ext;
525 wxFileType::MessageParameters params(filename, type);
526 filetype->GetOpenCommand(&open, params);
527
528 textCtrl << "MIME information about extension '" << ext << "'\n"
529 << "\tMIME type: " << ( !type ? "unknown"
530 : type.c_str() ) << '\n'
531 << "\tDescription: " << ( !desc ? "" : desc.c_str() )
532 << '\n'
533 << "\tCommand to open: " << ( !open ? "no" : open.c_str() )
534 << '\n';
535
536 delete filetype;
537 }
538 }
539 //else: cancelled by user
540 }
541
542 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
543 {
544 wxTextCtrl& textCtrl = * GetTextCtrl();
545
546 textCtrl.Clear();
547 textCtrl << "\nTest byte order macros:\n\n";
548
549 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
550 textCtrl << "This is a little endian system.\n\n";
551 else
552 textCtrl << "This is a big endian system.\n\n";
553
554 wxString text;
555
556 wxInt32 var = 0xF1F2F3F4;
557 text = "";
558 text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var );
559 textCtrl.WriteText( text );
560
561 text = "";
562 text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) );
563 textCtrl.WriteText( text );
564
565 text = "";
566 text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) );
567 textCtrl.WriteText( text );
568
569 text = "";
570 text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) );
571 textCtrl.WriteText( text );
572 }
573
574 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
575 {
576 wxTextCtrl& textCtrl = * GetTextCtrl();
577
578 textCtrl.Clear();
579 textCtrl << "\nTest class wxTime:\n";
580 wxTime now;
581 textCtrl << "It is now " << (wxString) now << "\n";
582 }
583
584 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
585 {
586 wxTextCtrl& textCtrl = * GetTextCtrl();
587
588 textCtrl.Clear();
589 textCtrl << "\nTest class wxDate" << "\n";
590
591 // Various versions of the constructors
592 // and various output
593
594 wxDate x(10,20,1962);
595
596 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
597
598 // constuctor with a string, just printing the day of the week
599 wxDate y("8/8/1988");
600
601 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
602
603 // constructor with a julian
604 wxDate z( 2450000L );
605 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
606
607 // using date addition and subtraction
608 wxDate a = x + 10;
609 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
610 a = a - 25;
611 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
612
613 // Using subtraction of two date objects
614 wxDate a1 = wxString("7/13/1991");
615 wxDate a2 = a1 + 14;
616 textCtrl << (a1-a2) << "\n";
617 textCtrl << (a2+=10) << "\n";
618
619 a1++;
620 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
621
622 wxDate tmpDate1("08/01/1991");
623 wxDate tmpDate2("07/14/1991");
624 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
625 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
626 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
627
628 wxDate a3 = a1;
629 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
630 wxDate a4 = a1;
631 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
632
633 wxDate a5 = wxString("today");
634 textCtrl << "Today is: " << a5 << "\n";
635 a4 = "TODAY";
636 textCtrl << "Today (a4) is: " << a4 << "\n";
637
638 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
639 a4 = "TODAY";
640 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
641
642 textCtrl << "=========== Leap Year Test ===========\n";
643 a1 = "1/15/1992";
644 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
645 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
646
647 a1 = "2/16/1993";
648 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
649 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
650
651 textCtrl << "================== string assignment test ====================\n";
652 wxString date_string=a1;
653 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
654
655 textCtrl << "================== SetFormat test ============================\n";
656 a1.SetFormat(wxFULL);
657 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
658 a1.SetFormat(wxEUROPEAN);
659 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
660
661 textCtrl << "================== SetOption test ============================\n";
662 textCtrl << "Date abbreviation ON\n";
663
664 a1.SetOption(wxDATE_ABBR);
665 a1.SetFormat(wxMONTH);
666 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
667 a1.SetFormat(wxDAY);
668 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
669 a1.SetFormat(wxFULL);
670 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
671 a1.SetFormat(wxEUROPEAN);
672 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
673 textCtrl << "Century suppression ON\n";
674 a1.SetOption(wxNO_CENTURY);
675 a1.SetFormat(wxMDY);
676 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
677 textCtrl << "Century suppression OFF\n";
678 a1.SetOption(wxNO_CENTURY,FALSE);
679 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
680 textCtrl << "Century suppression ON\n";
681 a1.SetOption(wxNO_CENTURY);
682 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
683 a1.SetFormat(wxFULL);
684 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
685
686 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
687
688 wxDate v4("11/26/1966");
689 textCtrl << "\n---------- Set Stuff -----------\n";
690 textCtrl << "First, 'Set' to today..." << "\n";
691 textCtrl << "Before 'Set' => " << v4 << "\n";
692 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
693
694 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
695 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
696 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
697 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
698
699 textCtrl << "---------- Add Stuff -----------\n";
700 textCtrl << "Start => " << v4 << "\n";
701 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
702 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
703 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
704
705 textCtrl << "---------- Misc Stuff -----------\n";
706 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
707 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
708 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
709 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
710 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
711 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
712 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
713 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
714 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
715 textCtrl << "The year alone is " << v4.GetYear() << "\n";
716
717 textCtrl << "---------- First and Last Stuff -----------\n";
718 v4.Set();
719 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
720 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
721 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
722 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
723 }
724
725 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
726 {
727 wxTextCtrl& textCtrl = * GetTextCtrl();
728
729 wxVariant var1 = "String value";
730 textCtrl << "var1 = " << var1.MakeString() << "\n";
731
732 // Conversion
733 wxString str = var1.MakeString();
734
735 var1 = 123.456;
736 textCtrl << "var1 = " << var1.GetReal() << "\n";
737
738 // Implicit conversion
739 double v = var1;
740
741 var1 = 9876L;
742 textCtrl << "var1 = " << var1.GetLong() << "\n";
743
744 // Implicit conversion
745 long l = var1;
746
747 // suppress compile warnings about unused variables
748 if ( l < v )
749 {
750 ;
751 }
752
753 wxStringList stringList;
754 stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
755 var1 = stringList;
756 textCtrl << "var1 = " << var1.MakeString() << "\n";
757
758 var1.ClearList();
759 var1.Append(wxVariant(1.2345));
760 var1.Append(wxVariant("hello"));
761 var1.Append(wxVariant(54321L));
762
763 textCtrl << "var1 = " << var1.MakeString() << "\n";
764
765 size_t n = var1.GetCount();
766 size_t i;
767 for (i = (size_t) 0; i < n; i++)
768 {
769 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
770 }
771 }
772
773 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
774 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
775 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
776 END_EVENT_TABLE()
777
778 // My frame constructor
779 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
780 const wxPoint& pos, const wxSize& size):
781 wxFrame(parent, -1, title, pos, size)
782 {}
783
784 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
785 {
786 Close(TRUE);
787 }
788
789 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
790 {
791 wxMessageDialog dialog(this, "Tests various wxWindows types",
792 "About Types", wxYES_NO|wxCANCEL);
793
794 dialog.ShowModal();
795 }
796
797