Work on streams of all sorts. More to come.
[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 ofstream std_file_output( "test_std.dat" );
110 wxFileOutputStream file_output( "test_wx.dat" );
111
112 textCtrl.WriteText( "Writig to fstream:\n" );
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 wxString str( "Hello!" );
134 tmp.Printf( "String: %s\n", str.c_str() );
135 textCtrl.WriteText( tmp );
136 file_output << str << "\n";
137 std_file_output << str.c_str() << "\n";
138
139 }
140
141 #if wxUSE_UNICODE
142 void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
143 {
144 wxTextCtrl& textCtrl = * GetTextCtrl();
145
146 textCtrl.Clear();
147 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
148
149 wxString str;
150 str = _T("Robert Röbling\n");
151
152 printf( "\n\nConversion with wxConvLocal:\n" );
153 wxConvCurrent = &wxConvLocal;
154 printf( (const char*) str.mbc_str() );
155
156 printf( "\n\nConversion with wxConvGdk:\n" );
157 wxConvCurrent = &wxConvGdk;
158 printf( (const char*) str.mbc_str() );
159
160 printf( "\n\nConversion with wxConvLibc:\n" );
161 wxConvCurrent = &wxConvLibc;
162 printf( (const char*) str.mbc_str() );
163
164 }
165 #endif
166
167 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
168 {
169 wxTextCtrl& textCtrl = * GetTextCtrl();
170
171 textCtrl.Clear();
172 textCtrl << "\nTest byte order macros:\n\n";
173
174 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
175 textCtrl << "This is a little endian system.\n\n";
176 else
177 textCtrl << "This is a big endian system.\n\n";
178
179 wxString text;
180
181 wxInt32 var = 0xF1F2F3F4;
182 text = "";
183 text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var );
184 textCtrl.WriteText( text );
185
186 text = "";
187 text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) );
188 textCtrl.WriteText( text );
189
190 text = "";
191 text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) );
192 textCtrl.WriteText( text );
193
194 text = "";
195 text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) );
196 textCtrl.WriteText( text );
197 }
198
199 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
200 {
201 wxTextCtrl& textCtrl = * GetTextCtrl();
202
203 textCtrl.Clear();
204 textCtrl << "\nTest class wxTime:\n";
205 wxTime now;
206 textCtrl << "It is now " << (wxString) now << "\n";
207 }
208
209 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
210 {
211 wxTextCtrl& textCtrl = * GetTextCtrl();
212
213 textCtrl.Clear();
214 textCtrl << "\nTest class wxDate" << "\n";
215
216 // Various versions of the constructors
217 // and various output
218
219 wxDate x(10,20,1962);
220
221 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
222
223 // constuctor with a string, just printing the day of the week
224 wxDate y("8/8/1988");
225
226 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
227
228 // constructor with a julian
229 wxDate z( 2450000L );
230 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
231
232 // using date addition and subtraction
233 wxDate a = x + 10;
234 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
235 a = a - 25;
236 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
237
238
239 // Using subtraction of two date objects
240 wxDate a1 = wxString("7/13/1991");
241 wxDate a2 = a1 + 14;
242 textCtrl << (a1-a2) << "\n";
243 textCtrl << (a2+=10) << "\n";
244
245 a1++;
246 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
247
248 wxDate tmpDate1("08/01/1991");
249 wxDate tmpDate2("07/14/1991");
250 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
251 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
252 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
253
254 wxDate a3 = a1;
255 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
256 wxDate a4 = a1;
257 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
258
259 wxDate a5 = wxString("today");
260 textCtrl << "Today is: " << a5 << "\n";
261 a4 = "TODAY";
262 textCtrl << "Today (a4) is: " << a4 << "\n";
263
264 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
265 a4 = "TODAY";
266 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
267
268 textCtrl << "=========== Leap Year Test ===========\n";
269 a1 = "1/15/1992";
270 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
271 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
272
273 a1 = "2/16/1993";
274 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
275 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
276
277 textCtrl << "================== string assignment test ====================\n";
278 wxString date_string=a1;
279 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
280
281 textCtrl << "================== SetFormat test ============================\n";
282 a1.SetFormat(wxFULL);
283 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
284 a1.SetFormat(wxEUROPEAN);
285 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
286
287 textCtrl << "================== SetOption test ============================\n";
288 textCtrl << "Date abbreviation ON\n";
289
290 a1.SetOption(wxDATE_ABBR);
291 a1.SetFormat(wxMONTH);
292 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
293 a1.SetFormat(wxDAY);
294 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
295 a1.SetFormat(wxFULL);
296 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
297 a1.SetFormat(wxEUROPEAN);
298 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
299 textCtrl << "Century suppression ON\n";
300 a1.SetOption(wxNO_CENTURY);
301 a1.SetFormat(wxMDY);
302 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
303 textCtrl << "Century suppression OFF\n";
304 a1.SetOption(wxNO_CENTURY,FALSE);
305 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
306 textCtrl << "Century suppression ON\n";
307 a1.SetOption(wxNO_CENTURY);
308 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
309 a1.SetFormat(wxFULL);
310 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
311
312 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
313
314 wxDate v4("11/26/1966");
315 textCtrl << "\n---------- Set Stuff -----------\n";
316 textCtrl << "First, 'Set' to today..." << "\n";
317 textCtrl << "Before 'Set' => " << v4 << "\n";
318 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
319
320 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
321 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
322 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
323 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
324
325 textCtrl << "---------- Add Stuff -----------\n";
326 textCtrl << "Start => " << v4 << "\n";
327 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
328 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
329 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
330
331 textCtrl << "---------- Misc Stuff -----------\n";
332 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
333 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
334 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
335 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
336 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
337 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
338 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
339 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
340 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
341 textCtrl << "The year alone is " << v4.GetYear() << "\n";
342
343 textCtrl << "---------- First and Last Stuff -----------\n";
344 v4.Set();
345 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
346 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
347 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
348 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
349 }
350
351 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
352 {
353 wxTextCtrl& textCtrl = * GetTextCtrl();
354
355 wxVariant var1 = "String value";
356 textCtrl << "var1 = " << var1.MakeString() << "\n";
357
358 // Conversion
359 wxString str = var1.MakeString();
360
361 var1 = 123.456;
362 textCtrl << "var1 = " << var1.GetReal() << "\n";
363
364 // Implicit conversion
365 double v = var1;
366
367 var1 = 9876L;
368 textCtrl << "var1 = " << var1.GetLong() << "\n";
369
370 // Implicit conversion
371 long l = var1;
372
373 wxStringList stringList;
374 stringList.Add(_T("one")); stringList.Add(_T("two")); stringList.Add(_T("three"));
375 var1 = stringList;
376 textCtrl << "var1 = " << var1.MakeString() << "\n";
377
378 var1.ClearList();
379 var1.Append(wxVariant(1.2345));
380 var1.Append(wxVariant("hello"));
381 var1.Append(wxVariant(54321L));
382
383 textCtrl << "var1 = " << var1.MakeString() << "\n";
384
385 size_t n = var1.GetCount();
386 size_t i;
387 for (i = (size_t) 0; i < n; i++)
388 {
389 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
390 }
391 }
392
393 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
394 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
395 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
396 END_EVENT_TABLE()
397
398 // My frame constructor
399 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
400 const wxPoint& pos, const wxSize& size):
401 wxFrame(parent, -1, title, pos, size)
402 {}
403
404 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
405 {
406 Close(TRUE);
407 }
408
409 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
410 {
411 wxMessageDialog dialog(this, "Tests various wxWindows types",
412 "About Types", wxYES_NO|wxCANCEL);
413
414 dialog.ShowModal();
415 }
416
417