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