Added wxExtDialog and sample.
[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
31 #include "typetest.h"
32
33 #if defined(__WXGTK__) || defined(__WXMOTIF__)
34 #include "mondrian.xpm"
35 #endif
36
37 #include "wx/ioswrap.h"
38
39 #if wxUSE_IOSTREAMH
40 #include <fstream.h>
41 #else
42 #include <fstream>
43 #endif
44
45 #include "wx/wfstream.h"
46 #include "wx/datstrm.h"
47
48
49 // Create a new application object
50 IMPLEMENT_APP (MyApp)
51
52 IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp)
53
54 BEGIN_EVENT_TABLE(MyApp, wxApp)
55 EVT_MENU(TYPES_DATE, MyApp::DoDateDemo)
56 EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo)
57 EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo)
58 EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo)
59 #if wxUSE_UNICODE
60 EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
61 #endif
62 EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
63 END_EVENT_TABLE()
64
65 bool MyApp::OnInit(void)
66 {
67 // Create the main frame window
68 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
69 wxPoint(50, 50), wxSize(450, 340));
70
71 // Give it an icon
72 frame->SetIcon(wxICON(mondrian));
73
74 // Make a menubar
75 wxMenu *file_menu = new wxMenu;
76
77 file_menu->Append(TYPES_ABOUT, "&About");
78 file_menu->AppendSeparator();
79 file_menu->Append(TYPES_DATE, "&Date test");
80 file_menu->Append(TYPES_TIME, "&Time test");
81 file_menu->Append(TYPES_VARIANT, "&Variant test");
82 file_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
83 #if wxUSE_UNICODE
84 file_menu->Append(TYPES_UNICODE, "&Unicode test");
85 #endif
86 file_menu->Append(TYPES_STREAM, "&Stream test");
87 file_menu->AppendSeparator();
88 file_menu->Append(TYPES_QUIT, "E&xit");
89 wxMenuBar *menu_bar = new wxMenuBar;
90 menu_bar->Append(file_menu, "&File");
91 frame->SetMenuBar(menu_bar);
92
93 m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
94
95 // Show the frame
96 frame->Show(TRUE);
97
98 SetTopWindow(frame);
99
100 return TRUE;
101 }
102
103 void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
104 {
105 wxTextCtrl& textCtrl = * GetTextCtrl();
106
107 textCtrl.Clear();
108 textCtrl << "\nTest fstream vs. wxFileStream:\n\n";
109
110 textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
111
112 ofstream std_file_output( "test_std.dat" );
113 wxFileOutputStream file_output( "test_wx.dat" );
114
115 wxString tmp;
116 signed int si = 0xFFFFFFFF;
117 tmp.Printf( "Signed int: %d\n", si );
118 textCtrl.WriteText( tmp );
119 file_output << si << "\n";
120 std_file_output << si << "\n";
121
122 unsigned int ui = 0xFFFFFFFF;
123 tmp.Printf( "Unsigned int: %u\n", ui );
124 textCtrl.WriteText( tmp );
125 file_output << ui << "\n";
126 std_file_output << ui << "\n";
127
128 double d = 2.01234567890123456789;
129 tmp.Printf( "Double: %f\n", d );
130 textCtrl.WriteText( tmp );
131 file_output << d << "\n";
132 std_file_output << d << "\n";
133
134 float f = 0.00001;
135 tmp.Printf( "Float: %f\n", f );
136 textCtrl.WriteText( tmp );
137 file_output << f << "\n";
138 std_file_output << f << "\n";
139
140 wxString str( "Hello!" );
141 tmp.Printf( "String: %s\n", str.c_str() );
142 textCtrl.WriteText( tmp );
143 file_output << str << "\n";
144 std_file_output << str.c_str() << "\n";
145
146 textCtrl.WriteText( "\nReading from ifstream:\n" );
147
148 ifstream std_file_input( "test_std.dat" );
149
150 std_file_input >> si;
151 tmp.Printf( "Signed int: %d\n", si );
152 textCtrl.WriteText( tmp );
153
154 std_file_input >> ui;
155 tmp.Printf( "Unsigned int: %u\n", ui );
156 textCtrl.WriteText( tmp );
157
158 std_file_input >> d;
159 tmp.Printf( "Double: %f\n", d );
160 textCtrl.WriteText( tmp );
161
162 std_file_input >> f;
163 tmp.Printf( "Float: %f\n", f );
164 textCtrl.WriteText( tmp );
165
166 std_file_input >> str;
167 tmp.Printf( "String: %s\n", str.c_str() );
168 textCtrl.WriteText( tmp );
169
170 textCtrl.WriteText( "\nReading from wxFileInputStream:\n" );
171
172 file_output.OutputStreamBuffer()->FlushBuffer();
173
174 wxFileInputStream file_input( "test_wx.dat" );
175
176 file_input >> si;
177 tmp.Printf( "Signed int: %d\n", si );
178 textCtrl.WriteText( tmp );
179
180 file_input >> ui;
181 tmp.Printf( "Unsigned int: %u\n", ui );
182 textCtrl.WriteText( tmp );
183
184 file_input >> d;
185 tmp.Printf( "Double: %f\n", d );
186 textCtrl.WriteText( tmp );
187
188 file_input >> f;
189 tmp.Printf( "Float: %f\n", f );
190 textCtrl.WriteText( tmp );
191
192 file_input >> str;
193 tmp.Printf( "String: %s\n", str.c_str() );
194 textCtrl.WriteText( tmp );
195
196
197 textCtrl << "\nTest for wxDataStream:\n\n";
198
199 textCtrl.WriteText( "Writing to wxDataOutputStream:\n" );
200
201 file_output.SeekO( 0 );
202 wxDataOutputStream data_output( file_output );
203
204 wxInt16 i16 = 0xFFFF;
205 tmp.Printf( "Signed int16: %d\n", (int)i16 );
206 textCtrl.WriteText( tmp );
207 data_output.Write16( i16 );
208
209 wxUint16 ui16 = 0xFFFF;
210 tmp.Printf( "Unsigned int16: %u\n", (unsigned int) ui16 );
211 textCtrl.WriteText( tmp );
212 data_output.Write16( ui16 );
213
214 d = 2.01234567890123456789;
215 tmp.Printf( "Double: %f\n", d );
216 textCtrl.WriteText( tmp );
217 data_output.WriteDouble( d );
218
219 str = "Hello!";
220 tmp.Printf( "String: %s\n", str.c_str() );
221 textCtrl.WriteText( tmp );
222 data_output.WriteString( str );
223
224 file_output.OutputStreamBuffer()->FlushBuffer();
225
226 textCtrl.WriteText( "\nReading from wxDataInputStream:\n" );
227
228 file_input.SeekI( 0 );
229 wxDataInputStream data_input( file_input );
230
231 i16 = data_input.Read16();
232 tmp.Printf( "Signed int16: %d\n", (int)i16 );
233 textCtrl.WriteText( tmp );
234
235 ui16 = data_input.Read16();
236 tmp.Printf( "Unsigned int16: %u\n", (unsigned int) ui16 );
237 textCtrl.WriteText( tmp );
238
239 d = data_input.ReadDouble();
240 tmp.Printf( "Double: %f\n", d );
241 textCtrl.WriteText( tmp );
242
243 str = data_input.ReadString();
244 tmp.Printf( "String: %s\n", str.c_str() );
245 textCtrl.WriteText( tmp );
246 }
247
248 #if wxUSE_UNICODE
249 void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
250 {
251 wxTextCtrl& textCtrl = * GetTextCtrl();
252
253 textCtrl.Clear();
254 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
255
256 wxString str;
257 str = _T("Robert Röbling\n");
258
259 printf( "\n\nConversion with wxConvLocal:\n" );
260 wxConvCurrent = &wxConvLocal;
261 printf( (const char*) str.mbc_str() );
262
263 printf( "\n\nConversion with wxConvGdk:\n" );
264 wxConvCurrent = &wxConvGdk;
265 printf( (const char*) str.mbc_str() );
266
267 printf( "\n\nConversion with wxConvLibc:\n" );
268 wxConvCurrent = &wxConvLibc;
269 printf( (const char*) str.mbc_str() );
270
271 }
272 #endif
273
274 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
275 {
276 wxTextCtrl& textCtrl = * GetTextCtrl();
277
278 textCtrl.Clear();
279 textCtrl << "\nTest byte order macros:\n\n";
280
281 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
282 textCtrl << "This is a little endian system.\n\n";
283 else
284 textCtrl << "This is a big endian system.\n\n";
285
286 wxString text;
287
288 wxInt32 var = 0xF1F2F3F4;
289 text = "";
290 text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var );
291 textCtrl.WriteText( text );
292
293 text = "";
294 text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) );
295 textCtrl.WriteText( text );
296
297 text = "";
298 text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) );
299 textCtrl.WriteText( text );
300
301 text = "";
302 text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) );
303 textCtrl.WriteText( text );
304 }
305
306 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
307 {
308 wxTextCtrl& textCtrl = * GetTextCtrl();
309
310 textCtrl.Clear();
311 textCtrl << "\nTest class wxTime:\n";
312 wxTime now;
313 textCtrl << "It is now " << (wxString) now << "\n";
314 }
315
316 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
317 {
318 wxTextCtrl& textCtrl = * GetTextCtrl();
319
320 textCtrl.Clear();
321 textCtrl << "\nTest class wxDate" << "\n";
322
323 // Various versions of the constructors
324 // and various output
325
326 wxDate x(10,20,1962);
327
328 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
329
330 // constuctor with a string, just printing the day of the week
331 wxDate y("8/8/1988");
332
333 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
334
335 // constructor with a julian
336 wxDate z( 2450000L );
337 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
338
339 // using date addition and subtraction
340 wxDate a = x + 10;
341 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
342 a = a - 25;
343 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
344
345
346 // Using subtraction of two date objects
347 wxDate a1 = wxString("7/13/1991");
348 wxDate a2 = a1 + 14;
349 textCtrl << (a1-a2) << "\n";
350 textCtrl << (a2+=10) << "\n";
351
352 a1++;
353 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
354
355 wxDate tmpDate1("08/01/1991");
356 wxDate tmpDate2("07/14/1991");
357 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
358 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
359 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
360
361 wxDate a3 = a1;
362 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
363 wxDate a4 = a1;
364 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
365
366 wxDate a5 = wxString("today");
367 textCtrl << "Today is: " << a5 << "\n";
368 a4 = "TODAY";
369 textCtrl << "Today (a4) is: " << a4 << "\n";
370
371 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
372 a4 = "TODAY";
373 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
374
375 textCtrl << "=========== Leap Year Test ===========\n";
376 a1 = "1/15/1992";
377 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
378 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
379
380 a1 = "2/16/1993";
381 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
382 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
383
384 textCtrl << "================== string assignment test ====================\n";
385 wxString date_string=a1;
386 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
387
388 textCtrl << "================== SetFormat test ============================\n";
389 a1.SetFormat(wxFULL);
390 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
391 a1.SetFormat(wxEUROPEAN);
392 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
393
394 textCtrl << "================== SetOption test ============================\n";
395 textCtrl << "Date abbreviation ON\n";
396
397 a1.SetOption(wxDATE_ABBR);
398 a1.SetFormat(wxMONTH);
399 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
400 a1.SetFormat(wxDAY);
401 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
402 a1.SetFormat(wxFULL);
403 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
404 a1.SetFormat(wxEUROPEAN);
405 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
406 textCtrl << "Century suppression ON\n";
407 a1.SetOption(wxNO_CENTURY);
408 a1.SetFormat(wxMDY);
409 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
410 textCtrl << "Century suppression OFF\n";
411 a1.SetOption(wxNO_CENTURY,FALSE);
412 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
413 textCtrl << "Century suppression ON\n";
414 a1.SetOption(wxNO_CENTURY);
415 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
416 a1.SetFormat(wxFULL);
417 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
418
419 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
420
421 wxDate v4("11/26/1966");
422 textCtrl << "\n---------- Set Stuff -----------\n";
423 textCtrl << "First, 'Set' to today..." << "\n";
424 textCtrl << "Before 'Set' => " << v4 << "\n";
425 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
426
427 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
428 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
429 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
430 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
431
432 textCtrl << "---------- Add Stuff -----------\n";
433 textCtrl << "Start => " << v4 << "\n";
434 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
435 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
436 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
437
438 textCtrl << "---------- Misc Stuff -----------\n";
439 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
440 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
441 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
442 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
443 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
444 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
445 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
446 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
447 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
448 textCtrl << "The year alone is " << v4.GetYear() << "\n";
449
450 textCtrl << "---------- First and Last Stuff -----------\n";
451 v4.Set();
452 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
453 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
454 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
455 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
456 }
457
458 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
459 {
460 wxTextCtrl& textCtrl = * GetTextCtrl();
461
462 wxVariant var1 = "String value";
463 textCtrl << "var1 = " << var1.MakeString() << "\n";
464
465 // Conversion
466 wxString str = var1.MakeString();
467
468 var1 = 123.456;
469 textCtrl << "var1 = " << var1.GetReal() << "\n";
470
471 // Implicit conversion
472 double v = var1;
473
474 var1 = 9876L;
475 textCtrl << "var1 = " << var1.GetLong() << "\n";
476
477 // Implicit conversion
478 long l = var1;
479
480 wxStringList stringList;
481 stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
482 var1 = stringList;
483 textCtrl << "var1 = " << var1.MakeString() << "\n";
484
485 var1.ClearList();
486 var1.Append(wxVariant(1.2345));
487 var1.Append(wxVariant("hello"));
488 var1.Append(wxVariant(54321L));
489
490 textCtrl << "var1 = " << var1.MakeString() << "\n";
491
492 size_t n = var1.GetCount();
493 size_t i;
494 for (i = (size_t) 0; i < n; i++)
495 {
496 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
497 }
498 }
499
500 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
501 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
502 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
503 END_EVENT_TABLE()
504
505 // My frame constructor
506 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
507 const wxPoint& pos, const wxSize& size):
508 wxFrame(parent, -1, title, pos, size)
509 {}
510
511 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
512 {
513 Close(TRUE);
514 }
515
516 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
517 {
518 wxMessageDialog dialog(this, "Tests various wxWindows types",
519 "About Types", wxYES_NO|wxCANCEL);
520
521 dialog.ShowModal();
522 }
523
524