]>
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 #include "wx/ioswrap.h"
45 #include "wx/wfstream.h"
46 #include "wx/datstrm.h"
47 #include "wx/txtstrm.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
<< _T("\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" );
114 wxBufferedOutputStream
buf_output( file_output
);
115 wxTextOutputStream
text_output( buf_output
);
118 signed int si
= 0xFFFFFFFF;
119 tmp
.Printf( _T("Signed int: %d\n"), si
);
120 textCtrl
.WriteText( tmp
);
121 text_output
<< si
<< "\n";
122 std_file_output
<< si
<< "\n";
124 unsigned int ui
= 0xFFFFFFFF;
125 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
126 textCtrl
.WriteText( tmp
);
127 text_output
<< ui
<< "\n";
128 std_file_output
<< ui
<< "\n";
130 double d
= 2.01234567890123456789;
131 tmp
.Printf( _T("Double: %f\n"), d
);
132 textCtrl
.WriteText( tmp
);
133 text_output
<< d
<< "\n";
134 std_file_output
<< d
<< "\n";
137 tmp
.Printf( _T("Float: %f\n"), f
);
138 textCtrl
.WriteText( tmp
);
139 text_output
<< f
<< "\n";
140 std_file_output
<< f
<< "\n";
142 wxString
str( _T("Hello!") );
143 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
144 textCtrl
.WriteText( tmp
);
145 text_output
<< str
<< "\n";
146 std_file_output
<< str
.c_str() << "\n";
148 textCtrl
.WriteText( "\nReading from ifstream:\n" );
150 ifstream
std_file_input( "test_std.dat" );
152 std_file_input
>> si
;
153 tmp
.Printf( _T("Signed int: %d\n"), si
);
154 textCtrl
.WriteText( tmp
);
156 std_file_input
>> ui
;
157 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
158 textCtrl
.WriteText( tmp
);
161 tmp
.Printf( _T("Double: %f\n"), d
);
162 textCtrl
.WriteText( tmp
);
165 tmp
.Printf( _T("Float: %f\n"), f
);
166 textCtrl
.WriteText( tmp
);
168 std_file_input
>> str
;
169 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
170 textCtrl
.WriteText( tmp
);
172 textCtrl
.WriteText( "\nReading from wxFileInputStream:\n" );
176 wxFileInputStream
file_input( "test_wx.dat" );
177 wxBufferedInputStream
buf_input( file_input
);
178 wxTextInputStream
text_input( buf_input
);
181 tmp
.Printf( _T("Signed int: %d\n"), si
);
182 textCtrl
.WriteText( tmp
);
185 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
186 textCtrl
.WriteText( tmp
);
189 tmp
.Printf( _T("Double: %f\n"), d
);
190 textCtrl
.WriteText( tmp
);
193 tmp
.Printf( _T("Float: %f\n"), f
);
194 textCtrl
.WriteText( tmp
);
197 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
198 textCtrl
.WriteText( tmp
);
201 textCtrl
<< "\nTest for wxDataStream:\n\n";
203 textCtrl
.WriteText( "Writing to wxDataOutputStream:\n" );
205 file_output
.SeekO( 0 );
206 wxDataOutputStream
data_output( buf_output
);
208 wxInt16 i16
= 0xFFFF;
209 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
210 textCtrl
.WriteText( tmp
);
211 data_output
.Write16( i16
);
213 wxUint16 ui16
= 0xFFFF;
214 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
215 textCtrl
.WriteText( tmp
);
216 data_output
.Write16( ui16
);
218 d
= 2.01234567890123456789;
219 tmp
.Printf( _T("Double: %f\n"), d
);
220 textCtrl
.WriteText( tmp
);
221 data_output
.WriteDouble( d
);
224 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
225 textCtrl
.WriteText( tmp
);
226 data_output
.WriteString( str
);
230 textCtrl
.WriteText( "\nReading from wxDataInputStream:\n" );
232 file_input
.SeekI( 0 );
233 wxDataInputStream
data_input( buf_input
);
235 i16
= data_input
.Read16();
236 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
237 textCtrl
.WriteText( tmp
);
239 ui16
= data_input
.Read16();
240 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
241 textCtrl
.WriteText( tmp
);
243 d
= data_input
.ReadDouble();
244 tmp
.Printf( _T("Double: %f\n"), d
);
245 textCtrl
.WriteText( tmp
);
247 str
= data_input
.ReadString();
248 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
249 textCtrl
.WriteText( tmp
);
253 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
255 wxTextCtrl
& textCtrl
= * GetTextCtrl();
258 textCtrl
<< "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
261 str
= _T("Robert Röbling\n");
263 printf( "\n\nConversion with wxConvLocal:\n" );
264 wxConvCurrent
= &wxConvLocal
;
265 printf( (const char*) str
.mbc_str() );
267 printf( "\n\nConversion with wxConvGdk:\n" );
268 wxConvCurrent
= &wxConvGdk
;
269 printf( (const char*) str
.mbc_str() );
271 printf( "\n\nConversion with wxConvLibc:\n" );
272 wxConvCurrent
= &wxConvLibc
;
273 printf( (const char*) str
.mbc_str() );
278 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
280 wxTextCtrl
& textCtrl
= * GetTextCtrl();
283 textCtrl
<< "\nTest byte order macros:\n\n";
285 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
286 textCtrl
<< "This is a little endian system.\n\n";
288 textCtrl
<< "This is a big endian system.\n\n";
292 wxInt32 var
= 0xF1F2F3F4;
294 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
295 textCtrl
.WriteText( text
);
298 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
299 textCtrl
.WriteText( text
);
302 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
303 textCtrl
.WriteText( text
);
306 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
307 textCtrl
.WriteText( text
);
310 void MyApp::DoTimeDemo(wxCommandEvent
& WXUNUSED(event
))
312 wxTextCtrl
& textCtrl
= * GetTextCtrl();
315 textCtrl
<< "\nTest class wxTime:\n";
317 textCtrl
<< "It is now " << (wxString
) now
<< "\n";
320 void MyApp::DoDateDemo(wxCommandEvent
& WXUNUSED(event
))
322 wxTextCtrl
& textCtrl
= * GetTextCtrl();
325 textCtrl
<< "\nTest class wxDate" << "\n";
327 // Various versions of the constructors
328 // and various output
330 wxDate
x(10,20,1962);
332 textCtrl
<< x
.FormatDate(wxFULL
) << " (full)\n";
334 // constuctor with a string, just printing the day of the week
335 wxDate
y("8/8/1988");
337 textCtrl
<< y
.FormatDate(wxDAY
) << " (just day)\n";
339 // constructor with a julian
340 wxDate
z( 2450000L );
341 textCtrl
<< z
.FormatDate(wxFULL
) << " (full)\n";
343 // using date addition and subtraction
345 textCtrl
<< a
.FormatDate(wxFULL
) << " (full)\n";
347 textCtrl
<< a
.FormatDate(wxEUROPEAN
) << " (European)\n";
350 // Using subtraction of two date objects
351 wxDate a1
= wxString("7/13/1991");
353 textCtrl
<< (a1
-a2
) << "\n";
354 textCtrl
<< (a2
+=10) << "\n";
357 textCtrl
<< "Tomorrow= " << a1
.FormatDate(wxFULL
) << "\n";
359 wxDate
tmpDate1("08/01/1991");
360 wxDate
tmpDate2("07/14/1991");
361 textCtrl
<< "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1
< tmpDate1
) ? "TRUE" : "FALSE") << "\n";
362 textCtrl
<< "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1
> tmpDate1
) ? "TRUE" : "FALSE") << "\n";
363 textCtrl
<< "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1
==tmpDate2
) ? "TRUE" : "FALSE") << "\n";
366 textCtrl
<< "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1
==a3
) ? "TRUE" : "FALSE") << "\n";
368 textCtrl
<< "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1
==++a4
) ? "TRUE" : "FALSE") << "\n";
370 wxDate a5
= wxString("today");
371 textCtrl
<< "Today is: " << a5
<< "\n";
373 textCtrl
<< "Today (a4) is: " << a4
<< "\n";
375 textCtrl
<< "Today + 4 is: " << (a4
+=4) << "\n";
377 textCtrl
<< "Today - 4 is: " << (a4
-=4) << "\n";
379 textCtrl
<< "=========== Leap Year Test ===========\n";
381 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
382 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
385 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
386 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
388 textCtrl
<< "================== string assignment test ====================\n";
389 wxString date_string
=a1
;
390 textCtrl
<< "a1 as a string (s/b 2/16/1993) ==> " << date_string
<< "\n";
392 textCtrl
<< "================== SetFormat test ============================\n";
393 a1
.SetFormat(wxFULL
);
394 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
395 a1
.SetFormat(wxEUROPEAN
);
396 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
398 textCtrl
<< "================== SetOption test ============================\n";
399 textCtrl
<< "Date abbreviation ON\n";
401 a1
.SetOption(wxDATE_ABBR
);
402 a1
.SetFormat(wxMONTH
);
403 textCtrl
<< "a1 (s/b MONTH format) ==> " << a1
<< "\n";
405 textCtrl
<< "a1 (s/b DAY format) ==> " << a1
<< "\n";
406 a1
.SetFormat(wxFULL
);
407 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
408 a1
.SetFormat(wxEUROPEAN
);
409 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
410 textCtrl
<< "Century suppression ON\n";
411 a1
.SetOption(wxNO_CENTURY
);
413 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
414 textCtrl
<< "Century suppression OFF\n";
415 a1
.SetOption(wxNO_CENTURY
,FALSE
);
416 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
417 textCtrl
<< "Century suppression ON\n";
418 a1
.SetOption(wxNO_CENTURY
);
419 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
420 a1
.SetFormat(wxFULL
);
421 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
423 textCtrl
<< "\n=============== Version 4.0 Enhancement Test =================\n";
425 wxDate
v4("11/26/1966");
426 textCtrl
<< "\n---------- Set Stuff -----------\n";
427 textCtrl
<< "First, 'Set' to today..." << "\n";
428 textCtrl
<< "Before 'Set' => " << v4
<< "\n";
429 textCtrl
<< "After 'Set' => " << v4
.Set() << "\n\n";
431 textCtrl
<< "Set to 11/26/66 => " << v4
.Set(11,26,1966) << "\n";
432 textCtrl
<< "Current Julian => " << v4
.GetJulianDate() << "\n";
433 textCtrl
<< "Set to Julian 2450000L => " << v4
.Set(2450000L) << "\n";
434 textCtrl
<< "See! => " << v4
.GetJulianDate() << "\n";
436 textCtrl
<< "---------- Add Stuff -----------\n";
437 textCtrl
<< "Start => " << v4
<< "\n";
438 textCtrl
<< "Add 4 Weeks => " << v4
.AddWeeks(4) << "\n";
439 textCtrl
<< "Sub 1 Month => " << v4
.AddMonths(-1) << "\n";
440 textCtrl
<< "Add 2 Years => " << v4
.AddYears(2) << "\n";
442 textCtrl
<< "---------- Misc Stuff -----------\n";
443 textCtrl
<< "The date aboves' day of the month is => " << v4
.GetDay() << "\n";
444 textCtrl
<< "There are " << v4
.GetDaysInMonth() << " days in this month.\n";
445 textCtrl
<< "The first day of this month lands on " << v4
.GetFirstDayOfMonth() << "\n";
446 textCtrl
<< "This day happens to be " << v4
.GetDayOfWeekName() << "\n";
447 textCtrl
<< "the " << v4
.GetDayOfWeek() << " day of the week," << "\n";
448 textCtrl
<< "on the " << v4
.GetWeekOfYear() << " week of the year," << "\n";
449 textCtrl
<< "on the " << v4
.GetWeekOfMonth() << " week of the month, " << "\n";
450 textCtrl
<< "(which is " << v4
.GetMonthName() << ")\n";
451 textCtrl
<< "the "<< v4
.GetMonth() << "th month in the year.\n";
452 textCtrl
<< "The year alone is " << v4
.GetYear() << "\n";
454 textCtrl
<< "---------- First and Last Stuff -----------\n";
456 textCtrl
<< "The first date of this month is " << v4
.GetMonthStart() << "\n";
457 textCtrl
<< "The last date of this month is " << v4
.GetMonthEnd() << "\n";
458 textCtrl
<< "The first date of this year is " << v4
.GetYearStart() << "\n";
459 textCtrl
<< "The last date of this year is " << v4
.GetYearEnd() << "\n";
462 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
464 wxTextCtrl
& textCtrl
= * GetTextCtrl();
466 wxVariant var1
= "String value";
467 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
470 wxString str
= var1
.MakeString();
473 textCtrl
<< "var1 = " << var1
.GetReal() << "\n";
475 // Implicit conversion
479 textCtrl
<< "var1 = " << var1
.GetLong() << "\n";
481 // Implicit conversion
484 wxStringList stringList
;
485 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
487 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
490 var1
.Append(wxVariant(1.2345));
491 var1
.Append(wxVariant("hello"));
492 var1
.Append(wxVariant(54321L));
494 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
496 size_t n
= var1
.GetCount();
498 for (i
= (size_t) 0; i
< n
; i
++)
500 textCtrl
<< "var1[" << (int) i
<< "] (type " << var1
[i
].GetType() << ") = " << var1
[i
].MakeString() << "\n";
504 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
505 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
506 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
509 // My frame constructor
510 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
511 const wxPoint
& pos
, const wxSize
& size
):
512 wxFrame(parent
, -1, title
, pos
, size
)
515 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
520 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
522 wxMessageDialog
dialog(this, "Tests various wxWindows types",
523 "About Types", wxYES_NO
|wxCANCEL
);