]>
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"
30 #include "wx/mimetype.h"
34 #if defined(__WXGTK__) || defined(__WXMOTIF__)
35 #include "mondrian.xpm"
38 #include "wx/ioswrap.h"
46 #include "wx/wfstream.h"
47 #include "wx/datstrm.h"
48 #include "wx/txtstrm.h"
50 // Create a new application object
53 IMPLEMENT_DYNAMIC_CLASS (MyApp
, wxApp
)
55 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
56 EVT_MENU(TYPES_DATE
, MyApp::DoDateDemo
)
57 EVT_MENU(TYPES_TIME
, MyApp::DoTimeDemo
)
58 EVT_MENU(TYPES_VARIANT
, MyApp::DoVariantDemo
)
59 EVT_MENU(TYPES_BYTEORDER
, MyApp::DoByteOrderDemo
)
61 EVT_MENU(TYPES_UNICODE
, MyApp::DoUnicodeDemo
)
63 EVT_MENU(TYPES_STREAM
, MyApp::DoStreamDemo
)
64 EVT_MENU(TYPES_MIME
, MyApp::DoMIMEDemo
)
69 // Create the main frame window
70 MyFrame
*frame
= new MyFrame((wxFrame
*) NULL
, "wxWindows Types Demo",
71 wxPoint(50, 50), wxSize(450, 340));
74 frame
->SetIcon(wxICON(mondrian
));
77 wxMenu
*file_menu
= new wxMenu
;
79 file_menu
->Append(TYPES_ABOUT
, "&About");
80 file_menu
->AppendSeparator();
81 file_menu
->Append(TYPES_QUIT
, "E&xit\tAlt-X");
83 wxMenu
*test_menu
= new wxMenu
;
84 test_menu
->Append(TYPES_DATE
, "&Date test");
85 test_menu
->Append(TYPES_TIME
, "&Time test");
86 test_menu
->Append(TYPES_VARIANT
, "&Variant test");
87 test_menu
->Append(TYPES_BYTEORDER
, "&Byteorder test");
89 test_menu
->Append(TYPES_UNICODE
, "&Unicode test");
91 test_menu
->Append(TYPES_STREAM
, "&Stream test");
92 test_menu
->AppendSeparator();
93 test_menu
->Append(TYPES_MIME
, "&MIME database test");
95 wxMenuBar
*menu_bar
= new wxMenuBar
;
96 menu_bar
->Append(file_menu
, "&File");
97 menu_bar
->Append(test_menu
, "&Tests");
98 frame
->SetMenuBar(menu_bar
);
100 m_textCtrl
= new wxTextCtrl(frame
, -1, "", wxPoint(0, 0), wxDefaultSize
, wxTE_MULTILINE
);
110 void MyApp::DoStreamDemo(wxCommandEvent
& WXUNUSED(event
))
112 wxTextCtrl
& textCtrl
= * GetTextCtrl();
115 textCtrl
<< _T("\nTest fstream vs. wxFileStream:\n\n");
117 textCtrl
.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
119 ofstream
std_file_output( "test_std.dat" );
120 wxFileOutputStream
file_output( "test_wx.dat" );
121 wxBufferedOutputStream
buf_output( file_output
);
122 wxTextOutputStream
text_output( buf_output
);
125 signed int si
= 0xFFFFFFFF;
126 tmp
.Printf( _T("Signed int: %d\n"), si
);
127 textCtrl
.WriteText( tmp
);
128 text_output
<< si
<< "\n";
129 std_file_output
<< si
<< "\n";
131 unsigned int ui
= 0xFFFFFFFF;
132 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
133 textCtrl
.WriteText( tmp
);
134 text_output
<< ui
<< "\n";
135 std_file_output
<< ui
<< "\n";
137 double d
= 2.01234567890123456789;
138 tmp
.Printf( _T("Double: %f\n"), d
);
139 textCtrl
.WriteText( tmp
);
140 text_output
<< d
<< "\n";
141 std_file_output
<< d
<< "\n";
143 float f
= (float)0.00001;
144 tmp
.Printf( _T("Float: %f\n"), f
);
145 textCtrl
.WriteText( tmp
);
146 text_output
<< f
<< "\n";
147 std_file_output
<< f
<< "\n";
149 wxString
str( _T("Hello!") );
150 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
151 textCtrl
.WriteText( tmp
);
152 text_output
<< str
<< "\n";
153 std_file_output
<< str
.c_str() << "\n";
155 textCtrl
.WriteText( "\nReading from ifstream:\n" );
157 ifstream
std_file_input( "test_std.dat" );
159 std_file_input
>> si
;
160 tmp
.Printf( _T("Signed int: %d\n"), si
);
161 textCtrl
.WriteText( tmp
);
163 std_file_input
>> ui
;
164 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
165 textCtrl
.WriteText( tmp
);
168 tmp
.Printf( _T("Double: %f\n"), d
);
169 textCtrl
.WriteText( tmp
);
172 tmp
.Printf( _T("Float: %f\n"), f
);
173 textCtrl
.WriteText( tmp
);
175 std_file_input
>> str
;
176 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
177 textCtrl
.WriteText( tmp
);
179 textCtrl
.WriteText( "\nReading from wxFileInputStream:\n" );
183 wxFileInputStream
file_input( "test_wx.dat" );
184 wxBufferedInputStream
buf_input( file_input
);
185 wxTextInputStream
text_input( buf_input
);
188 tmp
.Printf( _T("Signed int: %d\n"), si
);
189 textCtrl
.WriteText( tmp
);
192 tmp
.Printf( _T("Unsigned int: %u\n"), ui
);
193 textCtrl
.WriteText( tmp
);
196 tmp
.Printf( _T("Double: %f\n"), d
);
197 textCtrl
.WriteText( tmp
);
200 tmp
.Printf( _T("Float: %f\n"), f
);
201 textCtrl
.WriteText( tmp
);
204 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
205 textCtrl
.WriteText( tmp
);
208 textCtrl
<< "\nTest for wxDataStream:\n\n";
210 textCtrl
.WriteText( "Writing to wxDataOutputStream:\n" );
212 file_output
.SeekO( 0 );
213 wxDataOutputStream
data_output( buf_output
);
215 wxInt16 i16
= (short)0xFFFF;
216 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
217 textCtrl
.WriteText( tmp
);
218 data_output
.Write16( i16
);
220 wxUint16 ui16
= 0xFFFF;
221 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
222 textCtrl
.WriteText( tmp
);
223 data_output
.Write16( ui16
);
225 d
= 2.01234567890123456789;
226 tmp
.Printf( _T("Double: %f\n"), d
);
227 textCtrl
.WriteText( tmp
);
228 data_output
.WriteDouble( d
);
231 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
232 textCtrl
.WriteText( tmp
);
233 data_output
.WriteString( str
);
237 textCtrl
.WriteText( "\nReading from wxDataInputStream:\n" );
239 file_input
.SeekI( 0 );
240 wxDataInputStream
data_input( buf_input
);
242 i16
= data_input
.Read16();
243 tmp
.Printf( _T("Signed int16: %d\n"), (int)i16
);
244 textCtrl
.WriteText( tmp
);
246 ui16
= data_input
.Read16();
247 tmp
.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16
);
248 textCtrl
.WriteText( tmp
);
250 d
= data_input
.ReadDouble();
251 tmp
.Printf( _T("Double: %f\n"), d
);
252 textCtrl
.WriteText( tmp
);
254 str
= data_input
.ReadString();
255 tmp
.Printf( _T("String: %s\n"), str
.c_str() );
256 textCtrl
.WriteText( tmp
);
260 void MyApp::DoUnicodeDemo(wxCommandEvent
& WXUNUSED(event
))
262 wxTextCtrl
& textCtrl
= * GetTextCtrl();
265 textCtrl
<< "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
268 str
= _T("Robert Röbling\n");
270 printf( "\n\nConversion with wxConvLocal:\n" );
271 wxConvCurrent
= &wxConvLocal
;
272 printf( (const char*) str
.mbc_str() );
274 printf( "\n\nConversion with wxConvGdk:\n" );
275 wxConvCurrent
= &wxConvGdk
;
276 printf( (const char*) str
.mbc_str() );
278 printf( "\n\nConversion with wxConvLibc:\n" );
279 wxConvCurrent
= &wxConvLibc
;
280 printf( (const char*) str
.mbc_str() );
285 void MyApp::DoMIMEDemo(wxCommandEvent
& WXUNUSED(event
))
287 static wxString s_defaultExt
= "xyz";
289 wxString ext
= wxGetTextFromUser("Enter a file extension: ",
290 "MIME database test",
296 // init MIME database if not done yet
297 if ( !m_mimeDatabase
)
299 m_mimeDatabase
= new wxMimeTypesManager
;
301 static const wxFileTypeInfo fallbacks
[] =
303 wxFileTypeInfo("application/xyz",
306 "The one and only XYZ format file",
308 wxFileTypeInfo("text/html",
310 "lynx -dump %s | lpr",
311 "HTML document (from fallback)",
312 "htm", "html", NULL
),
314 // must terminate the table with this!
318 m_mimeDatabase
->AddFallbacks(fallbacks
);
321 wxTextCtrl
& textCtrl
= * GetTextCtrl();
323 wxFileType
*filetype
= m_mimeDatabase
->GetFileTypeFromExtension(ext
);
326 textCtrl
<< "Unknown extension '" << ext
<< "'\n";
330 wxString type
, desc
, open
;
331 filetype
->GetMimeType(&type
);
332 filetype
->GetDescription(&desc
);
334 wxString filename
= "filename";
335 filename
<< "." << ext
;
336 wxFileType::MessageParameters
params(filename
, type
);
337 filetype
->GetOpenCommand(&open
, params
);
339 textCtrl
<< "MIME information about extension '" << ext
<< "'\n"
340 << "\tMIME type: " << ( !type
? "unknown"
341 : type
.c_str() ) << '\n'
342 << "\tDescription: " << ( !desc
? "" : desc
.c_str() )
344 << "\tCommand to open: " << ( !open
? "no" : open
.c_str() )
350 //else: cancelled by user
353 void MyApp::DoByteOrderDemo(wxCommandEvent
& WXUNUSED(event
))
355 wxTextCtrl
& textCtrl
= * GetTextCtrl();
358 textCtrl
<< "\nTest byte order macros:\n\n";
360 if (wxBYTE_ORDER
== wxLITTLE_ENDIAN
)
361 textCtrl
<< "This is a little endian system.\n\n";
363 textCtrl
<< "This is a big endian system.\n\n";
367 wxInt32 var
= 0xF1F2F3F4;
369 text
.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var
);
370 textCtrl
.WriteText( text
);
373 text
.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var
) );
374 textCtrl
.WriteText( text
);
377 text
.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var
) );
378 textCtrl
.WriteText( text
);
381 text
.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var
) );
382 textCtrl
.WriteText( text
);
385 void MyApp::DoTimeDemo(wxCommandEvent
& WXUNUSED(event
))
387 wxTextCtrl
& textCtrl
= * GetTextCtrl();
390 textCtrl
<< "\nTest class wxTime:\n";
392 textCtrl
<< "It is now " << (wxString
) now
<< "\n";
395 void MyApp::DoDateDemo(wxCommandEvent
& WXUNUSED(event
))
397 wxTextCtrl
& textCtrl
= * GetTextCtrl();
400 textCtrl
<< "\nTest class wxDate" << "\n";
402 // Various versions of the constructors
403 // and various output
405 wxDate
x(10,20,1962);
407 textCtrl
<< x
.FormatDate(wxFULL
) << " (full)\n";
409 // constuctor with a string, just printing the day of the week
410 wxDate
y("8/8/1988");
412 textCtrl
<< y
.FormatDate(wxDAY
) << " (just day)\n";
414 // constructor with a julian
415 wxDate
z( 2450000L );
416 textCtrl
<< z
.FormatDate(wxFULL
) << " (full)\n";
418 // using date addition and subtraction
420 textCtrl
<< a
.FormatDate(wxFULL
) << " (full)\n";
422 textCtrl
<< a
.FormatDate(wxEUROPEAN
) << " (European)\n";
424 // Using subtraction of two date objects
425 wxDate a1
= wxString("7/13/1991");
427 textCtrl
<< (a1
-a2
) << "\n";
428 textCtrl
<< (a2
+=10) << "\n";
431 textCtrl
<< "Tomorrow= " << a1
.FormatDate(wxFULL
) << "\n";
433 wxDate
tmpDate1("08/01/1991");
434 wxDate
tmpDate2("07/14/1991");
435 textCtrl
<< "a1 (7-14-91) < 8-01-91 ? ==> " << ((a1
< tmpDate1
) ? "TRUE" : "FALSE") << "\n";
436 textCtrl
<< "a1 (7-14-91) > 8-01-91 ? ==> " << ((a1
> tmpDate1
) ? "TRUE" : "FALSE") << "\n";
437 textCtrl
<< "a1 (7-14-91)== 7-14-91 ? ==> " << ((a1
==tmpDate2
) ? "TRUE" : "FALSE") << "\n";
440 textCtrl
<< "a1 (7-14-91)== a3 (7-14-91) ? ==> " << ((a1
==a3
) ? "TRUE" : "FALSE") << "\n";
442 textCtrl
<< "a1 (7-14-91)== a4 (7-15-91) ? ==> " << ((a1
==++a4
) ? "TRUE" : "FALSE") << "\n";
444 wxDate a5
= wxString("today");
445 textCtrl
<< "Today is: " << a5
<< "\n";
447 textCtrl
<< "Today (a4) is: " << a4
<< "\n";
449 textCtrl
<< "Today + 4 is: " << (a4
+=4) << "\n";
451 textCtrl
<< "Today - 4 is: " << (a4
-=4) << "\n";
453 textCtrl
<< "=========== Leap Year Test ===========\n";
455 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
456 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
459 textCtrl
<< a1
.FormatDate(wxFULL
) << " " << ((a1
.IsLeapYear()) ? "Leap" : "non-Leap");
460 textCtrl
<< " " << "day of year: " << a1
.GetDayOfYear() << "\n";
462 textCtrl
<< "================== string assignment test ====================\n";
463 wxString date_string
=a1
;
464 textCtrl
<< "a1 as a string (s/b 2/16/1993) ==> " << date_string
<< "\n";
466 textCtrl
<< "================== SetFormat test ============================\n";
467 a1
.SetFormat(wxFULL
);
468 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
469 a1
.SetFormat(wxEUROPEAN
);
470 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
472 textCtrl
<< "================== SetOption test ============================\n";
473 textCtrl
<< "Date abbreviation ON\n";
475 a1
.SetOption(wxDATE_ABBR
);
476 a1
.SetFormat(wxMONTH
);
477 textCtrl
<< "a1 (s/b MONTH format) ==> " << a1
<< "\n";
479 textCtrl
<< "a1 (s/b DAY format) ==> " << a1
<< "\n";
480 a1
.SetFormat(wxFULL
);
481 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
482 a1
.SetFormat(wxEUROPEAN
);
483 textCtrl
<< "a1 (s/b EUROPEAN format) ==> " << a1
<< "\n";
484 textCtrl
<< "Century suppression ON\n";
485 a1
.SetOption(wxNO_CENTURY
);
487 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
488 textCtrl
<< "Century suppression OFF\n";
489 a1
.SetOption(wxNO_CENTURY
,FALSE
);
490 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
491 textCtrl
<< "Century suppression ON\n";
492 a1
.SetOption(wxNO_CENTURY
);
493 textCtrl
<< "a1 (s/b MDY format) ==> " << a1
<< "\n";
494 a1
.SetFormat(wxFULL
);
495 textCtrl
<< "a1 (s/b FULL format) ==> " << a1
<< "\n";
497 textCtrl
<< "\n=============== Version 4.0 Enhancement Test =================\n";
499 wxDate
v4("11/26/1966");
500 textCtrl
<< "\n---------- Set Stuff -----------\n";
501 textCtrl
<< "First, 'Set' to today..." << "\n";
502 textCtrl
<< "Before 'Set' => " << v4
<< "\n";
503 textCtrl
<< "After 'Set' => " << v4
.Set() << "\n\n";
505 textCtrl
<< "Set to 11/26/66 => " << v4
.Set(11,26,1966) << "\n";
506 textCtrl
<< "Current Julian => " << v4
.GetJulianDate() << "\n";
507 textCtrl
<< "Set to Julian 2450000L => " << v4
.Set(2450000L) << "\n";
508 textCtrl
<< "See! => " << v4
.GetJulianDate() << "\n";
510 textCtrl
<< "---------- Add Stuff -----------\n";
511 textCtrl
<< "Start => " << v4
<< "\n";
512 textCtrl
<< "Add 4 Weeks => " << v4
.AddWeeks(4) << "\n";
513 textCtrl
<< "Sub 1 Month => " << v4
.AddMonths(-1) << "\n";
514 textCtrl
<< "Add 2 Years => " << v4
.AddYears(2) << "\n";
516 textCtrl
<< "---------- Misc Stuff -----------\n";
517 textCtrl
<< "The date aboves' day of the month is => " << v4
.GetDay() << "\n";
518 textCtrl
<< "There are " << v4
.GetDaysInMonth() << " days in this month.\n";
519 textCtrl
<< "The first day of this month lands on " << v4
.GetFirstDayOfMonth() << "\n";
520 textCtrl
<< "This day happens to be " << v4
.GetDayOfWeekName() << "\n";
521 textCtrl
<< "the " << v4
.GetDayOfWeek() << " day of the week," << "\n";
522 textCtrl
<< "on the " << v4
.GetWeekOfYear() << " week of the year," << "\n";
523 textCtrl
<< "on the " << v4
.GetWeekOfMonth() << " week of the month, " << "\n";
524 textCtrl
<< "(which is " << v4
.GetMonthName() << ")\n";
525 textCtrl
<< "the "<< v4
.GetMonth() << "th month in the year.\n";
526 textCtrl
<< "The year alone is " << v4
.GetYear() << "\n";
528 textCtrl
<< "---------- First and Last Stuff -----------\n";
530 textCtrl
<< "The first date of this month is " << v4
.GetMonthStart() << "\n";
531 textCtrl
<< "The last date of this month is " << v4
.GetMonthEnd() << "\n";
532 textCtrl
<< "The first date of this year is " << v4
.GetYearStart() << "\n";
533 textCtrl
<< "The last date of this year is " << v4
.GetYearEnd() << "\n";
536 void MyApp::DoVariantDemo(wxCommandEvent
& WXUNUSED(event
) )
538 wxTextCtrl
& textCtrl
= * GetTextCtrl();
540 wxVariant var1
= "String value";
541 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
544 wxString str
= var1
.MakeString();
547 textCtrl
<< "var1 = " << var1
.GetReal() << "\n";
549 // Implicit conversion
553 textCtrl
<< "var1 = " << var1
.GetLong() << "\n";
555 // Implicit conversion
558 // suppress compile warnings about unused variables
564 wxStringList stringList
;
565 stringList
.Add(_T("one")); stringList
.Add(_T("two")); stringList
.Add(_T("three"));
567 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
570 var1
.Append(wxVariant(1.2345));
571 var1
.Append(wxVariant("hello"));
572 var1
.Append(wxVariant(54321L));
574 textCtrl
<< "var1 = " << var1
.MakeString() << "\n";
576 size_t n
= var1
.GetCount();
578 for (i
= (size_t) 0; i
< n
; i
++)
580 textCtrl
<< "var1[" << (int) i
<< "] (type " << var1
[i
].GetType() << ") = " << var1
[i
].MakeString() << "\n";
584 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
585 EVT_MENU(TYPES_QUIT
, MyFrame::OnQuit
)
586 EVT_MENU(TYPES_ABOUT
, MyFrame::OnAbout
)
589 // My frame constructor
590 MyFrame::MyFrame(wxFrame
*parent
, const wxString
& title
,
591 const wxPoint
& pos
, const wxSize
& size
):
592 wxFrame(parent
, -1, title
, pos
, size
)
595 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
600 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
602 wxMessageDialog
dialog(this, "Tests various wxWindows types",
603 "About Types", wxYES_NO
|wxCANCEL
);