]>
Commit | Line | Data |
---|---|---|
cfb88c55 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: typetest.cpp | |
be5a51fb | 3 | // Purpose: Types wxWidgets sample |
cfb88c55 JS |
4 | // Author: Julian Smart |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6aa89a22 | 8 | // Copyright: (c) Julian Smart |
8e124873 | 9 | // Licence: wxWindows license |
cfb88c55 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
cfb88c55 JS |
12 | // For compilers that support precompilation, includes "wx/wx.h". |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/wx.h" | |
21 | #endif | |
22 | ||
cfb88c55 | 23 | #include "wx/variant.h" |
8e124873 | 24 | #include "wx/mimetype.h" |
cfb88c55 JS |
25 | |
26 | #include "typetest.h" | |
27 | ||
618f2efa | 28 | #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) |
cfb88c55 JS |
29 | #include "mondrian.xpm" |
30 | #endif | |
31 | ||
3f1802b5 JS |
32 | #ifdef new |
33 | #undef new | |
34 | #endif | |
35 | ||
38830220 RR |
36 | #include "wx/ioswrap.h" |
37 | ||
38 | #if wxUSE_IOSTREAMH | |
39 | #include <fstream.h> | |
40 | #else | |
41 | #include <fstream> | |
42 | #endif | |
43 | ||
44 | #include "wx/wfstream.h" | |
53daeada | 45 | #include "wx/datstrm.h" |
fae05df5 | 46 | #include "wx/txtstrm.h" |
f6bcfd97 | 47 | #include "wx/mstream.h" |
38830220 | 48 | |
cfb88c55 | 49 | // Create a new application object |
8e124873 | 50 | IMPLEMENT_APP (MyApp) |
cfb88c55 | 51 | |
8e124873 | 52 | IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp) |
cfb88c55 JS |
53 | |
54 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
8e124873 VZ |
55 | EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo) |
56 | EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo) | |
dcf924a3 | 57 | #if wxUSE_UNICODE |
8e124873 | 58 | EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) |
3409ae17 | 59 | #endif // wxUSE_UNICODE |
8e124873 | 60 | EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) |
aa51b2e1 RR |
61 | EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2) |
62 | EVT_MENU(TYPES_STREAM3, MyApp::DoStreamDemo3) | |
2bf8e4eb | 63 | EVT_MENU(TYPES_STREAM4, MyApp::DoStreamDemo4) |
f6bcfd97 | 64 | EVT_MENU(TYPES_STREAM5, MyApp::DoStreamDemo5) |
cbc906ce RR |
65 | EVT_MENU(TYPES_STREAM6, MyApp::DoStreamDemo6) |
66 | EVT_MENU(TYPES_STREAM7, MyApp::DoStreamDemo7) | |
8e124873 | 67 | EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo) |
cfb88c55 JS |
68 | END_EVENT_TABLE() |
69 | ||
925e9792 WS |
70 | wxString file_name = _T("test_wx.dat"); |
71 | wxString file_name2 = wxString(_T("test_wx2.dat")); | |
72 | ||
8e124873 | 73 | bool MyApp::OnInit() |
cfb88c55 | 74 | { |
45e6e6f8 VZ |
75 | if ( !wxApp::OnInit() ) |
76 | return false; | |
77 | ||
8e124873 | 78 | // Create the main frame window |
be5a51fb | 79 | MyFrame *frame = new MyFrame((wxFrame *) NULL, _T("wxWidgets Types Demo"), |
8e124873 VZ |
80 | wxPoint(50, 50), wxSize(450, 340)); |
81 | ||
82 | // Give it an icon | |
83 | frame->SetIcon(wxICON(mondrian)); | |
84 | ||
85 | // Make a menubar | |
86 | wxMenu *file_menu = new wxMenu; | |
87 | ||
ab1ca7b3 | 88 | file_menu->Append(TYPES_ABOUT, _T("&About")); |
8e124873 | 89 | file_menu->AppendSeparator(); |
ab1ca7b3 | 90 | file_menu->Append(TYPES_QUIT, _T("E&xit\tAlt-X")); |
8e124873 VZ |
91 | |
92 | wxMenu *test_menu = new wxMenu; | |
ab1ca7b3 MB |
93 | test_menu->Append(TYPES_VARIANT, _T("&Variant test")); |
94 | test_menu->Append(TYPES_BYTEORDER, _T("&Byteorder test")); | |
dcf924a3 | 95 | #if wxUSE_UNICODE |
ab1ca7b3 | 96 | test_menu->Append(TYPES_UNICODE, _T("&Unicode test")); |
3409ae17 | 97 | #endif // wxUSE_UNICODE |
ab1ca7b3 MB |
98 | test_menu->Append(TYPES_STREAM, _T("&Stream test")); |
99 | test_menu->Append(TYPES_STREAM2, _T("&Stream seek test")); | |
100 | test_menu->Append(TYPES_STREAM3, _T("&Stream error test")); | |
101 | test_menu->Append(TYPES_STREAM4, _T("&Stream buffer test")); | |
102 | test_menu->Append(TYPES_STREAM5, _T("&Stream peek test")); | |
103 | test_menu->Append(TYPES_STREAM6, _T("&Stream ungetch test")); | |
104 | test_menu->Append(TYPES_STREAM7, _T("&Stream ungetch test for a buffered stream")); | |
8e124873 | 105 | test_menu->AppendSeparator(); |
ab1ca7b3 | 106 | test_menu->Append(TYPES_MIME, _T("&MIME database test")); |
cfb88c55 | 107 | |
8e124873 | 108 | wxMenuBar *menu_bar = new wxMenuBar; |
ab1ca7b3 MB |
109 | menu_bar->Append(file_menu, _T("&File")); |
110 | menu_bar->Append(test_menu, _T("&Tests")); | |
8e124873 | 111 | frame->SetMenuBar(menu_bar); |
cfb88c55 | 112 | |
206d3a16 JS |
113 | m_textCtrl = new wxTextCtrl(frame, wxID_ANY, wxEmptyString, |
114 | wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE); | |
cfb88c55 | 115 | |
8e124873 | 116 | // Show the frame |
206d3a16 | 117 | frame->Show(true); |
8e124873 VZ |
118 | |
119 | SetTopWindow(frame); | |
120 | ||
206d3a16 | 121 | return true; |
cfb88c55 JS |
122 | } |
123 | ||
38830220 RR |
124 | void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) |
125 | { | |
126 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 127 | |
38830220 | 128 | textCtrl.Clear(); |
c980c992 | 129 | textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n"); |
38830220 | 130 | |
ab1ca7b3 | 131 | textCtrl.WriteText( _T("Writing to ofstream and wxFileOutputStream:\n") ); |
8e124873 | 132 | |
dd107c50 | 133 | wxSTD ofstream std_file_output( "test_std.dat" ); |
925e9792 | 134 | wxFileOutputStream file_output( file_name ); |
fae05df5 GL |
135 | wxBufferedOutputStream buf_output( file_output ); |
136 | wxTextOutputStream text_output( buf_output ); | |
38830220 | 137 | |
38830220 RR |
138 | wxString tmp; |
139 | signed int si = 0xFFFFFFFF; | |
c980c992 | 140 | tmp.Printf( _T("Signed int: %d\n"), si ); |
38830220 | 141 | textCtrl.WriteText( tmp ); |
ab1ca7b3 | 142 | text_output << si << _T("\n"); |
19b44116 | 143 | std_file_output << si << "\n"; |
8e124873 | 144 | |
38830220 | 145 | unsigned int ui = 0xFFFFFFFF; |
c980c992 | 146 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
38830220 | 147 | textCtrl.WriteText( tmp ); |
ab1ca7b3 | 148 | text_output << ui << _T("\n"); |
19b44116 | 149 | std_file_output << ui << "\n"; |
8e124873 | 150 | |
38830220 | 151 | double d = 2.01234567890123456789; |
c980c992 | 152 | tmp.Printf( _T("Double: %f\n"), d ); |
38830220 | 153 | textCtrl.WriteText( tmp ); |
ab1ca7b3 | 154 | text_output << d << _T("\n"); |
19b44116 | 155 | std_file_output << d << "\n"; |
8e124873 VZ |
156 | |
157 | float f = (float)0.00001; | |
c980c992 | 158 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 159 | textCtrl.WriteText( tmp ); |
ab1ca7b3 | 160 | text_output << f << _T("\n"); |
19b44116 | 161 | std_file_output << f << "\n"; |
8e124873 | 162 | |
c980c992 GL |
163 | wxString str( _T("Hello!") ); |
164 | tmp.Printf( _T("String: %s\n"), str.c_str() ); | |
38830220 | 165 | textCtrl.WriteText( tmp ); |
ab1ca7b3 | 166 | text_output << str << _T("\n"); |
19b44116 | 167 | std_file_output << str.ToAscii() << "\n"; |
2b5f62a0 | 168 | |
ab1ca7b3 | 169 | textCtrl.WriteText( _T("\nReading from ifstream:\n") ); |
8e124873 | 170 | |
dd107c50 | 171 | wxSTD ifstream std_file_input( "test_std.dat" ); |
e57e26dd RR |
172 | |
173 | std_file_input >> si; | |
c980c992 | 174 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 175 | textCtrl.WriteText( tmp ); |
8e124873 | 176 | |
e57e26dd | 177 | std_file_input >> ui; |
c980c992 | 178 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 179 | textCtrl.WriteText( tmp ); |
8e124873 | 180 | |
e57e26dd | 181 | std_file_input >> d; |
c980c992 | 182 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 183 | textCtrl.WriteText( tmp ); |
8e124873 | 184 | |
e57e26dd | 185 | std_file_input >> f; |
c980c992 | 186 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 187 | textCtrl.WriteText( tmp ); |
8e124873 | 188 | |
f6bcfd97 BP |
189 | char std_buf[200]; |
190 | std_file_input >> std_buf; | |
2a1f999f | 191 | str = wxString::FromAscii(std_buf); |
19b44116 | 192 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 193 | textCtrl.WriteText( tmp ); |
8e124873 | 194 | |
ab1ca7b3 | 195 | textCtrl.WriteText( _T("\nReading from wxFileInputStream:\n") ); |
b6bff301 | 196 | |
fae05df5 | 197 | buf_output.Sync(); |
8e124873 | 198 | |
925e9792 | 199 | wxFileInputStream file_input( file_name ); |
fae05df5 | 200 | wxBufferedInputStream buf_input( file_input ); |
78e848ca | 201 | wxTextInputStream text_input( file_input ); |
8e124873 | 202 | |
fae05df5 | 203 | text_input >> si; |
c980c992 | 204 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 205 | textCtrl.WriteText( tmp ); |
8e124873 | 206 | |
fae05df5 | 207 | text_input >> ui; |
c980c992 | 208 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 209 | textCtrl.WriteText( tmp ); |
8e124873 | 210 | |
fae05df5 | 211 | text_input >> d; |
c980c992 | 212 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 213 | textCtrl.WriteText( tmp ); |
8e124873 | 214 | |
fae05df5 | 215 | text_input >> f; |
c980c992 | 216 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 217 | textCtrl.WriteText( tmp ); |
8e124873 | 218 | |
fae05df5 | 219 | text_input >> str; |
c980c992 | 220 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 221 | textCtrl.WriteText( tmp ); |
8e124873 | 222 | |
53daeada | 223 | |
f6bcfd97 | 224 | |
ab1ca7b3 | 225 | textCtrl << _T("\nTest for wxDataStream:\n\n"); |
53daeada | 226 | |
ab1ca7b3 | 227 | textCtrl.WriteText( _T("Writing to wxDataOutputStream:\n") ); |
8e124873 | 228 | |
53daeada | 229 | file_output.SeekO( 0 ); |
fae05df5 | 230 | wxDataOutputStream data_output( buf_output ); |
53daeada | 231 | |
364f80ef | 232 | wxInt16 i16 = (unsigned short)0xFFFF; |
c980c992 | 233 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 234 | textCtrl.WriteText( tmp ); |
329e86bf | 235 | data_output.Write16( i16 ); |
8e124873 | 236 | |
329e86bf | 237 | wxUint16 ui16 = 0xFFFF; |
c980c992 | 238 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada | 239 | textCtrl.WriteText( tmp ); |
329e86bf | 240 | data_output.Write16( ui16 ); |
8e124873 | 241 | |
53daeada | 242 | d = 2.01234567890123456789; |
c980c992 | 243 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada RR |
244 | textCtrl.WriteText( tmp ); |
245 | data_output.WriteDouble( d ); | |
8e124873 | 246 | |
ab1ca7b3 | 247 | str = _T("Hello!"); |
c980c992 | 248 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada RR |
249 | textCtrl.WriteText( tmp ); |
250 | data_output.WriteString( str ); | |
8e124873 | 251 | |
fae05df5 | 252 | buf_output.Sync(); |
8e124873 | 253 | |
ab1ca7b3 | 254 | textCtrl.WriteText( _T("\nReading from wxDataInputStream:\n") ); |
8e124873 | 255 | |
53daeada | 256 | file_input.SeekI( 0 ); |
fae05df5 | 257 | wxDataInputStream data_input( buf_input ); |
53daeada | 258 | |
329e86bf | 259 | i16 = data_input.Read16(); |
c980c992 | 260 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 261 | textCtrl.WriteText( tmp ); |
8e124873 | 262 | |
329e86bf | 263 | ui16 = data_input.Read16(); |
c980c992 | 264 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada RR |
265 | textCtrl.WriteText( tmp ); |
266 | ||
267 | d = data_input.ReadDouble(); | |
c980c992 | 268 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada | 269 | textCtrl.WriteText( tmp ); |
8e124873 | 270 | |
53daeada | 271 | str = data_input.ReadString(); |
c980c992 | 272 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada | 273 | textCtrl.WriteText( tmp ); |
38830220 RR |
274 | } |
275 | ||
1b055864 RR |
276 | void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) |
277 | { | |
278 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
279 | ||
280 | textCtrl.Clear(); | |
aa51b2e1 | 281 | textCtrl << _T("\nTesting wxBufferedStream:\n\n"); |
1b055864 RR |
282 | |
283 | char ch,ch2; | |
284 | ||
ab1ca7b3 | 285 | textCtrl.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream:\n\n") ); |
1b055864 | 286 | |
925e9792 | 287 | wxFileOutputStream file_output( file_name ); |
1b055864 RR |
288 | wxBufferedOutputStream buf_output( file_output ); |
289 | for (ch = 0; ch < 10; ch++) | |
290 | buf_output.Write( &ch, 1 ); | |
291 | buf_output.Sync(); | |
2b5f62a0 | 292 | |
925e9792 | 293 | wxFileInputStream file_input( file_name ); |
1b055864 RR |
294 | for (ch2 = 0; ch2 < 10; ch2++) |
295 | { | |
296 | file_input.Read( &ch, 1 ); | |
ab1ca7b3 | 297 | textCtrl.WriteText( (wxChar)(ch + _T('0')) ); |
1b055864 | 298 | } |
ab1ca7b3 | 299 | textCtrl.WriteText( _T("\n\n\n") ); |
2b5f62a0 | 300 | |
ab1ca7b3 MB |
301 | textCtrl.WriteText( _T("Writing number 0 to 9 to buffered wxFileOutputStream, then\n") ); |
302 | textCtrl.WriteText( _T("seeking back to #3 and writing 0:\n\n") ); | |
1b055864 | 303 | |
925e9792 | 304 | wxFileOutputStream file_output2( file_name2 ); |
1b055864 RR |
305 | wxBufferedOutputStream buf_output2( file_output2 ); |
306 | for (ch = 0; ch < 10; ch++) | |
307 | buf_output2.Write( &ch, 1 ); | |
308 | buf_output2.SeekO( 3 ); | |
cbc906ce | 309 | ch = 0; |
1b055864 RR |
310 | buf_output2.Write( &ch, 1 ); |
311 | buf_output2.Sync(); | |
2b5f62a0 | 312 | |
925e9792 | 313 | wxFileInputStream file_input2( file_name2 ); |
1b055864 RR |
314 | for (ch2 = 0; ch2 < 10; ch2++) |
315 | { | |
316 | file_input2.Read( &ch, 1 ); | |
ab1ca7b3 | 317 | textCtrl.WriteText( (wxChar)(ch + _T('0')) ); |
1b055864 | 318 | } |
ab1ca7b3 | 319 | textCtrl.WriteText( _T("\n\n\n") ); |
2b5f62a0 | 320 | |
1b055864 RR |
321 | // now append 2000 bytes to file (bigger than buffer) |
322 | buf_output2.SeekO( 0, wxFromEnd ); | |
323 | ch = 1; | |
324 | for (int i = 0; i < 2000; i++) | |
325 | buf_output2.Write( &ch, 1 ); | |
326 | buf_output2.Sync(); | |
2b5f62a0 | 327 | |
ab1ca7b3 MB |
328 | textCtrl.WriteText( _T("Reading number 0 to 9 from buffered wxFileInputStream, then\n") ); |
329 | textCtrl.WriteText( _T("seeking back to #3 and reading the 0:\n\n") ); | |
1b055864 | 330 | |
925e9792 | 331 | wxFileInputStream file_input3( file_name2 ); |
1b055864 RR |
332 | wxBufferedInputStream buf_input3( file_input3 ); |
333 | for (ch2 = 0; ch2 < 10; ch2++) | |
334 | { | |
335 | buf_input3.Read( &ch, 1 ); | |
ab1ca7b3 | 336 | textCtrl.WriteText( (wxChar)(ch + _T('0')) ); |
1b055864 RR |
337 | } |
338 | for (int j = 0; j < 2000; j++) | |
339 | buf_input3.Read( &ch, 1 ); | |
ab1ca7b3 | 340 | textCtrl.WriteText( _T("\n") ); |
1b055864 RR |
341 | buf_input3.SeekI( 3 ); |
342 | buf_input3.Read( &ch, 1 ); | |
ab1ca7b3 MB |
343 | textCtrl.WriteText( (wxChar)(ch + _T('0')) ); |
344 | textCtrl.WriteText( _T("\n\n\n") ); | |
2b5f62a0 | 345 | |
1b055864 RR |
346 | } |
347 | ||
aa51b2e1 RR |
348 | void MyApp::DoStreamDemo3(wxCommandEvent& WXUNUSED(event)) |
349 | { | |
350 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
351 | ||
352 | textCtrl.Clear(); | |
ab1ca7b3 | 353 | textCtrl << _T("\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n"); |
aa51b2e1 RR |
354 | |
355 | char ch,ch2; | |
356 | ||
ab1ca7b3 | 357 | textCtrl.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") ); |
aa51b2e1 | 358 | |
925e9792 | 359 | wxFileOutputStream file_output( file_name ); |
aa51b2e1 RR |
360 | for (ch = 0; ch < 10; ch++) |
361 | file_output.Write( &ch, 1 ); | |
362 | ||
842d6c94 | 363 | // Testing wxFileInputStream |
2b5f62a0 | 364 | |
ab1ca7b3 | 365 | textCtrl.WriteText( _T("Reading 0 to 10 to wxFileInputStream:\n\n") ); |
aa51b2e1 | 366 | |
925e9792 | 367 | wxFileInputStream file_input( file_name ); |
aa51b2e1 RR |
368 | for (ch2 = 0; ch2 < 11; ch2++) |
369 | { | |
370 | file_input.Read( &ch, 1 ); | |
ab1ca7b3 | 371 | textCtrl.WriteText( _T("Value read: ") ); |
4693b20c | 372 | textCtrl.WriteText( (wxChar)(ch + '0') ); |
ab1ca7b3 | 373 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); |
2b5f62a0 | 374 | switch (file_input.GetLastError()) |
f6bcfd97 | 375 | { |
ab1ca7b3 MB |
376 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
377 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
378 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
379 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
380 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
f6bcfd97 | 381 | } |
aa51b2e1 | 382 | } |
ab1ca7b3 | 383 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 384 | |
ab1ca7b3 | 385 | textCtrl.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") ); |
aa51b2e1 | 386 | file_input.SeekI( 0 ); |
2b5f62a0 | 387 | switch (file_input.GetLastError()) |
aa51b2e1 | 388 | { |
ab1ca7b3 MB |
389 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
390 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
391 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
392 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
393 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
aa51b2e1 | 394 | } |
ab1ca7b3 | 395 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 396 | |
aa51b2e1 | 397 | file_input.Read( &ch, 1 ); |
ab1ca7b3 MB |
398 | textCtrl.WriteText( _T("Value read: ") ); |
399 | textCtrl.WriteText( (wxChar)(ch + _T('0')) ); | |
400 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); | |
2b5f62a0 | 401 | switch (file_input.GetLastError()) |
aa51b2e1 | 402 | { |
ab1ca7b3 MB |
403 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
404 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
405 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
406 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
407 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
aa51b2e1 | 408 | } |
ab1ca7b3 | 409 | textCtrl.WriteText( _T("\n\n") ); |
aa51b2e1 | 410 | |
2b5f62a0 | 411 | |
842d6c94 | 412 | // Testing wxFFileInputStream |
2b5f62a0 | 413 | |
ab1ca7b3 | 414 | textCtrl.WriteText( _T("Reading 0 to 10 to wxFFileInputStream:\n\n") ); |
aa51b2e1 | 415 | |
925e9792 | 416 | wxFFileInputStream ffile_input( file_name ); |
aa51b2e1 RR |
417 | for (ch2 = 0; ch2 < 11; ch2++) |
418 | { | |
419 | ffile_input.Read( &ch, 1 ); | |
ab1ca7b3 | 420 | textCtrl.WriteText( _T("Value read: ") ); |
4693b20c | 421 | textCtrl.WriteText( (wxChar)(ch + '0') ); |
ab1ca7b3 | 422 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); |
2b5f62a0 | 423 | switch (ffile_input.GetLastError()) |
f6bcfd97 | 424 | { |
ab1ca7b3 MB |
425 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
426 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
427 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
428 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
429 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2f6c54eb | 430 | } |
aa51b2e1 | 431 | } |
ab1ca7b3 | 432 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 433 | |
ab1ca7b3 | 434 | textCtrl.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") ); |
aa51b2e1 | 435 | ffile_input.SeekI( 0 ); |
2b5f62a0 | 436 | switch (ffile_input.GetLastError()) |
aa51b2e1 | 437 | { |
ab1ca7b3 MB |
438 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
439 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
440 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
441 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
442 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
aa51b2e1 | 443 | } |
ab1ca7b3 | 444 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 445 | |
aa51b2e1 | 446 | ffile_input.Read( &ch, 1 ); |
ab1ca7b3 | 447 | textCtrl.WriteText( _T("Value read: ") ); |
4693b20c | 448 | textCtrl.WriteText( (wxChar)(ch + '0') ); |
ab1ca7b3 | 449 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); |
2b5f62a0 | 450 | switch (ffile_input.GetLastError()) |
aa51b2e1 | 451 | { |
ab1ca7b3 MB |
452 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
453 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
454 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
455 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
456 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
842d6c94 | 457 | } |
ab1ca7b3 | 458 | textCtrl.WriteText( _T("\n\n") ); |
842d6c94 RR |
459 | |
460 | // Testing wxFFileInputStream | |
2b5f62a0 | 461 | |
ab1ca7b3 | 462 | textCtrl.WriteText( _T("Reading 0 to 10 to buffered wxFFileInputStream:\n\n") ); |
842d6c94 | 463 | |
925e9792 | 464 | wxFFileInputStream ffile_input2( file_name ); |
842d6c94 RR |
465 | wxBufferedInputStream buf_input( ffile_input2 ); |
466 | for (ch2 = 0; ch2 < 11; ch2++) | |
467 | { | |
468 | buf_input.Read( &ch, 1 ); | |
ab1ca7b3 | 469 | textCtrl.WriteText( _T("Value read: ") ); |
4693b20c | 470 | textCtrl.WriteText( (wxChar)(ch + '0') ); |
ab1ca7b3 | 471 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); |
2b5f62a0 | 472 | switch (buf_input.GetLastError()) |
f6bcfd97 | 473 | { |
ab1ca7b3 MB |
474 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
475 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
476 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
477 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
478 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
f6bcfd97 | 479 | } |
842d6c94 | 480 | } |
ab1ca7b3 | 481 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 482 | |
ab1ca7b3 | 483 | textCtrl.WriteText( _T("Seeking to 0; stream.GetLastError() returns: ") ); |
842d6c94 | 484 | buf_input.SeekI( 0 ); |
2b5f62a0 | 485 | switch (buf_input.GetLastError()) |
842d6c94 | 486 | { |
ab1ca7b3 MB |
487 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
488 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
489 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
490 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
491 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
842d6c94 | 492 | } |
ab1ca7b3 | 493 | textCtrl.WriteText( _T("\n") ); |
2b5f62a0 | 494 | |
842d6c94 | 495 | buf_input.Read( &ch, 1 ); |
ab1ca7b3 | 496 | textCtrl.WriteText( _T("Value read: ") ); |
4693b20c | 497 | textCtrl.WriteText( (wxChar)(ch + '0') ); |
ab1ca7b3 | 498 | textCtrl.WriteText( _T("; stream.GetLastError() returns: ") ); |
2b5f62a0 | 499 | switch (buf_input.GetLastError()) |
842d6c94 | 500 | { |
ab1ca7b3 MB |
501 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
502 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
503 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
504 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
505 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
aa51b2e1 | 506 | } |
2bf8e4eb RR |
507 | } |
508 | ||
509 | void MyApp::DoStreamDemo4(wxCommandEvent& WXUNUSED(event)) | |
510 | { | |
511 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
512 | ||
513 | wxString msg; | |
2b5f62a0 | 514 | |
2bf8e4eb | 515 | textCtrl.Clear(); |
ab1ca7b3 | 516 | textCtrl << _T("\nTesting wxStreamBuffer:\n\n"); |
2bf8e4eb RR |
517 | |
518 | // bigger than buffer | |
ab1ca7b3 | 519 | textCtrl.WriteText( _T("Writing 2000x 1 to wxFileOutputStream.\n\n") ); |
2bf8e4eb | 520 | |
925e9792 | 521 | wxFileOutputStream file_output( file_name ); |
2bf8e4eb RR |
522 | for (int i = 0; i < 2000; i++) |
523 | { | |
524 | char ch = 1; | |
525 | file_output.Write( &ch, 1 ); | |
526 | } | |
527 | ||
ab1ca7b3 | 528 | textCtrl.WriteText( _T("Opening with a buffered wxFileInputStream:\n\n") ); |
2bf8e4eb | 529 | |
925e9792 | 530 | wxFileInputStream file_input( file_name ); |
2bf8e4eb | 531 | wxBufferedInputStream buf_input( file_input ); |
2b5f62a0 | 532 | |
ab1ca7b3 | 533 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 534 | switch (buf_input.GetLastError()) |
2bf8e4eb | 535 | { |
ab1ca7b3 MB |
536 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
537 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
538 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
539 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
540 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 541 | } |
4693b20c | 542 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 543 | textCtrl.WriteText( msg ); |
4693b20c | 544 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 545 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 546 | textCtrl.WriteText( _T("\n\n") ); |
2b5f62a0 | 547 | |
2bf8e4eb | 548 | |
ab1ca7b3 | 549 | textCtrl.WriteText( _T("Seeking to position 300:\n\n") ); |
2bf8e4eb RR |
550 | |
551 | buf_input.SeekI( 300 ); | |
2b5f62a0 | 552 | |
ab1ca7b3 | 553 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 554 | switch (buf_input.GetLastError()) |
2bf8e4eb | 555 | { |
ab1ca7b3 MB |
556 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
557 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
558 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
559 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
560 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 561 | } |
4693b20c | 562 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 563 | textCtrl.WriteText( msg ); |
4693b20c | 564 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 565 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 566 | textCtrl.WriteText( _T("\n\n") ); |
2b5f62a0 | 567 | |
2bf8e4eb RR |
568 | |
569 | char buf[2000]; | |
570 | ||
ab1ca7b3 | 571 | textCtrl.WriteText( _T("Reading 500 bytes:\n\n") ); |
aa51b2e1 | 572 | |
2bf8e4eb | 573 | buf_input.Read( buf, 500 ); |
2b5f62a0 | 574 | |
ab1ca7b3 | 575 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 576 | switch (buf_input.GetLastError()) |
2bf8e4eb | 577 | { |
ab1ca7b3 MB |
578 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
579 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
580 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
581 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
582 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 583 | } |
4693b20c | 584 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 585 | textCtrl.WriteText( msg ); |
4693b20c | 586 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 587 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 588 | textCtrl.WriteText( _T("\n\n") ); |
2b5f62a0 | 589 | |
ab1ca7b3 | 590 | textCtrl.WriteText( _T("Reading another 500 bytes:\n\n") ); |
2bf8e4eb RR |
591 | |
592 | buf_input.Read( buf, 500 ); | |
2b5f62a0 | 593 | |
ab1ca7b3 | 594 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 595 | switch (buf_input.GetLastError()) |
2bf8e4eb | 596 | { |
ab1ca7b3 MB |
597 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
598 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
599 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
600 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
601 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 602 | } |
4693b20c | 603 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 604 | textCtrl.WriteText( msg ); |
4693b20c | 605 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 606 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 607 | textCtrl.WriteText( _T("\n\n") ); |
2bf8e4eb | 608 | |
ab1ca7b3 | 609 | textCtrl.WriteText( _T("Reading another 500 bytes:\n\n") ); |
2bf8e4eb RR |
610 | |
611 | buf_input.Read( buf, 500 ); | |
2b5f62a0 | 612 | |
ab1ca7b3 | 613 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 614 | switch (buf_input.GetLastError()) |
2bf8e4eb | 615 | { |
ab1ca7b3 MB |
616 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
617 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
618 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
619 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
620 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 621 | } |
4693b20c | 622 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 623 | textCtrl.WriteText( msg ); |
4693b20c | 624 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 625 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 626 | textCtrl.WriteText( _T("\n\n") ); |
2bf8e4eb | 627 | |
ab1ca7b3 | 628 | textCtrl.WriteText( _T("Reading another 500 bytes:\n\n") ); |
2bf8e4eb RR |
629 | |
630 | buf_input.Read( buf, 500 ); | |
2b5f62a0 | 631 | |
ab1ca7b3 | 632 | textCtrl.WriteText( _T("wxBufferedInputStream.GetLastError() returns: ") ); |
2b5f62a0 | 633 | switch (buf_input.GetLastError()) |
2bf8e4eb | 634 | { |
ab1ca7b3 MB |
635 | case wxSTREAM_NO_ERROR: textCtrl.WriteText( _T("wxSTREAM_NO_ERROR\n") ); break; |
636 | case wxSTREAM_EOF: textCtrl.WriteText( _T("wxSTREAM_EOF\n") ); break; | |
637 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( _T("wxSTREAM_READ_ERROR\n") ); break; | |
638 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( _T("wxSTREAM_WRITE_ERROR\n") ); break; | |
639 | default: textCtrl.WriteText( _T("Huh?\n") ); break; | |
2bf8e4eb | 640 | } |
4693b20c | 641 | msg.Printf( wxT("wxBufferedInputStream.LastRead() returns: %d\n"), (int)buf_input.LastRead() ); |
2bf8e4eb | 642 | textCtrl.WriteText( msg ); |
4693b20c | 643 | msg.Printf( wxT("wxBufferedInputStream.TellI() returns: %d\n"), (int)buf_input.TellI() ); |
2bf8e4eb | 644 | textCtrl.WriteText( msg ); |
ab1ca7b3 | 645 | textCtrl.WriteText( _T("\n\n") ); |
aa51b2e1 RR |
646 | } |
647 | ||
f6bcfd97 BP |
648 | void MyApp::DoStreamDemo5(wxCommandEvent& WXUNUSED(event)) |
649 | { | |
650 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
651 | ||
652 | textCtrl.Clear(); | |
ab1ca7b3 | 653 | textCtrl << _T("\nTesting wxFileInputStream's Peek():\n\n"); |
f6bcfd97 BP |
654 | |
655 | char ch; | |
656 | wxString str; | |
657 | ||
ab1ca7b3 | 658 | textCtrl.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream:\n\n") ); |
f6bcfd97 | 659 | |
925e9792 | 660 | wxFileOutputStream file_output( file_name ); |
f6bcfd97 BP |
661 | for (ch = 0; ch < 10; ch++) |
662 | file_output.Write( &ch, 1 ); | |
2b5f62a0 | 663 | |
f6bcfd97 | 664 | file_output.Sync(); |
2b5f62a0 | 665 | |
925e9792 | 666 | wxFileInputStream file_input( file_name ); |
2b5f62a0 | 667 | |
f6bcfd97 | 668 | ch = file_input.Peek(); |
4693b20c | 669 | str.Printf( wxT("First char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 670 | textCtrl.WriteText( str ); |
2b5f62a0 | 671 | |
f6bcfd97 | 672 | ch = file_input.GetC(); |
4693b20c | 673 | str.Printf( wxT("First char read: %d\n"), (int) ch ); |
f6bcfd97 | 674 | textCtrl.WriteText( str ); |
2b5f62a0 | 675 | |
f6bcfd97 | 676 | ch = file_input.Peek(); |
4693b20c | 677 | str.Printf( wxT("Second char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 678 | textCtrl.WriteText( str ); |
2b5f62a0 | 679 | |
f6bcfd97 | 680 | ch = file_input.GetC(); |
4693b20c | 681 | str.Printf( wxT("Second char read: %d\n"), (int) ch ); |
f6bcfd97 | 682 | textCtrl.WriteText( str ); |
2b5f62a0 | 683 | |
f6bcfd97 | 684 | ch = file_input.Peek(); |
4693b20c | 685 | str.Printf( wxT("Third char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 686 | textCtrl.WriteText( str ); |
2b5f62a0 | 687 | |
f6bcfd97 | 688 | ch = file_input.GetC(); |
4693b20c | 689 | str.Printf( wxT("Third char read: %d\n"), (int) ch ); |
f6bcfd97 | 690 | textCtrl.WriteText( str ); |
2b5f62a0 VZ |
691 | |
692 | ||
ab1ca7b3 | 693 | textCtrl << _T("\n\n\nTesting wxMemoryInputStream's Peek():\n\n"); |
2b5f62a0 | 694 | |
f6bcfd97 BP |
695 | char buf[] = { 0,1,2,3,4,5,6,7,8,9,10 }; |
696 | wxMemoryInputStream input( buf, 10 ); | |
2b5f62a0 | 697 | |
f6bcfd97 | 698 | ch = input.Peek(); |
4693b20c | 699 | str.Printf( wxT("First char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 700 | textCtrl.WriteText( str ); |
2b5f62a0 | 701 | |
f6bcfd97 | 702 | ch = input.GetC(); |
4693b20c | 703 | str.Printf( wxT("First char read: %d\n"), (int) ch ); |
f6bcfd97 | 704 | textCtrl.WriteText( str ); |
2b5f62a0 | 705 | |
f6bcfd97 | 706 | ch = input.Peek(); |
4693b20c | 707 | str.Printf( wxT("Second char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 708 | textCtrl.WriteText( str ); |
2b5f62a0 | 709 | |
f6bcfd97 | 710 | ch = input.GetC(); |
4693b20c | 711 | str.Printf( wxT("Second char read: %d\n"), (int) ch ); |
f6bcfd97 | 712 | textCtrl.WriteText( str ); |
2b5f62a0 | 713 | |
f6bcfd97 | 714 | ch = input.Peek(); |
4693b20c | 715 | str.Printf( wxT("Third char peeked: %d\n"), (int) ch ); |
f6bcfd97 | 716 | textCtrl.WriteText( str ); |
2b5f62a0 | 717 | |
f6bcfd97 | 718 | ch = input.GetC(); |
4693b20c | 719 | str.Printf( wxT("Third char read: %d\n"), (int) ch ); |
f6bcfd97 BP |
720 | textCtrl.WriteText( str ); |
721 | } | |
722 | ||
cbc906ce RR |
723 | void MyApp::DoStreamDemo6(wxCommandEvent& WXUNUSED(event)) |
724 | { | |
725 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
726 | ||
727 | textCtrl.Clear(); | |
ab1ca7b3 | 728 | textCtrl.WriteText( _T("\nTesting Ungetch():\n\n") ); |
2b5f62a0 | 729 | |
cbc906ce | 730 | char ch = 0; |
cbc906ce RR |
731 | wxString str; |
732 | ||
ab1ca7b3 | 733 | textCtrl.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") ); |
cbc906ce | 734 | |
925e9792 | 735 | wxFileOutputStream file_output( file_name ); |
cbc906ce RR |
736 | for (ch = 0; ch < 10; ch++) |
737 | file_output.Write( &ch, 1 ); | |
2b5f62a0 | 738 | |
cbc906ce | 739 | file_output.Sync(); |
2b5f62a0 | 740 | |
ab1ca7b3 | 741 | textCtrl.WriteText( _T("Reading char from wxFileInputStream:\n\n") ); |
cbc906ce | 742 | |
925e9792 | 743 | wxFileInputStream file_input( file_name ); |
2b5f62a0 | 744 | |
cbc906ce | 745 | ch = file_input.GetC(); |
925e9792 | 746 | size_t pos = (size_t)file_input.TellI(); |
cbc906ce RR |
747 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
748 | textCtrl.WriteText( str ); | |
2b5f62a0 | 749 | |
ab1ca7b3 | 750 | textCtrl.WriteText( _T("Reading another char from wxFileInputStream:\n\n") ); |
cbc906ce RR |
751 | |
752 | ch = file_input.GetC(); | |
925e9792 | 753 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
754 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
755 | textCtrl.WriteText( str ); | |
2b5f62a0 | 756 | |
ab1ca7b3 | 757 | textCtrl.WriteText( _T("Reading yet another char from wxFileInputStream:\n\n") ); |
cbc906ce RR |
758 | |
759 | ch = file_input.GetC(); | |
925e9792 | 760 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
761 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
762 | textCtrl.WriteText( str ); | |
2b5f62a0 | 763 | |
ab1ca7b3 | 764 | textCtrl.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream...\n\n") ); |
cbc906ce RR |
765 | |
766 | file_input.Ungetch( 5 ); | |
925e9792 | 767 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
768 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); |
769 | textCtrl.WriteText( str ); | |
2b5f62a0 | 770 | |
ab1ca7b3 | 771 | textCtrl.WriteText( _T("Reading char from wxFileInputStream:\n\n") ); |
cbc906ce RR |
772 | |
773 | ch = file_input.GetC(); | |
925e9792 | 774 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
775 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
776 | textCtrl.WriteText( str ); | |
2b5f62a0 | 777 | |
ab1ca7b3 | 778 | textCtrl.WriteText( _T("Reading another char from wxFileInputStream:\n\n") ); |
cbc906ce RR |
779 | |
780 | ch = file_input.GetC(); | |
925e9792 | 781 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
782 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
783 | textCtrl.WriteText( str ); | |
2b5f62a0 | 784 | |
ab1ca7b3 | 785 | textCtrl.WriteText( _T("Now calling Ungetch( 5 ) from wxFileInputStream again...\n\n") ); |
cbc906ce RR |
786 | |
787 | file_input.Ungetch( 5 ); | |
925e9792 | 788 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
789 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); |
790 | textCtrl.WriteText( str ); | |
2b5f62a0 | 791 | |
ab1ca7b3 | 792 | textCtrl.WriteText( _T("Seeking to pos 3 in wxFileInputStream. This invalidates the writeback buffer.\n\n") ); |
2b5f62a0 | 793 | |
cbc906ce RR |
794 | file_input.SeekI( 3 ); |
795 | ||
796 | ch = file_input.GetC(); | |
925e9792 | 797 | pos = (size_t)file_input.TellI(); |
cbc906ce RR |
798 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
799 | textCtrl.WriteText( str ); | |
800 | } | |
801 | ||
802 | void MyApp::DoStreamDemo7(wxCommandEvent& WXUNUSED(event)) | |
803 | { | |
804 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
805 | ||
806 | textCtrl.Clear(); | |
ab1ca7b3 | 807 | textCtrl.WriteText( _T("\nTesting Ungetch() in buffered input stream:\n\n") ); |
2b5f62a0 | 808 | |
cbc906ce | 809 | char ch = 0; |
cbc906ce RR |
810 | wxString str; |
811 | ||
ab1ca7b3 | 812 | textCtrl.WriteText( _T("Writing number 0 to 9 to wxFileOutputStream...\n\n") ); |
cbc906ce | 813 | |
925e9792 | 814 | wxFileOutputStream file_output( file_name ); |
cbc906ce RR |
815 | for (ch = 0; ch < 10; ch++) |
816 | file_output.Write( &ch, 1 ); | |
2b5f62a0 | 817 | |
cbc906ce | 818 | file_output.Sync(); |
2b5f62a0 | 819 | |
ab1ca7b3 | 820 | textCtrl.WriteText( _T("Reading char from wxBufferedInputStream via wxFileInputStream:\n\n") ); |
cbc906ce | 821 | |
925e9792 | 822 | wxFileInputStream file_input( file_name ); |
cbc906ce | 823 | wxBufferedInputStream buf_input( file_input ); |
2b5f62a0 | 824 | |
cbc906ce | 825 | ch = buf_input.GetC(); |
925e9792 | 826 | size_t pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
827 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
828 | textCtrl.WriteText( str ); | |
2b5f62a0 | 829 | |
ab1ca7b3 | 830 | textCtrl.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") ); |
cbc906ce RR |
831 | |
832 | ch = buf_input.GetC(); | |
925e9792 | 833 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
834 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
835 | textCtrl.WriteText( str ); | |
2b5f62a0 | 836 | |
ab1ca7b3 | 837 | textCtrl.WriteText( _T("Reading yet another char from wxBufferedInputStream:\n\n") ); |
cbc906ce RR |
838 | |
839 | ch = buf_input.GetC(); | |
925e9792 | 840 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
841 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
842 | textCtrl.WriteText( str ); | |
2b5f62a0 | 843 | |
ab1ca7b3 | 844 | textCtrl.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream...\n\n") ); |
cbc906ce RR |
845 | |
846 | buf_input.Ungetch( 5 ); | |
925e9792 | 847 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
848 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); |
849 | textCtrl.WriteText( str ); | |
2b5f62a0 | 850 | |
ab1ca7b3 | 851 | textCtrl.WriteText( _T("Reading char from wxBufferedInputStream:\n\n") ); |
cbc906ce RR |
852 | |
853 | ch = buf_input.GetC(); | |
925e9792 | 854 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
855 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
856 | textCtrl.WriteText( str ); | |
2b5f62a0 | 857 | |
ab1ca7b3 | 858 | textCtrl.WriteText( _T("Reading another char from wxBufferedInputStream:\n\n") ); |
cbc906ce RR |
859 | |
860 | ch = buf_input.GetC(); | |
925e9792 | 861 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
862 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
863 | textCtrl.WriteText( str ); | |
2b5f62a0 | 864 | |
ab1ca7b3 | 865 | textCtrl.WriteText( _T("Now calling Ungetch( 5 ) from wxBufferedInputStream again...\n\n") ); |
cbc906ce RR |
866 | |
867 | buf_input.Ungetch( 5 ); | |
925e9792 | 868 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
869 | str.Printf( wxT("Now at position %d\n\n"), (int) pos ); |
870 | textCtrl.WriteText( str ); | |
2b5f62a0 | 871 | |
ab1ca7b3 | 872 | textCtrl.WriteText( _T("Seeking to pos 3 in wxBufferedInputStream. This invalidates the writeback buffer.\n\n") ); |
2b5f62a0 | 873 | |
cbc906ce RR |
874 | buf_input.SeekI( 3 ); |
875 | ||
876 | ch = buf_input.GetC(); | |
925e9792 | 877 | pos = (size_t)buf_input.TellI(); |
cbc906ce RR |
878 | str.Printf( wxT("Read char: %d. Now at position %d\n\n"), (int) ch, (int) pos ); |
879 | textCtrl.WriteText( str ); | |
880 | } | |
881 | ||
dcf924a3 RR |
882 | #if wxUSE_UNICODE |
883 | void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) | |
884 | { | |
885 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 886 | |
dcf924a3 | 887 | textCtrl.Clear(); |
ab1ca7b3 | 888 | textCtrl << _T("\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:"); |
dcf924a3 RR |
889 | |
890 | wxString str; | |
548bbe14 | 891 | str = _T("Robert R\366bling\n"); |
8e124873 | 892 | |
dcf924a3 RR |
893 | printf( "\n\nConversion with wxConvLocal:\n" ); |
894 | wxConvCurrent = &wxConvLocal; | |
895 | printf( (const char*) str.mbc_str() ); | |
dcf924a3 RR |
896 | printf( "\n\nConversion with wxConvLibc:\n" ); |
897 | wxConvCurrent = &wxConvLibc; | |
898 | printf( (const char*) str.mbc_str() ); | |
8e124873 | 899 | |
dcf924a3 RR |
900 | } |
901 | #endif | |
902 | ||
8e124873 VZ |
903 | void MyApp::DoMIMEDemo(wxCommandEvent& WXUNUSED(event)) |
904 | { | |
ab1ca7b3 | 905 | static wxString s_defaultExt = _T("xyz"); |
8e124873 | 906 | |
ab1ca7b3 MB |
907 | wxString ext = wxGetTextFromUser(_T("Enter a file extension: "), |
908 | _T("MIME database test"), | |
8e124873 VZ |
909 | s_defaultExt); |
910 | if ( !!ext ) | |
911 | { | |
912 | s_defaultExt = ext; | |
913 | ||
914 | // init MIME database if not done yet | |
915 | if ( !m_mimeDatabase ) | |
916 | { | |
917 | m_mimeDatabase = new wxMimeTypesManager; | |
918 | ||
919 | static const wxFileTypeInfo fallbacks[] = | |
920 | { | |
ab1ca7b3 MB |
921 | wxFileTypeInfo(_T("application/xyz"), |
922 | _T("XyZ %s"), | |
923 | _T("XyZ -p %s"), | |
924 | _T("The one and only XYZ format file"), | |
925 | _T("xyz"), _T("123"), NULL), | |
926 | wxFileTypeInfo(_T("text/html"), | |
927 | _T("lynx %s"), | |
928 | _T("lynx -dump %s | lpr"), | |
929 | _T("HTML document (from fallback)"), | |
930 | _T("htm"), _T("html"), NULL), | |
8e124873 VZ |
931 | |
932 | // must terminate the table with this! | |
933 | wxFileTypeInfo() | |
934 | }; | |
935 | ||
936 | m_mimeDatabase->AddFallbacks(fallbacks); | |
937 | } | |
938 | ||
939 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
940 | ||
941 | wxFileType *filetype = m_mimeDatabase->GetFileTypeFromExtension(ext); | |
942 | if ( !filetype ) | |
943 | { | |
ab1ca7b3 | 944 | textCtrl << _T("Unknown extension '") << ext << _T("'\n"); |
8e124873 VZ |
945 | } |
946 | else | |
947 | { | |
948 | wxString type, desc, open; | |
949 | filetype->GetMimeType(&type); | |
950 | filetype->GetDescription(&desc); | |
951 | ||
ab1ca7b3 MB |
952 | wxString filename = _T("filename"); |
953 | filename << _T(".") << ext; | |
8e124873 VZ |
954 | wxFileType::MessageParameters params(filename, type); |
955 | filetype->GetOpenCommand(&open, params); | |
956 | ||
437bfb36 | 957 | textCtrl << _T("MIME information about extension '") << ext << _T('\n') |
ab1ca7b3 | 958 | << _T("\tMIME type: ") << ( !type ? wxT("unknown") |
437bfb36 | 959 | : type.c_str() ) << _T('\n') |
206d3a16 | 960 | << _T("\tDescription: ") << ( !desc ? wxEmptyString : desc.c_str() ) |
437bfb36 | 961 | << _T('\n') |
ab1ca7b3 | 962 | << _T("\tCommand to open: ") << ( !open ? wxT("no") : open.c_str() ) |
437bfb36 | 963 | << _T('\n'); |
8e124873 VZ |
964 | |
965 | delete filetype; | |
966 | } | |
967 | } | |
968 | //else: cancelled by user | |
969 | } | |
970 | ||
7e2c43b8 RR |
971 | void MyApp::DoByteOrderDemo(wxCommandEvent& WXUNUSED(event)) |
972 | { | |
973 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
974 | ||
975 | textCtrl.Clear(); | |
ab1ca7b3 | 976 | textCtrl << _T("\nTest byte order macros:\n\n"); |
8e124873 | 977 | |
dacaa6f1 | 978 | #if wxBYTE_ORDER == wxLITTLE_ENDIAN |
ab1ca7b3 | 979 | textCtrl << _T("This is a little endian system.\n\n"); |
dacaa6f1 | 980 | #else |
ab1ca7b3 | 981 | textCtrl << _T("This is a big endian system.\n\n"); |
dacaa6f1 | 982 | #endif |
8e124873 | 983 | |
7e2c43b8 | 984 | wxString text; |
8e124873 | 985 | |
7e2c43b8 | 986 | wxInt32 var = 0xF1F2F3F4; |
206d3a16 | 987 | text = wxEmptyString; |
dcf924a3 | 988 | text.Printf( _T("Value of wxInt32 is now: %#x.\n\n"), var ); |
7e2c43b8 | 989 | textCtrl.WriteText( text ); |
8e124873 | 990 | |
206d3a16 | 991 | text = wxEmptyString; |
dcf924a3 | 992 | text.Printf( _T("Value of swapped wxInt32 is: %#x.\n\n"), wxINT32_SWAP_ALWAYS( var ) ); |
7e2c43b8 | 993 | textCtrl.WriteText( text ); |
8e124873 | 994 | |
206d3a16 | 995 | text = wxEmptyString; |
dcf924a3 | 996 | text.Printf( _T("Value of wxInt32 swapped on little endian is: %#x.\n\n"), wxINT32_SWAP_ON_LE( var ) ); |
7e2c43b8 | 997 | textCtrl.WriteText( text ); |
8e124873 | 998 | |
206d3a16 | 999 | text = wxEmptyString; |
dcf924a3 | 1000 | text.Printf( _T("Value of wxInt32 swapped on big endian is: %#x.\n\n"), wxINT32_SWAP_ON_BE( var ) ); |
7e2c43b8 RR |
1001 | textCtrl.WriteText( text ); |
1002 | } | |
1003 | ||
bbf73190 | 1004 | void MyApp::DoVariantDemo(wxCommandEvent& WXUNUSED(event) ) |
cfb88c55 JS |
1005 | { |
1006 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
1007 | ||
ab1ca7b3 MB |
1008 | wxVariant var1 = _T("String value"); |
1009 | textCtrl << _T("var1 = ") << var1.MakeString() << _T("\n"); | |
cfb88c55 | 1010 | |
62448488 JS |
1011 | // Conversion |
1012 | wxString str = var1.MakeString(); | |
cfb88c55 JS |
1013 | |
1014 | var1 = 123.456; | |
ab1ca7b3 | 1015 | textCtrl << _T("var1 = ") << var1.GetReal() << _T("\n"); |
cfb88c55 JS |
1016 | |
1017 | // Implicit conversion | |
1018 | double v = var1; | |
1019 | ||
1020 | var1 = 9876L; | |
ab1ca7b3 | 1021 | textCtrl << _T("var1 = ") << var1.GetLong() << _T("\n"); |
cfb88c55 JS |
1022 | |
1023 | // Implicit conversion | |
1024 | long l = var1; | |
1025 | ||
8e124873 | 1026 | // suppress compile warnings about unused variables |
206d3a16 JS |
1027 | wxUnusedVar(l); |
1028 | wxUnusedVar(v); | |
8e124873 | 1029 | |
21fe01e2 WS |
1030 | wxArrayString stringArray; |
1031 | stringArray.Add(_T("one")); stringArray.Add(_T("two")); stringArray.Add(_T("three")); | |
1032 | var1 = stringArray; | |
ab1ca7b3 | 1033 | textCtrl << _T("var1 = ") << var1.MakeString() << _T("\n"); |
cfb88c55 JS |
1034 | |
1035 | var1.ClearList(); | |
1036 | var1.Append(wxVariant(1.2345)); | |
ab1ca7b3 | 1037 | var1.Append(wxVariant(_T("hello"))); |
cfb88c55 JS |
1038 | var1.Append(wxVariant(54321L)); |
1039 | ||
ab1ca7b3 | 1040 | textCtrl << _T("var1 = ") << var1.MakeString() << _T("\n"); |
cfb88c55 JS |
1041 | |
1042 | size_t n = var1.GetCount(); | |
1043 | size_t i; | |
1044 | for (i = (size_t) 0; i < n; i++) | |
1045 | { | |
ab1ca7b3 | 1046 | textCtrl << _T("var1[") << (int) i << _T("] (type ") << var1[i].GetType() << _T(") = ") << var1[i].MakeString() << _T("\n"); |
cfb88c55 | 1047 | } |
cf6ae290 RG |
1048 | |
1049 | var1 = wxVariant(new wxFont(wxSystemSettings::GetFont(wxSYS_OEM_FIXED_FONT))); | |
1050 | textCtrl << _T("var1 = (wxfont)\""); | |
1051 | wxFont* font = wxGetVariantCast(var1,wxFont); | |
206d3a16 JS |
1052 | if (font) |
1053 | { | |
1054 | textCtrl << font->GetNativeFontInfoDesc() << _T("\"\n"); | |
1055 | } | |
1056 | else | |
1057 | { | |
1058 | textCtrl << _T("(null)\"\n"); | |
cf6ae290 | 1059 | } |
cfb88c55 JS |
1060 | } |
1061 | ||
1062 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
8e124873 VZ |
1063 | EVT_MENU(TYPES_QUIT, MyFrame::OnQuit) |
1064 | EVT_MENU(TYPES_ABOUT, MyFrame::OnAbout) | |
cfb88c55 JS |
1065 | END_EVENT_TABLE() |
1066 | ||
1067 | // My frame constructor | |
1068 | MyFrame::MyFrame(wxFrame *parent, const wxString& title, | |
206d3a16 JS |
1069 | const wxPoint& pos, const wxSize& size) |
1070 | : wxFrame(parent, wxID_ANY, title, pos, size) | |
cfb88c55 JS |
1071 | {} |
1072 | ||
1073 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
1074 | { | |
206d3a16 | 1075 | Close(true); |
cfb88c55 JS |
1076 | } |
1077 | ||
1078 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
1079 | { | |
be5a51fb | 1080 | wxMessageDialog dialog(this, _T("Tests various wxWidgets types"), |
206d3a16 | 1081 | _T("About Types"), wxYES_NO|wxCANCEL); |
cfb88c55 | 1082 | |
206d3a16 | 1083 | dialog.ShowModal(); |
cfb88c55 JS |
1084 | } |
1085 | ||
1086 |