]>
Commit | Line | Data |
---|---|---|
cfb88c55 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: typetest.cpp | |
3 | // Purpose: Types wxWindows sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "typetest.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/time.h" | |
28 | #include "wx/date.h" | |
29 | #include "wx/variant.h" | |
30 | ||
31 | #include "typetest.h" | |
32 | ||
b412f9be | 33 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
cfb88c55 JS |
34 | #include "mondrian.xpm" |
35 | #endif | |
36 | ||
38830220 RR |
37 | #include "wx/ioswrap.h" |
38 | ||
39 | #if wxUSE_IOSTREAMH | |
40 | #include <fstream.h> | |
41 | #else | |
42 | #include <fstream> | |
43 | #endif | |
44 | ||
45 | #include "wx/wfstream.h" | |
53daeada | 46 | #include "wx/datstrm.h" |
fae05df5 | 47 | #include "wx/txtstrm.h" |
38830220 | 48 | |
cfb88c55 JS |
49 | // Create a new application object |
50 | IMPLEMENT_APP (MyApp) | |
51 | ||
52 | IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp) | |
53 | ||
54 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
dcf924a3 RR |
55 | EVT_MENU(TYPES_DATE, MyApp::DoDateDemo) |
56 | EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo) | |
57 | EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo) | |
7e2c43b8 | 58 | EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo) |
dcf924a3 RR |
59 | #if wxUSE_UNICODE |
60 | EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) | |
61 | #endif | |
38830220 | 62 | EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) |
cfb88c55 JS |
63 | END_EVENT_TABLE() |
64 | ||
65 | bool MyApp::OnInit(void) | |
66 | { | |
67 | // Create the main frame window | |
68 | MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo", | |
69 | wxPoint(50, 50), wxSize(450, 340)); | |
70 | ||
71 | // Give it an icon | |
72 | frame->SetIcon(wxICON(mondrian)); | |
73 | ||
74 | // Make a menubar | |
75 | wxMenu *file_menu = new wxMenu; | |
76 | ||
77 | file_menu->Append(TYPES_ABOUT, "&About"); | |
78 | file_menu->AppendSeparator(); | |
79 | file_menu->Append(TYPES_DATE, "&Date test"); | |
80 | file_menu->Append(TYPES_TIME, "&Time test"); | |
81 | file_menu->Append(TYPES_VARIANT, "&Variant test"); | |
7e2c43b8 | 82 | file_menu->Append(TYPES_BYTEORDER, "&Byteorder test"); |
dcf924a3 RR |
83 | #if wxUSE_UNICODE |
84 | file_menu->Append(TYPES_UNICODE, "&Unicode test"); | |
85 | #endif | |
38830220 | 86 | file_menu->Append(TYPES_STREAM, "&Stream test"); |
cfb88c55 JS |
87 | file_menu->AppendSeparator(); |
88 | file_menu->Append(TYPES_QUIT, "E&xit"); | |
89 | wxMenuBar *menu_bar = new wxMenuBar; | |
90 | menu_bar->Append(file_menu, "&File"); | |
91 | frame->SetMenuBar(menu_bar); | |
92 | ||
93 | m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE); | |
94 | ||
95 | // Show the frame | |
96 | frame->Show(TRUE); | |
97 | ||
98 | SetTopWindow(frame); | |
99 | ||
100 | return TRUE; | |
101 | } | |
102 | ||
38830220 RR |
103 | void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) |
104 | { | |
105 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
106 | ||
107 | textCtrl.Clear(); | |
c980c992 | 108 | textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n"); |
38830220 | 109 | |
e57e26dd RR |
110 | textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" ); |
111 | ||
38830220 RR |
112 | ofstream std_file_output( "test_std.dat" ); |
113 | wxFileOutputStream file_output( "test_wx.dat" ); | |
fae05df5 GL |
114 | wxBufferedOutputStream buf_output( file_output ); |
115 | wxTextOutputStream text_output( buf_output ); | |
38830220 | 116 | |
38830220 RR |
117 | wxString tmp; |
118 | signed int si = 0xFFFFFFFF; | |
c980c992 | 119 | tmp.Printf( _T("Signed int: %d\n"), si ); |
38830220 | 120 | textCtrl.WriteText( tmp ); |
fae05df5 | 121 | text_output << si << "\n"; |
38830220 RR |
122 | std_file_output << si << "\n"; |
123 | ||
124 | unsigned int ui = 0xFFFFFFFF; | |
c980c992 | 125 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
38830220 | 126 | textCtrl.WriteText( tmp ); |
fae05df5 | 127 | text_output << ui << "\n"; |
38830220 RR |
128 | std_file_output << ui << "\n"; |
129 | ||
130 | double d = 2.01234567890123456789; | |
c980c992 | 131 | tmp.Printf( _T("Double: %f\n"), d ); |
38830220 | 132 | textCtrl.WriteText( tmp ); |
fae05df5 | 133 | text_output << d << "\n"; |
38830220 RR |
134 | std_file_output << d << "\n"; |
135 | ||
e57e26dd | 136 | float f = 0.00001; |
c980c992 | 137 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 138 | textCtrl.WriteText( tmp ); |
fae05df5 | 139 | text_output << f << "\n"; |
e57e26dd RR |
140 | std_file_output << f << "\n"; |
141 | ||
c980c992 GL |
142 | wxString str( _T("Hello!") ); |
143 | tmp.Printf( _T("String: %s\n"), str.c_str() ); | |
38830220 | 144 | textCtrl.WriteText( tmp ); |
fae05df5 | 145 | text_output << str << "\n"; |
38830220 RR |
146 | std_file_output << str.c_str() << "\n"; |
147 | ||
e57e26dd RR |
148 | textCtrl.WriteText( "\nReading from ifstream:\n" ); |
149 | ||
150 | ifstream std_file_input( "test_std.dat" ); | |
151 | ||
152 | std_file_input >> si; | |
c980c992 | 153 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd RR |
154 | textCtrl.WriteText( tmp ); |
155 | ||
156 | std_file_input >> ui; | |
c980c992 | 157 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd RR |
158 | textCtrl.WriteText( tmp ); |
159 | ||
160 | std_file_input >> d; | |
c980c992 | 161 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd RR |
162 | textCtrl.WriteText( tmp ); |
163 | ||
164 | std_file_input >> f; | |
c980c992 | 165 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd RR |
166 | textCtrl.WriteText( tmp ); |
167 | ||
168 | std_file_input >> str; | |
c980c992 | 169 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd RR |
170 | textCtrl.WriteText( tmp ); |
171 | ||
172 | textCtrl.WriteText( "\nReading from wxFileInputStream:\n" ); | |
b6bff301 | 173 | |
fae05df5 | 174 | buf_output.Sync(); |
e57e26dd RR |
175 | |
176 | wxFileInputStream file_input( "test_wx.dat" ); | |
fae05df5 GL |
177 | wxBufferedInputStream buf_input( file_input ); |
178 | wxTextInputStream text_input( buf_input ); | |
e57e26dd | 179 | |
fae05df5 | 180 | text_input >> si; |
c980c992 | 181 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd RR |
182 | textCtrl.WriteText( tmp ); |
183 | ||
fae05df5 | 184 | text_input >> ui; |
c980c992 | 185 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd RR |
186 | textCtrl.WriteText( tmp ); |
187 | ||
fae05df5 | 188 | text_input >> d; |
c980c992 | 189 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd RR |
190 | textCtrl.WriteText( tmp ); |
191 | ||
fae05df5 | 192 | text_input >> f; |
c980c992 | 193 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd RR |
194 | textCtrl.WriteText( tmp ); |
195 | ||
fae05df5 | 196 | text_input >> str; |
c980c992 | 197 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 198 | textCtrl.WriteText( tmp ); |
53daeada RR |
199 | |
200 | ||
201 | textCtrl << "\nTest for wxDataStream:\n\n"; | |
202 | ||
203 | textCtrl.WriteText( "Writing to wxDataOutputStream:\n" ); | |
204 | ||
205 | file_output.SeekO( 0 ); | |
fae05df5 | 206 | wxDataOutputStream data_output( buf_output ); |
53daeada | 207 | |
329e86bf | 208 | wxInt16 i16 = 0xFFFF; |
c980c992 | 209 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 210 | textCtrl.WriteText( tmp ); |
329e86bf | 211 | data_output.Write16( i16 ); |
53daeada | 212 | |
329e86bf | 213 | wxUint16 ui16 = 0xFFFF; |
c980c992 | 214 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada | 215 | textCtrl.WriteText( tmp ); |
329e86bf | 216 | data_output.Write16( ui16 ); |
53daeada RR |
217 | |
218 | d = 2.01234567890123456789; | |
c980c992 | 219 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada RR |
220 | textCtrl.WriteText( tmp ); |
221 | data_output.WriteDouble( d ); | |
222 | ||
223 | str = "Hello!"; | |
c980c992 | 224 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada RR |
225 | textCtrl.WriteText( tmp ); |
226 | data_output.WriteString( str ); | |
227 | ||
fae05df5 | 228 | buf_output.Sync(); |
53daeada RR |
229 | |
230 | textCtrl.WriteText( "\nReading from wxDataInputStream:\n" ); | |
231 | ||
232 | file_input.SeekI( 0 ); | |
fae05df5 | 233 | wxDataInputStream data_input( buf_input ); |
53daeada | 234 | |
329e86bf | 235 | i16 = data_input.Read16(); |
c980c992 | 236 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada RR |
237 | textCtrl.WriteText( tmp ); |
238 | ||
329e86bf | 239 | ui16 = data_input.Read16(); |
c980c992 | 240 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada RR |
241 | textCtrl.WriteText( tmp ); |
242 | ||
243 | d = data_input.ReadDouble(); | |
c980c992 | 244 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada RR |
245 | textCtrl.WriteText( tmp ); |
246 | ||
247 | str = data_input.ReadString(); | |
c980c992 | 248 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada | 249 | textCtrl.WriteText( tmp ); |
38830220 RR |
250 | } |
251 | ||
dcf924a3 RR |
252 | #if wxUSE_UNICODE |
253 | void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) | |
254 | { | |
255 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
256 | ||
257 | textCtrl.Clear(); | |
258 | textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:"; | |
259 | ||
260 | wxString str; | |
261 |