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