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