Corrected byte swapping macros.
[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 // Create a new application object
38 IMPLEMENT_APP (MyApp)
39
40 IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp)
41
42 BEGIN_EVENT_TABLE(MyApp, wxApp)
43 EVT_MENU(TYPES_DATE, MyApp::DoDateDemo)
44 EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo)
45 EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo)
46 EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo)
47 END_EVENT_TABLE()
48
49 bool MyApp::OnInit(void)
50 {
51 // Create the main frame window
52 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
53 wxPoint(50, 50), wxSize(450, 340));
54
55 // Give it an icon
56 frame->SetIcon(wxICON(mondrian));
57
58 // Make a menubar
59 wxMenu *file_menu = new wxMenu;
60
61 file_menu->Append(TYPES_ABOUT, "&About");
62 file_menu->AppendSeparator();
63 file_menu->Append(TYPES_DATE, "&Date test");
64 file_menu->Append(TYPES_TIME, "&Time test");
65 file_menu->Append(TYPES_VARIANT, "&Variant test");
66 file_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
67 file_menu->AppendSeparator();
68 file_menu->Append(TYPES_QUIT, "E&xit");
69 wxMenuBar *menu_bar = new wxMenuBar;
70 menu_bar->Append(file_menu, "&File");
71 frame->SetMenuBar(menu_bar);
72
73 m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
74
75 // Show the frame
76 frame->Show(TRUE);
77
78 SetTopWindow(frame);
79
80 return TRUE;
81 }
82
83 void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event))
84 {
85 wxTextCtrl& textCtrl = * GetTextCtrl();
86
87 textCtrl.Clear();
88 textCtrl << "\nTest byte order macros:\n\n";
89
90 if (wxBYTE_ORDER == wxLITTLE_ENDIAN)
91 textCtrl << "This is a little endian system.\n\n";
92 else
93 textCtrl << "This is a big endian system.\n\n";
94
95 wxString text;
96
97 wxInt32 var = 0xF1F2F3F4;
98 text = "";
99 text.Printf( "Value of wxInt32 is now: %#x.\n\n", var );
100 textCtrl.WriteText( text );
101
102 text = "";
103 text.Printf( "Value of swapped wxInt32 is: %#x.\n\n", wxINT32_SWAP_ALWAYS( var ) );
104 textCtrl.WriteText( text );
105
106 text = "";
107 text.Printf( "Value of wxInt32 swapped on little endian is: %#x.\n\n", wxINT32_SWAP_ON_LE( var ) );
108 textCtrl.WriteText( text );
109
110 text = "";
111 text.Printf( "Value of wxInt32 swapped on big endian is: %#x.\n\n", wxINT32_SWAP_ON_BE( var ) );
112 textCtrl.WriteText( text );
113 }
114
115 void MyApp::DoTimeDemo(wxCommandEvent& WXUNUSED(event))
116 {
117 wxTextCtrl& textCtrl = * GetTextCtrl();
118
119 textCtrl.Clear();
120 textCtrl << "\nTest class wxTime:\n";
121 wxTime now;
122 textCtrl << "It is now " << (wxString) now << "\n";
123 }
124
125 void MyApp::DoDateDemo(wxCommandEvent& WXUNUSED(event))
126 {
127 wxTextCtrl& textCtrl = * GetTextCtrl();
128
129 textCtrl.Clear();
130 textCtrl << "\nTest class wxDate" << "\n";
131
132 // Various versions of the constructors
133 // and various output
134
135 wxDate x(10,20,1962);
136
137 textCtrl << x.FormatDate(wxFULL) << " (full)\n";
138
139 // constuctor with a string, just printing the day of the week
140 wxDate y("8/8/1988");
141
142 textCtrl << y.FormatDate(wxDAY) << " (just day)\n";
143
144 // constructor with a julian
145 wxDate z( 2450000L );
146 textCtrl << z.FormatDate(wxFULL) << " (full)\n";
147
148 // using date addition and subtraction
149 wxDate a = x + 10;
150 textCtrl << a.FormatDate(wxFULL) << " (full)\n";
151 a = a - 25;
152 textCtrl << a.FormatDate(wxEUROPEAN) << " (European)\n";
153
154
155 // Using subtraction of two date objects
156 wxDate a1 = wxString("7/13/1991");
157 wxDate a2 = a1 + 14;
158 textCtrl << (a1-a2) << "\n";
159 textCtrl << (a2+=10) << "\n";
160
161 a1++;
162 textCtrl << "Tomorrow= " << a1.FormatDate(wxFULL) << "\n";
163
164 wxDate tmpDate1("08/01/1991");
165 wxDate tmpDate2("07/14/1991");
166 textCtrl << "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1 < tmpDate1) ? "TRUE" : "FALSE") << "\n";
167 textCtrl << "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1 > tmpDate1) ? "TRUE" : "FALSE") << "\n";
168 textCtrl << "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1==tmpDate2) ? "TRUE" : "FALSE") << "\n";
169
170 wxDate a3 = a1;
171 textCtrl << "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1==a3) ? "TRUE" : "FALSE") << "\n";
172 wxDate a4 = a1;
173 textCtrl << "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1==++a4) ? "TRUE" : "FALSE") << "\n";
174
175 wxDate a5 = wxString("today");
176 textCtrl << "Today is: " << a5 << "\n";
177 a4 = "TODAY";
178 textCtrl << "Today (a4) is: " << a4 << "\n";
179
180 textCtrl << "Today + 4 is: " << (a4+=4) << "\n";
181 a4 = "TODAY";
182 textCtrl << "Today - 4 is: " << (a4-=4) << "\n";
183
184 textCtrl << "=========== Leap Year Test ===========\n";
185 a1 = "1/15/1992";
186 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
187 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
188
189 a1 = "2/16/1993";
190 textCtrl << a1.FormatDate(wxFULL) << " " << ((a1.IsLeapYear()) ? "Leap" : "non-Leap");
191 textCtrl << " " << "day of year: " << a1.GetDayOfYear() << "\n";
192
193 textCtrl << "================== string assignment test ====================\n";
194 wxString date_string=a1;
195 textCtrl << "a1 as a string (s/b 2/16/1993) ==> " << date_string << "\n";
196
197 textCtrl << "================== SetFormat test ============================\n";
198 a1.SetFormat(wxFULL);
199 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
200 a1.SetFormat(wxEUROPEAN);
201 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
202
203 textCtrl << "================== SetOption test ============================\n";
204 textCtrl << "Date abbreviation ON\n";
205
206 a1.SetOption(wxDATE_ABBR);
207 a1.SetFormat(wxMONTH);
208 textCtrl << "a1 (s/b MONTH format) ==> " << a1 << "\n";
209 a1.SetFormat(wxDAY);
210 textCtrl << "a1 (s/b DAY format) ==> " << a1 << "\n";
211 a1.SetFormat(wxFULL);
212 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
213 a1.SetFormat(wxEUROPEAN);
214 textCtrl << "a1 (s/b EUROPEAN format) ==> " << a1 << "\n";
215 textCtrl << "Century suppression ON\n";
216 a1.SetOption(wxNO_CENTURY);
217 a1.SetFormat(wxMDY);
218 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
219 textCtrl << "Century suppression OFF\n";
220 a1.SetOption(wxNO_CENTURY,FALSE);
221 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
222 textCtrl << "Century suppression ON\n";
223 a1.SetOption(wxNO_CENTURY);
224 textCtrl << "a1 (s/b MDY format) ==> " << a1 << "\n";
225 a1.SetFormat(wxFULL);
226 textCtrl << "a1 (s/b FULL format) ==> " << a1 << "\n";
227
228 textCtrl << "\n=============== Version 4.0 Enhancement Test =================\n";
229
230 wxDate v4("11/26/1966");
231 textCtrl << "\n---------- Set Stuff -----------\n";
232 textCtrl << "First, 'Set' to today..." << "\n";
233 textCtrl << "Before 'Set' => " << v4 << "\n";
234 textCtrl << "After 'Set' => " << v4.Set() << "\n\n";
235
236 textCtrl << "Set to 11/26/66 => " << v4.Set(11,26,1966) << "\n";
237 textCtrl << "Current Julian => " << v4.GetJulianDate() << "\n";
238 textCtrl << "Set to Julian 2450000L => " << v4.Set(2450000L) << "\n";
239 textCtrl << "See! => " << v4.GetJulianDate() << "\n";
240
241 textCtrl << "---------- Add Stuff -----------\n";
242 textCtrl << "Start => " << v4 << "\n";
243 textCtrl << "Add 4 Weeks => " << v4.AddWeeks(4) << "\n";
244 textCtrl << "Sub 1 Month => " << v4.AddMonths(-1) << "\n";
245 textCtrl << "Add 2 Years => " << v4.AddYears(2) << "\n";
246
247 textCtrl << "---------- Misc Stuff -----------\n";
248 textCtrl << "The date aboves' day of the month is => " << v4.GetDay() << "\n";
249 textCtrl << "There are " << v4.GetDaysInMonth() << " days in this month.\n";
250 textCtrl << "The first day of this month lands on " << v4.GetFirstDayOfMonth() << "\n";
251 textCtrl << "This day happens to be " << v4.GetDayOfWeekName() << "\n";
252 textCtrl << "the " << v4.GetDayOfWeek() << " day of the week," << "\n";
253 textCtrl << "on the " << v4.GetWeekOfYear() << " week of the year," << "\n";
254 textCtrl << "on the " << v4.GetWeekOfMonth() << " week of the month, " << "\n";
255 textCtrl << "(which is " << v4.GetMonthName() << ")\n";
256 textCtrl << "the "<< v4.GetMonth() << "th month in the year.\n";
257 textCtrl << "The year alone is " << v4.GetYear() << "\n";
258
259 textCtrl << "---------- First and Last Stuff -----------\n";
260 v4.Set();
261 textCtrl << "The first date of this month is " << v4.GetMonthStart() << "\n";
262 textCtrl << "The last date of this month is " << v4.GetMonthEnd() << "\n";
263 textCtrl << "The first date of this year is " << v4.GetYearStart() << "\n";
264 textCtrl << "The last date of this year is " << v4.GetYearEnd() << "\n";
265 }
266
267 void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) )
268 {
269 wxTextCtrl& textCtrl = * GetTextCtrl();
270
271 wxVariant var1 = "String value";
272 textCtrl << "var1 = " << var1.MakeString() << "\n";
273
274 // Conversion
275 wxString str = var1.MakeString();
276
277 var1 = 123.456;
278 textCtrl << "var1 = " << var1.GetReal() << "\n";
279
280 // Implicit conversion
281 double v = var1;
282
283 var1 = 9876L;
284 textCtrl << "var1 = " << var1.GetLong() << "\n";
285
286 // Implicit conversion
287 long l = var1;
288
289 wxStringList stringList;
290 stringList.Add("one"); stringList.Add("two"); stringList.Add("three");
291 var1 = stringList;
292 textCtrl << "var1 = " << var1.MakeString() << "\n";
293
294 var1.ClearList();
295 var1.Append(wxVariant(1.2345));
296 var1.Append(wxVariant("hello"));
297 var1.Append(wxVariant(54321L));
298
299 textCtrl << "var1 = " << var1.MakeString() << "\n";
300
301 size_t n = var1.GetCount();
302 size_t i;
303 for (i = (size_t) 0; i < n; i++)
304 {
305 textCtrl << "var1[" << (int) i << "] (type " << var1[i].GetType() << ") = " << var1[i].MakeString() << "\n";
306 }
307 }
308
309 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
310 EVT_MENU(TYPES_QUIT, MyFrame::OnQuit)
311 EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout)
312 END_EVENT_TABLE()
313
314 // My frame constructor
315 MyFrame::MyFrame(wxFrame *parent, const wxString& title,
316 const wxPoint& pos, const wxSize& size):
317 wxFrame(parent, -1, title, pos, size)
318 {}
319
320 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
321 {
322 Close(TRUE);
323 }
324
325 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
326 {
327 wxMessageDialog dialog(this, "Tests various wxWindows types",
328 "About Types", wxYES_NO|wxCANCEL);
329
330 dialog.ShowModal();
331 }
332
333