Documented wxFFile and wxFFileStream and Co.
[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 wxFileInputStream
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 wxFFileInputStream
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 textCtrl.WriteText( "\n\n" );
446
447 // Testing wxFFileInputStream
448
449 textCtrl.WriteText( "Reading 0 to 10 to buffered wxFFileInputStream:\n\n" );
450
451 wxFFileInputStream ffile_input2( "test_wx.dat" );
452 wxBufferedInputStream buf_input( ffile_input2 );
453 for (ch2 = 0; ch2 < 11; ch2++)
454 {
455 buf_input.Read( &ch, 1 );
456 textCtrl.WriteText( "Value read: " );
457 textCtrl.WriteText( (char)(ch + '0') );
458 textCtrl.WriteText( "; stream.LastError() returns: " );
459 switch (buf_input.LastError())
460 {
461 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
462 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
463 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
464 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
465 default: textCtrl.WriteText( "Huh?\n" ); break;
466 }
467 }
468 textCtrl.WriteText( "\n" );
469
470 textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " );
471 buf_input.SeekI( 0 );
472 switch (buf_input.LastError())
473 {
474 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
475 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
476 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
477 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
478 default: textCtrl.WriteText( "Huh?\n" ); break;
479 }
480 textCtrl.WriteText( "\n" );
481
482 buf_input.Read( &ch, 1 );
483 textCtrl.WriteText( "Value read: " );
484 textCtrl.WriteText( (char)(ch + '0') );
485 textCtrl.WriteText( "; stream.LastError() returns: " );
486 switch (buf_input.LastError())
487 {
488 case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break;
489 case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break;
490 case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break;
491 case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break;
492 default: textCtrl.WriteText( "Huh?\n" ); break;
493 }
494
495 }
496
497 #if wxUSE_UNICODE
498 void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
499 {
500 wxTextCtrl& textCtrl = * GetTextCtrl();
501
502 textCtrl.Clear();
503 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
504
505 wxString str;
506 str = _T("Robert Röbling\n");
507
508 printf( "\n\nConversion with wxConvLocal:\n" );
509 wxConvCurrent = &wxConvLocal;
510 printf( (const char*) str.mbc_str() );
511
512 printf( "\n\nConversion with wxConvGdk:\n" );
513 wxConvCurrent = &wxConvGdk;
514 printf( (const char*) str.mbc_str() );
515
516 printf( "\n\nConversion with wxConvLibc:\n" );
517 wxConvCurrent = &wxConvLibc;
518 printf( (const char*) str.mbc_str() );
519
520 }
521 #endif
522
523 void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event))
524 {
525 static wxString s_defaultExt = "xyz";
526
527 wxString ext = wxGetTextFromUser("Enter a file extension: ",
528 "MIME database test",
529 s_defaultExt);
530 if ( !!ext )
531 {
532 s_defaultExt = ext;
533
534 // init MIME database if not done yet
535 if ( !m_mimeDatabase )
536 {
537 m_mimeDatabase = new wxMimeTypesManager;
538
539 static const wxFileTypeInfo fallbacks[] =
540 {
541 wxFileTypeInfo("application/xyz",
542 "XyZ %s",
543 "XyZ -p %s",
544 "The one and only XYZ format file",
545 "xyz", "123", NULL),
546 wxFileTypeInfo("text/html",
547 "lynx %s",
548 "lynx -dump %s | lpr",
549 "HTML document (from fallback)",
550 "htm", "html", NULL),
551
552 // must terminate the table with this!
553 wxFileTypeInfo()
554 };
555
556 m_mimeDatabase->AddFallbacks(fallbacks);
557 }
558
559 wxTextCtrl& textCtrl = * GetTextCtrl();
560
561 wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext);
562 if ( !filetype )
563 {
564 textCtrl << "Unknown extension '" << ext << "'\n";
565 }
566 else
567 {
568 wxString type, desc, open;
569 filetype->GetMimeType(&type);
570 filetype->GetDescription(&desc);
571
572 wxString filename = "filename";
573 filename << "." << ext;
574 wxFileType::MessageParameters params(filename, type);
575 filetype->GetOpenCommand(&open, params);
576
577 textCtrl << "MIME information about extension '" << ext << "'\n"
578 << "\tMIME type: " << ( !type ? "unknown"
579 : type.c_str() ) << '\n'
580 << "\tDescription: " << ( !desc ? "" : desc.c_str() )
581 << '\n'
582 << "\tCommand to open: " << ( !open ? "no" : open.c_str() )
583 << '\n';
584
585 delete filetype;
586 }
587 }
588 //else: cancelled by user
589 }
590
591 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
592 {
593 wxTextCtrl& textCtrl = * GetTextCtrl();
594
595 textCtrl.Clear();
596 textCtrl << "\nTest byte order macros:\n\n";
597
598 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
599 textCtrl << "This is a little endian system.\n\n";
600 else
601 textCtrl << "This is a big endian system.\n\n";
602
603 wxString text;
604
605 wxInt32 var = 0xF1F2F3F4;
606 text = "";
607 text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var );
608 textCtrl.WriteText( text );
609
610 text = "";
611 text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) );
612 textCtrl.WriteText( text );
613
614 text = "";
615 text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) );
616 textCtrl.WriteText( text );
617
618 text = "";
619 text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) );
620 textCtrl.WriteText( text );
621 }
622
623 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
624 {
625 wxTextCtrl& textCtrl = * GetTextCtrl();
626
627 textCtrl.Clear();
628 textCtrl << "\nTest class wxTime:\n";
629 wxTime now;
630 textCtrl << "It is now " << (wxString) now << "\n";
631 }
632
633 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
634 {
635 wxTextCtrl& textCtrl = * GetTextCtrl();
636
637 textCtrl.Clear();
638 textCtrl << "\nTest class wxDate" << "\n";
639
640 // Various versions of the constructors
641 // and various output
642
643 wxDate x(10,20,1962);
644
645 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
646
647 // constuctor with a string, just printing the day of the week
648 wxDate y("8/8/1988");
649
650 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
651
652 // constructor with a julian
653 wxDate z( 2450000L );
654 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
655
656 // using date addition and subtraction
657 wxDate a = x + 10;
658 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
659 a = a - 25;
660 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
661
662 // Using subtraction of two date objects
663 wxDate a1 = wxString("7/13/1991");
664 wxDate a2 = a1 + 14;
665 textCtrl << (a1-a2) << "\n";
666 textCtrl << (a2+=10) << "\n";
667
668 a1++;
669 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
670
671 wxDate tmpDate1("08/01/1991");
672 wxDate tmpDate2("07/14/1991");
673 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
674 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
675 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
676
677 wxDate a3 = a1;
678 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
679 wxDate a4 = a1;
680 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
681
682 wxDate a5 = wxString("today");
683 textCtrl << "Today is: " << a5 << "\n";
684 a4 = "TODAY";
685 textCtrl << "Today (a4) is: " << a4 << "\n";
686
687 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
688 a4 = "TODAY";
689 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
690
691 textCtrl << "=========== Leap Year Test ===========\n";
692 a1 = "1/15/1992";
693 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
694 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
695
696 a1 = "2/16/1993";
697 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
698 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
699
700 textCtrl << "================== string assignment test ====================\n";
701 wxString date_string=a1;
702 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
703
704 textCtrl << "================== SetFormat test ============================\n";
705 a1.SetFormat(wxFULL);
706 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
707 a1.SetFormat(wxEUROPEAN);
708 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
709
710 textCtrl << "================== SetOption test ============================\n";
711 textCtrl << "Date abbreviation ON\n";
712
713 a1.SetOption(wxDATE_ABBR);
714 a1.SetFormat(wxMONTH);
715 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
716 a1.SetFormat(wxDAY);
717 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
718 a1.SetFormat(wxFULL);
719 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
720 a1.SetFormat(wxEUROPEAN);
721 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
722 textCtrl << "Century suppression ON\n";
723 a1.SetOption(wxNO_CENTURY);
724 a1.SetFormat(wxMDY);
725 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
726 textCtrl << "Century suppression OFF\n";
727 a1.SetOption(wxNO_CENTURY,FALSE);
728 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
729 textCtrl << "Century suppression ON\n";
730 a1.SetOption(wxNO_CENTURY);
731 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
732 a1.SetFormat(wxFULL);
733 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
734
735 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
736
737 wxDate v4("11/26/1966");
738 textCtrl << "\n---------- Set Stuff -----------\n";
739 textCtrl << "First, 'Set' to today..." << "\n";
740 textCtrl << "Before 'Set' => " << v4 << "\n";
741 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
742
743 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
744 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
745 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
746 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
747
748 textCtrl << "---------- Add Stuff -----------\n";
749 textCtrl << "Start => " << v4 << "\n";
750 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
751 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
752 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
753
754 textCtrl << "---------- Misc Stuff -----------\n";
755 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
756 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
757 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
758 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
759 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
760 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
761 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
762 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
763 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
764 textCtrl << "The year alone is " << v4.GetYear() << "\n";
765
766 textCtrl << "---------- First and Last Stuff -----------\n";
767 v4.Set();
768 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
769 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
770 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
771 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
772 }
773
774 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
775 {
776 wxTextCtrl& textCtrl = * GetTextCtrl();
777
778 wxVariant var1 = "String value";
779 textCtrl << "var1 = " << var1.MakeString() << "\n";
780
781 // Conversion
782 wxString str = var1.MakeString();
783
784 var1 = 123.456;
785 textCtrl << "var1 = " << var1.GetReal() << "\n";
786
787 // Implicit conversion
788 double v = var1;
789
790 var1 = 9876L;
791 textCtrl << "var1 = " << var1.GetLong() << "\n";
792
793 // Implicit conversion
794 long l = var1;
795
796 // suppress compile warnings about unused variables
797 if ( l < v )
798 {
799 ;
800 }
801
802 wxStringList stringList;
803 stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
804 var1 = stringList;
805 textCtrl << "var1 = " << var1.MakeString() << "\n";
806
807 var1.ClearList();
808 var1.Append(wxVariant(1.2345));
809 var1.Append(wxVariant("hello"));
810 var1.Append(wxVariant(54321L));
811
812 textCtrl << "var1 = " << var1.MakeString() << "\n";
813
814 size_t n = var1.GetCount();
815 size_t i;
816 for (i = (size_t) 0; i < n; i++)
817 {
818 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
819 }
820 }
821
822 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
823 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
824 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
825 END_EVENT_TABLE()
826
827 // My frame constructor
828 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
829 const wxPoint& pos, const wxSize& size):
830 wxFrame(parent, -1, title, pos, size)
831 {}
832
833 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
834 {
835 Close(TRUE);
836 }
837
838 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
839 {
840 wxMessageDialog dialog(this, "Tests various wxWindows types",
841 "About Types", wxYES_NO|wxCANCEL);
842
843 dialog.ShowModal();
844 }
845
846