]>
git.saurik.com Git - wxWidgets.git/blob - samples/typetest/typetest.cpp
3cc3dc4d83b30485ee22e68a7bbbeb65a6bbd2ef
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 #include "wx/ioswrap.h"
45 #include "wx/wfstream.h"
46 #include "wx/datstrm.h"
49 // Create a new application object
52 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
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
)
60 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
62 EVT_MENU(TYPES_STREAM
, MyApp::DoStreamDemo
)
65 bool MyApp::OnInit(void)
67 // Create the main frame window
68 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxWindows Types Demo",
69 wxPoint(50, 50), wxSize(450, 340));
72 frame
->SetIcon(wxICON(mondrian
));
75 wxMenu
*file_menu
= new wxMenu
;
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");
84 file_menu
->Append(TYPES_UNICODE
, "&Unicode test");
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
);
93 m_textCtrl
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxDefaultSize
, wxTE_MULTILINE
);
103 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
105 wxTextCtrl
& textCtrl
= * GetTextCtrl();
108 textCtrl
<< "\nTest fstream vs. wxFileStream:\n\n";
110 textCtrl
.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
112 ofstream
std_file_output( "test_std.dat" );
113 wxFileOutputStream
file_output( "test_wx.dat" );
116 signed int si
= 0xFFFFFFFF;
117 tmp
.Printf( "Signed int: %d\n", si
);
118 textCtrl
.WriteText( tmp
);
119 file_output
<< si
<< "\n";
120 std_file_output
<< si
<< "\n";
122 unsigned int ui
= 0xFFFFFFFF;
123 tmp
.Printf( "Unsigned int: %u\n", ui
);
124 textCtrl
.WriteText( tmp
);
125 file_output
<< ui
<< "\n";
126 std_file_output
<< ui
<< "\n";
128 double d
= 2.01234567890123456789;
129 tmp
.Printf( "Double: %f\n", d
);
130 textCtrl
.WriteText( tmp
);
131 file_output
<< d
<< "\n";
132 std_file_output
<< d
<< "\n";
135 tmp
.Printf( "Float: %f\n", f
);
136 textCtrl
.WriteText( tmp
);
137 file_output
<< f
<< "\n";
138 std_file_output
<< f
<< "\n";
140 wxString
str( "Hello!" );
141 tmp
.Printf( "String: %s\n", str
.c_str() );
142 textCtrl
.WriteText( tmp
);
143 file_output
<< str
<< "\n";
144 std_file_output
<< str
.c_str() << "\n";
146 textCtrl
.WriteText( "\nReading from ifstream:\n" );
148 ifstream
std_file_input( "test_std.dat" );
150 std_file_input
>> si
;
151 tmp
.Printf( "Signed int: %d\n", si
);
152 textCtrl
.WriteText( tmp
);
154 std_file_input
>> ui
;
155 tmp
.Printf( "Unsigned int: %u\n", ui
);
156 textCtrl
.WriteText( tmp
);
159 tmp
.Printf( "Double: %f\n", d
);
160 textCtrl
.WriteText( tmp
);
163 tmp
.Printf( "Float: %f\n", f
);
164 textCtrl
.WriteText( tmp
);
166 std_file_input
>> str
;
167 tmp
.Printf( "String: %s\n", str
.c_str() );
168 textCtrl
.WriteText( tmp
);
170 textCtrl
.WriteText( "\nReading from wxFileInputStream:\n" );
172 file_output
.OutputStreamBuffer()->FlushBuffer();
174 wxFileInputStream
file_input( "test_wx.dat" );
177 tmp
.Printf( "Signed int: %d\n", si
);
178 textCtrl
.WriteText( tmp
);
181 tmp
.Printf( "Unsigned int: %u\n", ui
);
182 textCtrl
.WriteText( tmp
);
185 tmp
.Printf( "Double: %f\n", d
);
186 textCtrl
.WriteText( tmp
);
189 tmp
.Printf( "Float: %f\n", f
);
190 textCtrl
.WriteText( tmp
);
193 tmp
.Printf( "String: %s\n", str
.c_str() );
194 textCtrl
.WriteText( tmp
);
197 textCtrl
<< "\nTest for wxDataStream:\n\n";
199 textCtrl
.WriteText( "Writing to wxDataOutputStream:\n" );
201 file_output
.SeekO( 0 );
202 wxDataOutputStream
data_output( file_output
);
204 wxInt16 i16
= 0xFFFF;
205 tmp
.Printf( "Signed int16: %d\n", (int)i16
);
206 textCtrl
.WriteText( tmp
);
207 data_output
.Write16( i16
);
209 wxUint16 ui16
= 0xFFFF;
210 tmp
.Printf( "Unsigned int16: %u\n", (unsigned int) ui16
);
211 textCtrl
.WriteText( tmp
);
212 data_output
.Write16( ui16
);
214 d
= 2.01234567890123456789;
215 tmp
.Printf( "Double: %f\n", d
);
216 textCtrl
.WriteText( tmp
);
217 data_output
.WriteDouble( d
);
220 tmp
.Printf( "String: %s\n", str
.c_str() );
221 textCtrl
.WriteText( tmp
);
222 data_output
.WriteString( str
);
224 file_output
.OutputStreamBuffer()->FlushBuffer();
226 textCtrl
.WriteText( "\nReading from wxDataInputStream:\n" );
228 file_input
.SeekI( 0 );
229 wxDataInputStream
data_input( file_input
);
231 i16
= data_input
.Read16();
232 tmp
.Printf( "Signed int16: %d\n", (int)i16
);
233 textCtrl
.WriteText( tmp
);
235 ui16
= data_input
.Read16();
236 tmp
.Printf( "Unsigned int16: %u\n", (unsigned int) ui16
);
237 textCtrl
.WriteText( tmp
);
239 d
= data_input
.ReadDouble();
240 tmp
.Printf( "Double: %f\n", d
);
241 textCtrl
.WriteText( tmp
);
243 str
= data_input
.ReadString();
244 tmp
.Printf( "String: %s\n", str
.c_str() );
245 textCtrl
.WriteText( tmp
);
249 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
251 wxTextCtrl
& textCtrl
= * GetTextCtrl();
254 textCtrl
<< "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
257 str
= _T("Robert Röbling\n");
259 printf( "\n\nConversion with wxConvLocal:\n" );
260 wxConvCurrent
= &wxConvLocal
;
261 printf( (const char*) str
.mbc_str() );
263 printf( "\n\nConversion with wxConvGdk:\n" );
264 wxConvCurrent
= &wxConvGdk
;
265 printf( (const char*) str
.mbc_str() );
267 printf( "\n\nConversion with wxConvLibc:\n" );
268 wxConvCurrent
= &wxConvLibc
;
269 printf( (const char*) str
.mbc_str() );
274 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
276 wxTextCtrl
& textCtrl
= * GetTextCtrl();
279 textCtrl
<< "\nTest byte order macros:\n\n";
281 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
282 textCtrl
<< "This is a little endian system.\n\n";
284 textCtrl
<< "This is a big endian system.\n\n";
288 wxInt32 var
= 0xF1F2F3F4;
290 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
291 textCtrl
.WriteText( text
);
294 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
295 textCtrl
.WriteText( text
);
298 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
299 textCtrl
.WriteText( text
);
302 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
303 textCtrl
.WriteText( text
);
306 void MyApp::DoTimeDemo(wxCommandEvent
& WXUNUSED(event
))
308 wxTextCtrl
& textCtrl
= * GetTextCtrl();
311 textCtrl
<< "\nTest class wxTime:\n";
313 textCtrl
<< "It is now " << (wxString
) now
<< "\n";
316 void MyApp::DoDateDemo(wxCommandEvent
& WXUNUSED(event
))
318 wxTextCtrl
& textCtrl
= * GetTextCtrl();
321 textCtrl
<< "\nTest class wxDate" << "\n";
323 // Various versions of the constructors
324 // and various output
326 wxDate
x(10,20,1962);
328 textCtrl
<< x
.FormatDate(wxFULL
) << " (full)\n";
330 // constuctor with a string, just printing the day of the week
331 wxDate
y("8/8/1988");
333 textCtrl
<< y
.FormatDate(wxDAY
) << " (just day)\n";
335 // constructor with a julian
336 wxDate
z( 2450000L );
337 textCtrl
<< z
.FormatDate(wxFULL
) << " (full)\n";
339 // using date addition and subtraction
341 textCtrl
<< a
.FormatDate(wxFULL
) << " (full)\n";
343 textCtrl
<< a
.FormatDate(wxEUROPEAN
) << " (European)\n";
346 // Using subtraction of two date objects
347 wxDate a1
= wxString("7/13/1991");
349 textCtrl
<< (a1
-a2
) << "\n";
350 textCtrl
<< (a2
+=10) << "\n";
353 textCtrl
<< "Tomorrow= " << a1
.FormatDate(wxFULL
) << "\n";
355 wxDate
tmpDate1("08/01/1991");
356 wxDate
tmpDate2("07/14/1991");
357 textCtrl
<< "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1
< tmpDate1
) ? "TRUE" : "FALSE") << "\n";
358 textCtrl
<< "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1
> tmpDate1
) ? "TRUE" : "FALSE") << "\n";
359 textCtrl
<< "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1
==tmpDate2
) ? "TRUE" : "FALSE") << "\n";
362 textCtrl
<< "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1
==a3
) ? "TRUE" : "FALSE") << "\n";
364 textCtrl
<< "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1
==++a4
) ? "TRUE" : "FALSE") << "\n";
366 wxDate a5
= wxString("today");
367 textCtrl
<< "Today is: " << a5
<< "\n";
369 textCtrl
<< "Today (a4) is: " << a4
<< "\n";
371 textCtrl
<< "Today + 4 is: " << (a4
+=4) << "\n";
373 textCtrl
<< "Today - 4 is: " << (a4
-=4) << "\n";
375 textCtrl
<< "=========== Leap Year Test ===========\n";
377 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
378 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
381 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
382 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
384 textCtrl
<< "================== string assignment test ====================\n";
385 wxString date_string
=a1
;
386 textCtrl
<< "a1 as a string (s/b 2/16/1993) ==> " << date_string
<< "\n";
388 textCtrl
<< "================== SetFormat test ============================\n";
389 a1
.SetFormat(wxFULL
);
390 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
391 a1
.SetFormat(wxEUROPEAN
);
392 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
394 textCtrl
<< "================== SetOption test ============================\n";
395 textCtrl
<< "Date abbreviation ON\n";
397 a1
.SetOption(wxDATE_ABBR
);
398 a1
.SetFormat(wxMONTH
);
399 textCtrl
<< "a1 (s/b MONTH format) ==> " << a1
<< "\n";
401 textCtrl
<< "a1 (s/b DAY format) ==> " << a1
<< "\n";
402 a1
.SetFormat(wxFULL
);
403 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
404 a1
.SetFormat(wxEUROPEAN
);
405 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
406 textCtrl
<< "Century suppression ON\n";
407 a1
.SetOption(wxNO_CENTURY
);
409 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
410 textCtrl
<< "Century suppression OFF\n";
411 a1
.SetOption(wxNO_CENTURY
,FALSE
);
412 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
413 textCtrl
<< "Century suppression ON\n";
414 a1
.SetOption(wxNO_CENTURY
);
415 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
416 a1
.SetFormat(wxFULL
);
417 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
419 textCtrl
<< "\n=============== Version 4.0 Enhancement Test =================\n";
421 wxDate
v4("11/26/1966");
422 textCtrl
<< "\n---------- Set Stuff -----------\n";
423 textCtrl
<< "First, 'Set' to today..." << "\n";
424 textCtrl
<< "Before 'Set' => " << v4
<< "\n";
425 textCtrl
<< "After 'Set' => " << v4
.Set() << "\n\n";
427 textCtrl
<< "Set to 11/26/66 => " << v4
.Set(11,26,1966) << "\n";
428 textCtrl
<< "Current Julian => " << v4
.GetJulianDate() << "\n";
429 textCtrl
<< "Set to Julian 2450000L => " << v4
.Set(2450000L) << "\n";
430 textCtrl
<< "See! => " << v4
.GetJulianDate() << "\n";
432 textCtrl
<< "---------- Add Stuff -----------\n";
433 textCtrl
<< "Start => " << v4
<< "\n";
434 textCtrl
<< "Add 4 Weeks => " << v4
.AddWeeks(4) << "\n";
435 textCtrl
<< "Sub 1 Month => " << v4
.AddMonths(-1) << "\n";
436 textCtrl
<< "Add 2 Years => " << v4
.AddYears(2) << "\n";
438 textCtrl
<< "---------- Misc Stuff -----------\n";
439 textCtrl
<< "The date aboves' day of the month is => " << v4
.GetDay() << "\n";
440 textCtrl
<< "There are " << v4
.GetDaysInMonth() << " days in this month.\n";
441 textCtrl
<< "The first day of this month lands on " << v4
.GetFirstDayOfMonth() << "\n";
442 textCtrl
<< "This day happens to be " << v4
.GetDayOfWeekName() << "\n";
443 textCtrl
<< "the " << v4
.GetDayOfWeek() << " day of the week," << "\n";
444 textCtrl
<< "on the " << v4
.GetWeekOfYear() << " week of the year," << "\n";
445 textCtrl
<< "on the " << v4
.GetWeekOfMonth() << " week of the month, " << "\n";
446 textCtrl
<< "(which is " << v4
.GetMonthName() << ")\n";
447 textCtrl
<< "the "<< v4
.GetMonth() << "th month in the year.\n";
448 textCtrl
<< "The year alone is " << v4
.GetYear() << "\n";
450 textCtrl
<< "---------- First and Last Stuff -----------\n";
452 textCtrl
<< "The first date of this month is " << v4
.GetMonthStart() << "\n";
453 textCtrl
<< "The last date of this month is " << v4
.GetMonthEnd() << "\n";
454 textCtrl
<< "The first date of this year is " << v4
.GetYearStart() << "\n";
455 textCtrl
<< "The last date of this year is " << v4
.GetYearEnd() << "\n";
458 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
460 wxTextCtrl
& textCtrl
= * GetTextCtrl();
462 wxVariant var1
= "String value";
463 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
466 wxString str
= var1
.MakeString();
469 textCtrl
<< "var1 = " << var1
.GetReal() << "\n";
471 // Implicit conversion
475 textCtrl
<< "var1 = " << var1
.GetLong() << "\n";
477 // Implicit conversion
480 wxStringList stringList
;
481 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
483 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
486 var1
.Append(wxVariant(1.2345));
487 var1
.Append(wxVariant("hello"));
488 var1
.Append(wxVariant(54321L));
490 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
492 size_t n
= var1
.GetCount();
494 for (i
= (size_t) 0; i
< n
; i
++)
496 textCtrl
<< "var1[" << (int) i
<< "] (type " << var1
[i
].GetType() << ") = " << var1
[i
].MakeString() << "\n";
500 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
501 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
502 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
505 // My frame constructor
506 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
507 const wxPoint
& pos
, const wxSize
& size
):
508 wxFrame(parent
, -1, title
, pos
, size
)
511 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
516 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
518 wxMessageDialog
dialog(this, "Tests various wxWindows types",
519 "About Types", wxYES_NO
|wxCANCEL
);