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