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