]> git.saurik.com Git - wxWidgets.git/blame - samples/dialup/nettest.cpp
fixing reentrancy which happened in tests, bringing client coordinates origins in...
[wxWidgets.git] / samples / dialup / nettest.cpp
CommitLineData
09884325
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: net.cpp
be5a51fb 3// Purpose: wxWidgets sample demonstrating network-related functions
09884325
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 07.07.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
09884325
VZ
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27// for all others, include the necessary headers (this file is usually all you
be5a51fb 28// need because it includes almost all "standard" wxWidgets headers
09884325
VZ
29#ifndef WX_PRECOMP
30 #include "wx/wx.h"
31#endif
32
9832ce0b
GD
33#if !wxUSE_DIALUP_MANAGER
34#error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h!
35#endif
36
e90c1d2a 37#include "wx/dialup.h"
09884325 38
e7092398 39#ifndef wxHAS_IMAGES_IN_RESOURCES
41f02b9a
FM
40 #include "../sample.xpm"
41#endif
42
09884325
VZ
43// ----------------------------------------------------------------------------
44// private classes
45// ----------------------------------------------------------------------------
46
47// Define a new application type, each program should derive a class from wxApp
48class MyApp : public wxApp
49{
50public:
51 // override base class virtuals
52 // ----------------------------
53
54 // this one is called on application startup and is a good place for the app
55 // initialization (doing it here and not in the ctor allows to have an error
56 // return: if OnInit() returns false, the application terminates)
57 virtual bool OnInit();
58
24df4c19
VZ
59 // called before the application termination
60 virtual int OnExit();
61
09884325
VZ
62 // event handlers
63 void OnConnected(wxDialUpEvent& event);
64
24df4c19
VZ
65 // accessor to dial up manager
66 wxDialUpManager *GetDialer() const { return m_dial; }
67
09884325 68private:
24df4c19
VZ
69 wxDialUpManager *m_dial;
70
8caa4ed1 71 DECLARE_EVENT_TABLE()
09884325
VZ
72};
73
74// Define a new frame type: this is going to be our main frame
75class MyFrame : public wxFrame
76{
77public:
78 // ctor(s)
79 MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
80
81 // event handlers (these functions should _not_ be virtual)
82 void OnQuit(wxCommandEvent& event);
83 void OnAbout(wxCommandEvent& event);
84 void OnHangUp(wxCommandEvent& event);
85 void OnDial(wxCommandEvent& event);
2690830e 86 void OnEnumISPs(wxCommandEvent& event);
24e1f1ca 87 void OnCheck(wxCommandEvent& event);
24df4c19
VZ
88 void OnUpdateUI(wxUpdateUIEvent& event);
89
09884325
VZ
90 void OnIdle(wxIdleEvent& event);
91
92private:
be5a51fb 93 // any class wishing to process wxWidgets events must use this macro
09884325
VZ
94 DECLARE_EVENT_TABLE()
95};
96
97// ----------------------------------------------------------------------------
98// constants
99// ----------------------------------------------------------------------------
100
101// IDs for the controls and the menu commands
102enum
103{
104 // menu items
105 NetTest_Quit = 1,
106 NetTest_About,
107 NetTest_HangUp,
2690830e
VZ
108 NetTest_Dial,
109 NetTest_EnumISP,
24e1f1ca 110 NetTest_Check,
2690830e 111 NetTest_Max
09884325
VZ
112};
113
114// ----------------------------------------------------------------------------
be5a51fb 115// event tables and other macros for wxWidgets
09884325
VZ
116// ----------------------------------------------------------------------------
117
118BEGIN_EVENT_TABLE(MyApp, wxApp)
119 EVT_DIALUP_CONNECTED(MyApp::OnConnected)
120 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected)
121END_EVENT_TABLE()
122
be5a51fb 123// the event tables connect the wxWidgets events with the functions (event
09884325
VZ
124// handlers) which process them. It can be also done at run-time, but for the
125// simple menu events like this the static method is much simpler.
126BEGIN_EVENT_TABLE(MyFrame, wxFrame)
127 EVT_MENU(NetTest_Quit, MyFrame::OnQuit)
128 EVT_MENU(NetTest_About, MyFrame::OnAbout)
129 EVT_MENU(NetTest_HangUp, MyFrame::OnHangUp)
130 EVT_MENU(NetTest_Dial, MyFrame::OnDial)
2690830e 131 EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs)
24e1f1ca 132 EVT_MENU(NetTest_Check, MyFrame::OnCheck)
09884325 133
24df4c19
VZ
134 EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI)
135
09884325
VZ
136 EVT_IDLE(MyFrame::OnIdle)
137END_EVENT_TABLE()
138
be5a51fb 139// Create a new application object: this macro will allow wxWidgets to create
09884325
VZ
140// the application object during program execution (it's better than using a
141// static object for many reasons) and also declares the accessor function
142// wxGetApp() which will return the reference of the right type (i.e. MyApp and
143// not wxApp)
144IMPLEMENT_APP(MyApp)
145
146// ============================================================================
147// implementation
148// ============================================================================
149
150// ----------------------------------------------------------------------------
151// the application class
152// ----------------------------------------------------------------------------
153
154// `Main program' equivalent: the program execution "starts" here
155bool MyApp::OnInit()
156{
45e6e6f8
VZ
157 if ( !wxApp::OnInit() )
158 return false;
159
09884325 160 // Create the main application window
9a83f860 161 MyFrame *frame = new MyFrame(wxT("Dial-up wxWidgets demo"),
09884325
VZ
162 wxPoint(50, 50), wxSize(450, 340));
163
18f42b94 164 // Show it
f2aea0d1 165 frame->Show(true);
09884325 166
24df4c19
VZ
167 // Init dial up manager
168 m_dial = wxDialUpManager::Create();
169
170 if ( !m_dial->IsOk() )
171 {
4693b20c 172 wxLogError(wxT("The sample can't run on this system."));
24df4c19 173
f07941fc 174#if wxUSE_LOG
24df4c19 175 wxLog::GetActiveTarget()->Flush();
f07941fc 176#endif // wxUSE_LOG
24df4c19
VZ
177
178 // do it here, OnExit() won't be called
179 delete m_dial;
180
f2aea0d1 181 return false;
24df4c19
VZ
182 }
183
8520f137 184#if wxUSE_STATUSBAR
9a83f860 185 frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? wxT("LAN") : wxT("No LAN"), 2);
8520f137 186#endif // wxUSE_STATUSBAR
2690830e 187
f2aea0d1 188 return true;
09884325
VZ
189}
190
24df4c19
VZ
191int MyApp::OnExit()
192{
193 delete m_dial;
194
195 // exit code is 0, everything is ok
196 return 0;
197}
198
09884325
VZ
199void MyApp::OnConnected(wxDialUpEvent& event)
200{
4693b20c 201 const wxChar *msg;
24df4c19
VZ
202 if ( event.IsOwnEvent() )
203 {
4693b20c
MB
204 msg = event.IsConnectedEvent() ? wxT("Successfully connected")
205 : wxT("Dialing failed");
24df4c19 206
aec18ff7 207 wxLogStatus(wxEmptyString);
24df4c19
VZ
208 }
209 else
210 {
4693b20c
MB
211 msg = event.IsConnectedEvent() ? wxT("Just connected!")
212 : wxT("Disconnected");
24df4c19
VZ
213 }
214
2690830e 215 wxLogMessage(msg);
09884325
VZ
216}
217
218// ----------------------------------------------------------------------------
219// main frame
220// ----------------------------------------------------------------------------
221
222// frame constructor
223MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
f2aea0d1 224 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
09884325 225{
41f02b9a
FM
226 SetIcon(wxICON(sample));
227
09884325
VZ
228 // create a menu bar
229 wxMenu *menuFile = new wxMenu;
230
9a83f860
VZ
231 menuFile->Append(NetTest_Dial, wxT("&Dial\tCtrl-D"), wxT("Dial default ISP"));
232 menuFile->Append(NetTest_HangUp, wxT("&HangUp\tCtrl-H"), wxT("Hang up modem"));
09884325 233 menuFile->AppendSeparator();
9a83f860
VZ
234 menuFile->Append(NetTest_EnumISP, wxT("&Enumerate ISPs...\tCtrl-E"));
235 menuFile->Append(NetTest_Check, wxT("&Check connection status...\tCtrl-C"));
2690830e 236 menuFile->AppendSeparator();
2d143b66 237 menuFile->Append(NetTest_About, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
09884325 238 menuFile->AppendSeparator();
9a83f860 239 menuFile->Append(NetTest_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
09884325
VZ
240
241 // now append the freshly created menu to the menu bar...
242 wxMenuBar *menuBar = new wxMenuBar;
9a83f860 243 menuBar->Append(menuFile, wxT("&File"));
09884325
VZ
244
245 // ... and attach this menu bar to the frame
246 SetMenuBar(menuBar);
247
8520f137 248#if wxUSE_STATUSBAR
2690830e
VZ
249 // create status bar and fill the LAN field
250 CreateStatusBar(3);
251 static const int widths[3] = { -1, 100, 60 };
252 SetStatusWidths(3, widths);
8520f137 253#endif // wxUSE_STATUSBAR
09884325
VZ
254}
255
256
257// event handlers
258
259void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
260{
f2aea0d1
WS
261 // true is to force the frame to close
262 Close(true);
09884325
VZ
263}
264
265void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
266{
267 wxString msg;
f565a6c2 268 msg.Printf( wxT("This is the network functions test sample.\n")
749bfe9a 269 wxT("(c) 1999 Vadim Zeitlin") );
09884325 270
f565a6c2 271 wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this);
09884325
VZ
272}
273
274void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event))
275{
24df4c19 276 if ( wxGetApp().GetDialer()->HangUp() )
09884325 277 {
3103e8a9 278 wxLogStatus(this, wxT("Connection was successfully terminated."));
09884325
VZ
279 }
280 else
281 {
4693b20c 282 wxLogStatus(this, wxT("Failed to hang up."));
09884325
VZ
283 }
284}
285
286void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event))
287{
4693b20c 288 wxLogStatus(this, wxT("Preparing to dial..."));
09884325
VZ
289 wxYield();
290 wxBeginBusyCursor();
291
2690830e 292 if ( wxGetApp().GetDialer()->Dial() )
09884325 293 {
4693b20c 294 wxLogStatus(this, wxT("Dialing..."));
09884325
VZ
295 }
296 else
297 {
4693b20c 298 wxLogStatus(this, wxT("Dialing attempt failed."));
09884325
VZ
299 }
300
301 wxEndBusyCursor();
302}
303
24e1f1ca
KB
304void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event))
305{
306 if(wxGetApp().GetDialer()->IsOnline())
43b2d5e7 307 {
4693b20c 308 wxLogMessage(wxT("Network is online."));
43b2d5e7 309 }
24e1f1ca 310 else
43b2d5e7 311 {
4693b20c 312 wxLogMessage(wxT("Network is offline."));
43b2d5e7 313 }
24e1f1ca
KB
314}
315
2690830e
VZ
316void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event))
317{
318 wxArrayString names;
319 size_t nCount = wxGetApp().GetDialer()->GetISPNames(names);
320 if ( nCount == 0 )
321 {
4693b20c 322 wxLogWarning(wxT("No ISPs found."));
2690830e
VZ
323 }
324 else
325 {
9a83f860 326 wxString msg = wxT("Known ISPs:\n");
2690830e
VZ
327 for ( size_t n = 0; n < nCount; n++ )
328 {
329 msg << names[n] << '\n';
330 }
331
332 wxLogMessage(msg);
333 }
334}
335
24df4c19
VZ
336void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
337{
338 // disable this item while dialing
339 event.Enable( !wxGetApp().GetDialer()->IsDialing() );
340}
341
09884325
VZ
342void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
343{
f2aea0d1 344 static int s_isOnline = -1; // not true nor false
09884325 345
24df4c19 346 bool isOnline = wxGetApp().GetDialer()->IsOnline();
09884325
VZ
347 if ( s_isOnline != (int)isOnline )
348 {
349 s_isOnline = isOnline;
350
8520f137 351#if wxUSE_STATUSBAR
9a83f860 352 SetStatusText(isOnline ? wxT("Online") : wxT("Offline"), 1);
8520f137 353#endif // wxUSE_STATUSBAR
09884325
VZ
354 }
355}