1///////////////////////////////////////////////////////////////////////////// 2// Name: net.cpp 3// Purpose: wxWidgets sample demonstrating network-related functions 4// Author: Vadim Zeitlin 5// Modified by: 6// Created: 07.07.99 7// RCS-ID: $Id: nettest.cpp 35650 2005-09-23 12:56:45Z MR $ 8// Copyright: (c) Vadim Zeitlin 9// Licence: wxWindows licence 10///////////////////////////////////////////////////////////////////////////// 11 12// ============================================================================ 13// declarations 14// ============================================================================ 15 16// ---------------------------------------------------------------------------- 17// headers 18// ---------------------------------------------------------------------------- 19 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 28// need because it includes almost all "standard" wxWidgets headers 29#ifndef WX_PRECOMP 30 #include "wx/wx.h" 31#endif 32 33#if !wxUSE_DIALUP_MANAGER 34#error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h! 35#endif 36 37#include "wx/dialup.h" 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 55 // called before the application termination 56 virtual int OnExit(); 57 58 // event handlers 59 void OnConnected(wxDialUpEvent& event); 60 61 // accessor to dial up manager 62 wxDialUpManager *GetDialer() const { return m_dial; } 63 64private: 65 wxDialUpManager *m_dial; 66 67 DECLARE_EVENT_TABLE() 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); 82 void OnEnumISPs(wxCommandEvent& event); 83 void OnCheck(wxCommandEvent& event); 84 void OnUpdateUI(wxUpdateUIEvent& event); 85 86 void OnIdle(wxIdleEvent& event); 87 88private: 89 // any class wishing to process wxWidgets events must use this macro 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, 104 NetTest_Dial, 105 NetTest_EnumISP, 106 NetTest_Check, 107 NetTest_Max 108}; 109 110// ---------------------------------------------------------------------------- 111// event tables and other macros for wxWidgets 112// ---------------------------------------------------------------------------- 113 114BEGIN_EVENT_TABLE(MyApp, wxApp) 115 EVT_DIALUP_CONNECTED(MyApp::OnConnected) 116 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected) 117END_EVENT_TABLE() 118 119// the event tables connect the wxWidgets events with the functions (event 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) 127 EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs) 128 EVT_MENU(NetTest_Check, MyFrame::OnCheck) 129 130 EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI) 131 132 EVT_IDLE(MyFrame::OnIdle) 133END_EVENT_TABLE() 134 135// Create a new application object: this macro will allow wxWidgets to create 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{ 153 // Create the main application window 154 MyFrame *frame = new MyFrame(_T("Dial-up wxWidgets demo"), 155 wxPoint(50, 50), wxSize(450, 340)); 156 157 // Show it and tell the application that it's our main window 158 frame->Show(true); 159 SetTopWindow(frame); 160 161 // Init dial up manager 162 m_dial = wxDialUpManager::Create(); 163 164 if ( !m_dial->IsOk() ) 165 { 166 wxLogError(wxT("The sample can't run on this system.")); 167 168#if wxUSE_LOG 169 wxLog::GetActiveTarget()->Flush(); 170#endif // wxUSE_LOG 171 172 // do it here, OnExit() won't be called 173 delete m_dial; 174 175 return false; 176 } 177 178#if wxUSE_STATUSBAR 179 frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? _T("LAN") : _T("No LAN"), 2); 180#endif // wxUSE_STATUSBAR 181 182 return true; 183} 184 185int MyApp::OnExit() 186{ 187 delete m_dial; 188 189 // exit code is 0, everything is ok 190 return 0; 191} 192 193void MyApp::OnConnected(wxDialUpEvent& event) 194{ 195 const wxChar *msg; 196 if ( event.IsOwnEvent() ) 197 { 198 msg = event.IsConnectedEvent() ? wxT("Successfully connected") 199 : wxT("Dialing failed"); 200 201 wxLogStatus(wxEmptyString); 202 } 203 else 204 { 205 msg = event.IsConnectedEvent() ? wxT("Just connected!") 206 : wxT("Disconnected"); 207 } 208 209 wxLogMessage(msg); 210} 211 212// ---------------------------------------------------------------------------- 213// main frame 214// ---------------------------------------------------------------------------- 215 216// frame constructor 217MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) 218 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) 219{ 220 // create a menu bar 221 wxMenu *menuFile = new wxMenu; 222 223 menuFile->Append(NetTest_Dial, _T("&Dial\tCtrl-D"), _T("Dial default ISP")); 224 menuFile->Append(NetTest_HangUp, _T("&HangUp\tCtrl-H"), _T("Hang up modem")); 225 menuFile->AppendSeparator(); 226 menuFile->Append(NetTest_EnumISP, _T("&Enumerate ISPs...\tCtrl-E")); 227 menuFile->Append(NetTest_Check, _T("&Check connection status...\tCtrl-C")); 228 menuFile->AppendSeparator(); 229 menuFile->Append(NetTest_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); 230 menuFile->AppendSeparator(); 231 menuFile->Append(NetTest_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); 232 233 // now append the freshly created menu to the menu bar... 234 wxMenuBar *menuBar = new wxMenuBar; 235 menuBar->Append(menuFile, _T("&File")); 236 237 // ... and attach this menu bar to the frame 238 SetMenuBar(menuBar); 239 240#if wxUSE_STATUSBAR 241 // create status bar and fill the LAN field 242 CreateStatusBar(3); 243 static const int widths[3] = { -1, 100, 60 }; 244 SetStatusWidths(3, widths); 245#endif // wxUSE_STATUSBAR 246} 247 248 249// event handlers 250 251void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) 252{ 253 // true is to force the frame to close 254 Close(true); 255} 256 257void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) 258{ 259 wxString msg; 260 msg.Printf( wxT("This is the network functions test sample.\n") 261 wxT("(c) 1999 Vadim Zeitlin") ); 262 263 wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this); 264} 265 266void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event)) 267{ 268 if ( wxGetApp().GetDialer()->HangUp() ) 269 { 270 wxLogStatus(this, wxT("Connection was successfully terminated.")); 271 } 272 else 273 { 274 wxLogStatus(this, wxT("Failed to hang up.")); 275 } 276} 277 278void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event)) 279{ 280 wxLogStatus(this, wxT("Preparing to dial...")); 281 wxYield(); 282 wxBeginBusyCursor(); 283 284 if ( wxGetApp().GetDialer()->Dial() ) 285 { 286 wxLogStatus(this, wxT("Dialing...")); 287 } 288 else 289 { 290 wxLogStatus(this, wxT("Dialing attempt failed.")); 291 } 292 293 wxEndBusyCursor(); 294} 295 296void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event)) 297{ 298 if(wxGetApp().GetDialer()->IsOnline()) 299 wxLogMessage(wxT("Network is online.")); 300 else 301 wxLogMessage(wxT("Network is offline.")); 302} 303 304void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event)) 305{ 306 wxArrayString names; 307 size_t nCount = wxGetApp().GetDialer()->GetISPNames(names); 308 if ( nCount == 0 ) 309 { 310 wxLogWarning(wxT("No ISPs found.")); 311 } 312 else 313 { 314 wxString msg = _T("Known ISPs:\n"); 315 for ( size_t n = 0; n < nCount; n++ ) 316 { 317 msg << names[n] << '\n'; 318 } 319 320 wxLogMessage(msg); 321 } 322} 323 324void MyFrame::OnUpdateUI(wxUpdateUIEvent& event) 325{ 326 // disable this item while dialing 327 event.Enable( !wxGetApp().GetDialer()->IsDialing() ); 328} 329 330void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event)) 331{ 332 static int s_isOnline = -1; // not true nor false 333 334 bool isOnline = wxGetApp().GetDialer()->IsOnline(); 335 if ( s_isOnline != (int)isOnline ) 336 { 337 s_isOnline = isOnline; 338 339#if wxUSE_STATUSBAR 340 SetStatusText(isOnline ? _T("Online") : _T("Offline"), 1); 341#endif // wxUSE_STATUSBAR 342 } 343} 344