nettest.cpp 9.8 KB

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