]> git.saurik.com Git - wxWidgets.git/blame_incremental - samples/typetest/typetest.cpp
If we used 10% less inline functions in useless occasions,
[wxWidgets.git] / samples / typetest / typetest.cpp
... / ...
CommitLineData
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#include "wx/mimetype.h"
31
32#include "typetest.h"
33
34#if defined(__WXGTK__) || defined(__WXMOTIF__)
35#include "mondrian.xpm"
36#endif
37
38#include "wx/ioswrap.h"
39
40#if wxUSE_IOSTREAMH
41 #include <fstream.h>
42#else
43 #include <fstream>
44#endif
45
46#include "wx/wfstream.h"
47#include "wx/datstrm.h"
48#include "wx/txtstrm.h"
49
50// Create a new application object
51IMPLEMENT_APP (MyApp)
52
53IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp)
54
55BEGIN_EVENT_TABLE(MyApp, wxApp)
56 EVT_MENU(TYPES_DATE, MyApp::DoDateDemo)
57 EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo)
58 EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo)
59 EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo)
60#if wxUSE_UNICODE
61 EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo)
62#endif
63 EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo)
64 EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo)
65END_EVENT_TABLE()
66
67bool MyApp::OnInit()
68{
69 // Create the main frame window
70 MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo",
71 wxPoint(50, 50), wxSize(450, 340));
72
73 // Give it an icon
74 frame->SetIcon(wxICON(mondrian));
75
76 // Make a menubar
77 wxMenu *file_menu = new wxMenu;
78
79 file_menu->Append(TYPES_ABOUT, "&About");
80 file_menu->AppendSeparator();
81 file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X");
82
83 wxMenu *test_menu = new wxMenu;
84 test_menu->Append(TYPES_DATE, "&Date test");
85 test_menu->Append(TYPES_TIME, "&Time test");
86 test_menu->Append(TYPES_VARIANT, "&Variant test");
87 test_menu->Append(TYPES_BYTEORDER, "&Byteorder test");
88#if wxUSE_UNICODE
89 test_menu->Append(TYPES_UNICODE, "&Unicode test");
90#endif
91 test_menu->Append(TYPES_STREAM, "&Stream test");
92 test_menu->AppendSeparator();
93 test_menu->Append(TYPES_MIME, "&MIME database test");
94
95 wxMenuBar *menu_bar = new wxMenuBar;
96 menu_bar->Append(file_menu, "&File");
97 menu_bar->Append(test_menu, "&Tests");
98 frame->SetMenuBar(menu_bar);
99
100 m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE);
101
102 // Show the frame
103 frame->Show(TRUE);
104
105 SetTopWindow(frame);
106
107 return TRUE;
108}
109
110void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event))
111{
112 wxTextCtrl& textCtrl = * GetTextCtrl();
113
114 textCtrl.Clear();
115 textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n");
116
117 textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" );
118
119 ofstream std_file_output( "test_std.dat" );
120 wxFileOutputStream file_output( "test_wx.dat" );
121 wxBufferedOutputStream buf_output( file_output );
122 wxTextOutputStream text_output( buf_output );
123
124 wxString tmp;
125 signed int si = 0xFFFFFFFF;
126 tmp.Printf( _T("Signed int: %d\n"), si );
127 textCtrl.WriteText( tmp );
128 text_output << si << "\n";
129 std_file_output << si << "\n";
130
131 unsigned int ui = 0xFFFFFFFF;
132 tmp.Printf( _T("Unsigned int: %u\n"), ui );
133 textCtrl.WriteText( tmp );
134 text_output << ui << "\n";
135 std_file_output << ui << "\n";
136
137 double d = 2.01234567890123456789;
138 tmp.Printf( _T("Double: %f\n"), d );
139 textCtrl.WriteText( tmp );
140 text_output << d << "\n";
141 std_file_output << d << "\n";
142
143 float f = (float)0.00001;
144 tmp.Printf( _T("Float: %f\n"), f );
145 textCtrl.WriteText( tmp );
146 text_output << f << "\n";
147 std_file_output << f << "\n";
148
149 wxString str( _T("Hello!") );
150 tmp.Printf( _T("String: %s\n"), str.c_str() );
151 textCtrl.WriteText( tmp );
152 text_output << str << "\n";
153 std_file_output << str.c_str() << "\n";
154
155 textCtrl.WriteText( "\nReading from ifstream:\n" );
156
157 ifstream std_file_input( "test_std.dat" );
158
159 std_file_input >> si;
160 tmp.Printf( _T("Signed int: %d\n"), si );
161 textCtrl.WriteText( tmp );
162
163 std_file_input >> ui;
164 tmp.Printf( _T("Unsigned int: %u\n"), ui );
165 textCtrl.WriteText( tmp );
166
167 std_file_input >> d;
168 tmp.Printf( _T("Double: %f\n"), d );
169 textCtrl.WriteText( tmp );
170
171 std_file_input >> f;
172 tmp.Printf( _T("Float: %f\n"), f );
173 textCtrl.WriteText( tmp );
174
175 std_file_input >> str;
176 tmp.Printf( _T("String: %s\n"), str.c_str() );
177 textCtrl.WriteText( tmp );
178
179 textCtrl.WriteText( "\nReading from wxFileInputStream:\n" );
180
181 buf_output.Sync();
182
183 wxFileInputStream file_input( "test_wx.dat" );
184 wxBufferedInputStream buf_input( file_input );
185 wxTextInputStream text_input( buf_input );
186
187 text_input >> si;
188 tmp.Printf( _T("Signed int: %d\n"), si );
189 textCtrl.WriteText( tmp );
190
191 text_input >> ui;
192 tmp.Printf( _T("Unsigned int: %u\n"), ui );
193 textCtrl.WriteText( tmp );
194
195 text_input >> d;
196 tmp.Printf( _T("Double: %f\n"), d );
197 textCtrl.WriteText( tmp );
198
199 text_input >> f;
200 tmp.Printf( _T("Float: %f\n"), f );
201 textCtrl.WriteText( tmp );
202
203 text_input >> str;
204 tmp.Printf( _T("String: %s\n"), str.c_str() );
205 textCtrl.WriteText( tmp );
206
207
208 textCtrl << "\nTest for wxDataStream:\n\n";
209
210 textCtrl.WriteText( "Writing to wxDataOutputStream:\n" );
211
212 file_output.SeekO( 0 );
213 wxDataOutputStream data_output( buf_output );
214
215 wxInt16 i16 = (short)0xFFFF;
216 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
217 textCtrl.WriteText( tmp );
218 data_output.Write16( i16 );
219
220 wxUint16 ui16 = 0xFFFF;
221 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
222 textCtrl.WriteText( tmp );
223 data_output.Write16( ui16 );
224
225 d = 2.01234567890123456789;
226 tmp.Printf( _T("Double: %f\n"), d );
227 textCtrl.WriteText( tmp );
228 data_output.WriteDouble( d );
229
230 str = "Hello!";
231 tmp.Printf( _T("String: %s\n"), str.c_str() );
232 textCtrl.WriteText( tmp );
233 data_output.WriteString( str );
234
235 buf_output.Sync();
236
237 textCtrl.WriteText( "\nReading from wxDataInputStream:\n" );
238
239 file_input.SeekI( 0 );
240 wxDataInputStream data_input( buf_input );
241
242 i16 = data_input.Read16();
243 tmp.Printf( _T("Signed int16: %d\n"), (int)i16 );
244 textCtrl.WriteText( tmp );
245
246 ui16 = data_input.Read16();
247 tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 );
248 textCtrl.WriteText( tmp );
249
250 d = data_input.ReadDouble();
251 tmp.Printf( _T("Double: %f\n"), d );
252 textCtrl.WriteText( tmp );
253
254 str = data_input.ReadString();
255 tmp.Printf( _T("String: %s\n"), str.c_str() );
256 textCtrl.WriteText( tmp );
257}
258
259#if wxUSE_UNICODE
260void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event))
261{
262 wxTextCtrl& textCtrl = * GetTextCtrl();
263
264 textCtrl.Clear();
265 textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:";
266
267 wxString str;
268