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