]>
Commit | Line | Data |
---|---|---|
cfb88c55 JS |
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 | |
8e124873 | 9 | // Licence: wxWindows license |
cfb88c55 JS |
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" | |
8e124873 | 30 | #include "wx/mimetype.h" |
cfb88c55 JS |
31 | |
32 | #include "typetest.h" | |
33 | ||
b412f9be | 34 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
cfb88c55 JS |
35 | #include "mondrian.xpm" |
36 | #endif | |
37 | ||
38830220 RR |
38 | #include "wx/ioswrap.h" |
39 | ||
40 | #if wxUSE_IOSTREAMH | |
41 | #include <fstream.h> | |
42 | #else | |
43 | #include <fstream> | |
44 | #endif | |
45 | ||
46 | #include "wx/wfstream.h" | |
53daeada | 47 | #include "wx/datstrm.h" |
fae05df5 | 48 | #include "wx/txtstrm.h" |
38830220 | 49 | |
cfb88c55 | 50 | // Create a new application object |
8e124873 | 51 | IMPLEMENT_APP (MyApp) |
cfb88c55 | 52 | |
8e124873 | 53 | IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp) |
cfb88c55 JS |
54 | |
55 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
8e124873 VZ |
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) | |
dcf924a3 | 60 | #if wxUSE_UNICODE |
8e124873 | 61 | EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) |
dcf924a3 | 62 | #endif |
1b055864 | 63 | EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2) |
8e124873 VZ |
64 | EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) |
65 | EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo) | |
cfb88c55 JS |
66 | END_EVENT_TABLE() |
67 | ||
8e124873 | 68 | bool MyApp::OnInit() |
cfb88c55 | 69 | { |
8e124873 VZ |
70 | // Create the main frame window |
71 | MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo", | |
72 | wxPoint(50, 50), wxSize(450, 340)); | |
73 | ||
74 | // Give it an icon | |
75 | frame->SetIcon(wxICON(mondrian)); | |
76 | ||
77 | // Make a menubar | |
78 | wxMenu *file_menu = new wxMenu; | |
79 | ||
80 | file_menu->Append(TYPES_ABOUT, "&About"); | |
81 | file_menu->AppendSeparator(); | |
82 | file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X"); | |
83 | ||
84 | wxMenu *test_menu = new wxMenu; | |
85 | test_menu->Append(TYPES_DATE, "&Date test"); | |
86 | test_menu->Append(TYPES_TIME, "&Time test"); | |
87 | test_menu->Append(TYPES_VARIANT, "&Variant test"); | |
88 | test_menu->Append(TYPES_BYTEORDER, "&Byteorder test"); | |
dcf924a3 | 89 | #if wxUSE_UNICODE |
8e124873 | 90 | test_menu->Append(TYPES_UNICODE, "&Unicode test"); |
dcf924a3 | 91 | #endif |
8e124873 | 92 | test_menu->Append(TYPES_STREAM, "&Stream test"); |
1b055864 | 93 | test_menu->Append(TYPES_STREAM2, "&Stream seek test"); |
8e124873 VZ |
94 | test_menu->AppendSeparator(); |
95 | test_menu->Append(TYPES_MIME, "&MIME database test"); | |
cfb88c55 | 96 | |
8e124873 VZ |
97 | wxMenuBar *menu_bar = new wxMenuBar; |
98 | menu_bar->Append(file_menu, "&File"); | |
99 | menu_bar->Append(test_menu, "&Tests"); | |
100 | frame->SetMenuBar(menu_bar); | |
cfb88c55 | 101 | |
8e124873 | 102 | m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE); |
cfb88c55 | 103 | |
8e124873 VZ |
104 | // Show the frame |
105 | frame->Show(TRUE); | |
106 | ||
107 | SetTopWindow(frame); | |
108 | ||
109 | return TRUE; | |
cfb88c55 JS |
110 | } |
111 | ||
38830220 RR |
112 | void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) |
113 | { | |
114 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 115 | |
38830220 | 116 | textCtrl.Clear(); |
c980c992 | 117 | textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n"); |
38830220 | 118 | |
e57e26dd | 119 | textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" ); |
8e124873 | 120 | |
38830220 RR |
121 | ofstream std_file_output( "test_std.dat" ); |
122 | wxFileOutputStream file_output( "test_wx.dat" ); | |
fae05df5 GL |
123 | wxBufferedOutputStream buf_output( file_output ); |
124 | wxTextOutputStream text_output( buf_output ); | |
38830220 | 125 | |
38830220 RR |
126 | wxString tmp; |
127 | signed int si = 0xFFFFFFFF; | |
c980c992 | 128 | tmp.Printf( _T("Signed int: %d\n"), si ); |
38830220 | 129 | textCtrl.WriteText( tmp ); |
fae05df5 | 130 | text_output << si << "\n"; |
38830220 | 131 | std_file_output << si << "\n"; |
8e124873 | 132 | |
38830220 | 133 | unsigned int ui = 0xFFFFFFFF; |
c980c992 | 134 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
38830220 | 135 | textCtrl.WriteText( tmp ); |
fae05df5 | 136 | text_output << ui << "\n"; |
38830220 | 137 | std_file_output << ui << "\n"; |
8e124873 | 138 | |
38830220 | 139 | double d = 2.01234567890123456789; |
c980c992 | 140 | tmp.Printf( _T("Double: %f\n"), d ); |
38830220 | 141 | textCtrl.WriteText( tmp ); |
fae05df5 | 142 | text_output << d << "\n"; |
38830220 | 143 | std_file_output << d << "\n"; |
8e124873 VZ |
144 | |
145 | float f = (float)0.00001; | |
c980c992 | 146 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 147 | textCtrl.WriteText( tmp ); |
fae05df5 | 148 | text_output << f << "\n"; |
e57e26dd | 149 | std_file_output << f << "\n"; |
8e124873 | 150 | |
c980c992 GL |
151 | wxString str( _T("Hello!") ); |
152 | tmp.Printf( _T("String: %s\n"), str.c_str() ); | |
38830220 | 153 | textCtrl.WriteText( tmp ); |
fae05df5 | 154 | text_output << str << "\n"; |
38830220 | 155 | std_file_output << str.c_str() << "\n"; |
8e124873 | 156 | |
e57e26dd | 157 | textCtrl.WriteText( "\nReading from ifstream:\n" ); |
8e124873 | 158 | |
e57e26dd RR |
159 | ifstream std_file_input( "test_std.dat" ); |
160 | ||
161 | std_file_input >> si; | |
c980c992 | 162 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 163 | textCtrl.WriteText( tmp ); |
8e124873 | 164 | |
e57e26dd | 165 | std_file_input >> ui; |
c980c992 | 166 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 167 | textCtrl.WriteText( tmp ); |
8e124873 | 168 | |
e57e26dd | 169 | std_file_input >> d; |
c980c992 | 170 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 171 | textCtrl.WriteText( tmp ); |
8e124873 | 172 | |
e57e26dd | 173 | std_file_input >> f; |
c980c992 | 174 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 175 | textCtrl.WriteText( tmp ); |
8e124873 | 176 | |
e57e26dd | 177 | std_file_input >> str; |
c980c992 | 178 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 179 | textCtrl.WriteText( tmp ); |
8e124873 | 180 | |
e57e26dd | 181 | textCtrl.WriteText( "\nReading from wxFileInputStream:\n" ); |
b6bff301 | 182 | |
fae05df5 | 183 | buf_output.Sync(); |
8e124873 | 184 | |
e57e26dd | 185 | wxFileInputStream file_input( "test_wx.dat" ); |
fae05df5 GL |
186 | wxBufferedInputStream buf_input( file_input ); |
187 | wxTextInputStream text_input( buf_input ); | |
8e124873 | 188 | |
fae05df5 | 189 | text_input >> si; |
c980c992 | 190 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 191 | textCtrl.WriteText( tmp ); |
8e124873 | 192 | |
fae05df5 | 193 | text_input >> ui; |
c980c992 | 194 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 195 | textCtrl.WriteText( tmp ); |
8e124873 | 196 | |
fae05df5 | 197 | text_input >> d; |
c980c992 | 198 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 199 | textCtrl.WriteText( tmp ); |
8e124873 | 200 | |
fae05df5 | 201 | text_input >> f; |
c980c992 | 202 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 203 | textCtrl.WriteText( tmp ); |
8e124873 | 204 | |
fae05df5 | 205 | text_input >> str; |
c980c992 | 206 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 207 | textCtrl.WriteText( tmp ); |
8e124873 | 208 | |
53daeada RR |
209 | |
210 | textCtrl << "\nTest for wxDataStream:\n\n"; | |
211 | ||
212 | textCtrl.WriteText( "Writing to wxDataOutputStream:\n" ); | |
8e124873 | 213 | |
53daeada | 214 | file_output.SeekO( 0 ); |
fae05df5 | 215 | wxDataOutputStream data_output( buf_output ); |
53daeada | 216 | |
8e124873 | 217 | wxInt16 i16 = (short)0xFFFF; |
c980c992 | 218 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 219 | textCtrl.WriteText( tmp ); |
329e86bf | 220 | data_output.Write16( i16 ); |
8e124873 | 221 | |
329e86bf | 222 | wxUint16 ui16 = 0xFFFF; |
c980c992 | 223 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada | 224 | textCtrl.WriteText( tmp ); |
329e86bf | 225 | data_output.Write16( ui16 ); |
8e124873 | 226 | |
53daeada | 227 | d = 2.01234567890123456789; |
c980c992 | 228 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada RR |
229 | textCtrl.WriteText( tmp ); |
230 | data_output.WriteDouble( d ); | |
8e124873 | 231 | |
53daeada | 232 | str = "Hello!"; |
c980c992 | 233 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada RR |
234 | textCtrl.WriteText( tmp ); |
235 | data_output.WriteString( str ); | |
8e124873 | 236 | |
fae05df5 | 237 | buf_output.Sync(); |
8e124873 | 238 | |
53daeada | 239 | textCtrl.WriteText( "\nReading from wxDataInputStream:\n" ); |
8e124873 | 240 | |
53daeada | 241 | file_input.SeekI( 0 ); |
fae05df5 | 242 | wxDataInputStream data_input( buf_input ); |
53daeada | 243 | |
329e86bf | 244 | i16 = data_input.Read16(); |
c980c992 | 245 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 246 | textCtrl.WriteText( tmp ); |
8e124873 | 247 | |
329e86bf | 248 | ui16 = data_input.Read16(); |
c980c992 | 249 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada RR |
250 | textCtrl.WriteText( tmp ); |
251 | ||
252 | d = data_input.ReadDouble(); | |
c980c992 | 253 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada | 254 | textCtrl.WriteText( tmp ); |
8e124873 | 255 | |
53daeada | 256 | str = data_input.ReadString(); |
c980c992 | 257 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada | 258 | textCtrl.WriteText( tmp ); |
38830220 RR |
259 | } |
260 | ||
1b055864 RR |
261 | void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) |
262 | { | |
263 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
264 | ||
265 | textCtrl.Clear(); | |
266 | textCtrl << _T("\nTest wxBufferedStream:\n\n"); | |
267 | ||
268 | char ch,ch2; | |
269 | ||
270 | textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" ); | |
271 | ||
272 | wxFileOutputStream file_output( "test_wx.dat" ); | |
273 | wxBufferedOutputStream buf_output( file_output ); | |
274 | for (ch = 0; ch < 10; ch++) | |
275 | buf_output.Write( &ch, 1 ); | |
276 | buf_output.Sync(); | |
277 | ||
278 | wxFileInputStream file_input( "test_wx.dat" ); | |
279 | for (ch2 = 0; ch2 < 10; ch2++) | |
280 | { | |
281 | file_input.Read( &ch, 1 ); | |
282 | textCtrl.WriteText( (char)(ch + '0') ); | |
283 | } | |
284 | textCtrl.WriteText( "\n\n\n" ); | |
285 | ||
286 | textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" ); | |
287 | textCtrl.WriteText( "seeking back to #3 and writing 3:\n\n" ); | |
288 | ||
289 | wxFileOutputStream file_output2( "test_wx2.dat" ); | |
290 | wxBufferedOutputStream buf_output2( file_output2 ); | |
291 | for (ch = 0; ch < 10; ch++) | |
292 | buf_output2.Write( &ch, 1 ); | |
293 | buf_output2.SeekO( 3 ); | |
294 | ch = 3; | |
295 | buf_output2.Write( &ch, 1 ); | |
296 | buf_output2.Sync(); | |
297 | ||
298 | wxFileInputStream file_input2( "test_wx2.dat" ); | |
299 | for (ch2 = 0; ch2 < 10; ch2++) | |
300 | { | |
301 | file_input2.Read( &ch, 1 ); | |
302 | textCtrl.WriteText( (char)(ch + '0') ); | |
303 | } | |
304 | textCtrl.WriteText( "\n\n\n" ); | |
305 | ||
306 | // now append 2000 bytes to file (bigger than buffer) | |
307 | buf_output2.SeekO( 0, wxFromEnd ); | |
308 | ch = 1; | |
309 | for (int i = 0; i < 2000; i++) | |
310 | buf_output2.Write( &ch, 1 ); | |
311 | buf_output2.Sync(); | |
312 | ||
313 | textCtrl.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" ); | |
314 | textCtrl.WriteText( "seeking back to #3 and reading 3:\n\n" ); | |
315 | ||
316 | wxFileInputStream file_input3( "test_wx2.dat" ); | |
317 | wxBufferedInputStream buf_input3( file_input3 ); | |
318 | for (ch2 = 0; ch2 < 10; ch2++) | |
319 | { | |
320 | buf_input3.Read( &ch, 1 ); | |
321 | textCtrl.WriteText( (char)(ch + '0') ); | |
322 | } | |
323 | for (int j = 0; j < 2000; j++) | |
324 | buf_input3.Read( &ch, 1 ); | |
325 | textCtrl.WriteText( "\n" ); | |
326 | buf_input3.SeekI( 3 ); | |
327 | buf_input3.Read( &ch, 1 ); | |
328 | textCtrl.WriteText( (char)(ch + '0') ); | |
329 | textCtrl.WriteText( "\n\n\n" ); | |
330 | ||
331 | } | |
332 | ||
dcf924a3 RR |
333 | #if wxUSE_UNICODE |
334 | void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) | |
335 | { | |
336 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 337 | |
dcf924a3 RR |
338 | textCtrl.Clear(); |
339 | textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:"; | |
340 | ||
341 | wxString str; | |
342 |