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