]>
git.saurik.com Git - wxWidgets.git/blob - samples/typetest/typetest.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Types wxWindows sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "typetest.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
29 #include "wx/variant.h"
33 #if defined(__WXGTK__) || defined(__WXMOTIF__)
34 #include "mondrian.xpm"
37 // Create a new application object
40 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
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
)
48 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
52 bool MyApp::OnInit(void)
54 // Create the main frame window
55 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxWindows Types Demo",
56 wxPoint(50, 50), wxSize(450, 340));
59 frame
->SetIcon(wxICON(mondrian
));
62 wxMenu
*file_menu
= new wxMenu
;
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");
71 file_menu
->Append(TYPES_UNICODE
, "&Unicode test");
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
);
79 m_textCtrl
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxDefaultSize
, wxTE_MULTILINE
);
90 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
92 wxTextCtrl
& textCtrl
= * GetTextCtrl();
95 textCtrl
<< "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
98 str
= _T("Robert Röbling\n");
100 printf( "\n\nConversion with wxConvLocal:\n" );
101 wxConvCurrent
= &wxConvLocal
;
102 printf( (const char*) str
.mbc_str() );
104 printf( "\n\nConversion with wxConvGdk:\n" );
105 wxConvCurrent
= &wxConvGdk
;
106 printf( (const char*) str
.mbc_str() );
108 printf( "\n\nConversion with wxConvLibc:\n" );
109 wxConvCurrent
= &wxConvLibc
;
110 printf( (const char*) str
.mbc_str() );
115 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
117 wxTextCtrl
& textCtrl
= * GetTextCtrl();
120 textCtrl
<< "\nTest byte order macros:\n\n";
122 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
123 textCtrl
<< "This is a little endian system.\n\n";
125 textCtrl
<< "This is a big endian system.\n\n";
129 wxInt32 var
= 0xF1F2F3F4;
131 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
132 textCtrl
.WriteText( text
);
135 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
136 textCtrl
.WriteText( 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
);
143 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
144 textCtrl
.WriteText( text
);
147 void MyApp::DoTimeDemo(wxCommandEvent
& WXUNUSED(event
))
149 wxTextCtrl
& textCtrl
= * GetTextCtrl();
152 textCtrl
<< "\nTest class wxTime:\n";
154 textCtrl
<< "It is now " << (wxString
) now
<< "\n";
157 void MyApp::DoDateDemo(wxCommandEvent
& WXUNUSED(event
))
159 wxTextCtrl
& textCtrl
= * GetTextCtrl();
162 textCtrl
<< "\nTest class wxDate" << "\n";
164 // Various versions of the constructors
165 // and various output
167 wxDate
x(10,20,1962);
169 textCtrl
<< x
.FormatDate(wxFULL
) << " (full)\n";
171 // constuctor with a string, just printing the day of the week
172 wxDate
y("8/8/1988");
174 textCtrl
<< y
.FormatDate(wxDAY
) << " (just day)\n";
176 // constructor with a julian
177 wxDate
z( 2450000L );
178 textCtrl
<< z
.FormatDate(wxFULL
) << " (full)\n";
180 // using date addition and subtraction
182 textCtrl
<< a
.FormatDate(wxFULL
) << " (full)\n";
184 textCtrl
<< a
.FormatDate(wxEUROPEAN
) << " (European)\n";
187 // Using subtraction of two date objects
188 wxDate a1
= wxString("7/13/1991");
190 textCtrl
<< (a1
-a2
) << "\n";
191 textCtrl
<< (a2
+=10) << "\n";
194 textCtrl
<< "Tomorrow= " << a1
.FormatDate(wxFULL
) << "\n";
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";
203 textCtrl
<< "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1
==a3
) ? "TRUE" : "FALSE") << "\n";
205 textCtrl
<< "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1
==++a4
) ? "TRUE" : "FALSE") << "\n";
207 wxDate a5
= wxString("today");
208 textCtrl
<< "Today is: " << a5
<< "\n";
210 textCtrl
<< "Today (a4) is: " << a4
<< "\n";
212 textCtrl
<< "Today + 4 is: " << (a4
+=4) << "\n";
214 textCtrl
<< "Today - 4 is: " << (a4
-=4) << "\n";
216 textCtrl
<< "=========== Leap Year Test ===========\n";
218 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
219 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
222 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
223 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
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";
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";
235 textCtrl
<< "================== SetOption test ============================\n";
236 textCtrl
<< "Date abbreviation ON\n";
238 a1
.SetOption(wxDATE_ABBR
);
239 a1
.SetFormat(wxMONTH
);
240 textCtrl
<< "a1 (s/b MONTH format) ==> " << a1
<< "\n";
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
);
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";
260 textCtrl
<< "\n=============== Version 4.0 Enhancement Test =================\n";
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";
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";
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";
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";
291 textCtrl
<< "---------- First and Last Stuff -----------\n";
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";
299 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
301 wxTextCtrl
& textCtrl
= * GetTextCtrl();
303 wxVariant var1
= "String value";
304 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
307 wxString str
= var1
.MakeString();
310 textCtrl
<< "var1 = " << var1
.GetReal() << "\n";
312 // Implicit conversion
316 textCtrl
<< "var1 = " << var1
.GetLong() << "\n";
318 // Implicit conversion
321 wxStringList stringList
;
322 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
324 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
327 var1
.Append(wxVariant(1.2345));
328 var1
.Append(wxVariant("hello"));
329 var1
.Append(wxVariant(54321L));
331 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
333 size_t n
= var1
.GetCount();
335 for (i
= (size_t) 0; i
< n
; i
++)
337 textCtrl
<< "var1[" << (int) i
<< "] (type " << var1
[i
].GetType() << ") = " << var1
[i
].MakeString() << "\n";
341 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
342 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
343 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
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
)
352 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
357 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
359 wxMessageDialog
dialog(this, "Tests various wxWindows types",
360 "About Types", wxYES_NO
|wxCANCEL
);