]> git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
Add @onlyfor tags for Unix-specific wxStandardPaths methods.
[wxWidgets.git] / samples / dialup / nettest.cpp
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$
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 #ifndef __WXMSW__
40 #include "../sample.xpm"
41 #endif
42
43 // ----------------------------------------------------------------------------
44 // private classes
45 // ----------------------------------------------------------------------------
46
47 // Define a new application type, each program should derive a class from wxApp
48 class MyApp : public wxApp
49 {
50 public:
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
59 // called before the application termination
60 virtual int OnExit();
61
62 // event handlers
63 void OnConnected(wxDialUpEvent& event);
64
65 // accessor to dial up manager
66 wxDialUpManager *GetDialer() const { return m_dial; }
67
68 private:
69 wxDialUpManager *m_dial;
70
71 DECLARE_EVENT_TABLE()
72 };
73
74 // Define a new frame type: this is going to be our main frame
75 class MyFrame : public wxFrame
76 {
77 public:
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);
86 void OnEnumISPs(wxCommandEvent& event);
87 void OnCheck(wxCommandEvent& event);
88 void OnUpdateUI(wxUpdateUIEvent& event);
89
90 void OnIdle(wxIdleEvent& event);
91
92 private:
93 // any class wishing to process wxWidgets events must use this macro
94 DECLARE_EVENT_TABLE()
95 };
96
97 // ----------------------------------------------------------------------------
98 // constants
99 // ----------------------------------------------------------------------------
100
101 // IDs for the controls and the menu commands
102 enum
103 {
104 // menu items
105 NetTest_Quit = 1,
106 NetTest_About,
107 NetTest_HangUp,
108 NetTest_Dial,
109 NetTest_EnumISP,
110 NetTest_Check,
111 NetTest_Max
112 };
113
114 // ----------------------------------------------------------------------------
115 // event tables and other macros for wxWidgets
116 // ----------------------------------------------------------------------------
117
118 BEGIN_EVENT_TABLE(MyApp, wxApp)
119 EVT_DIALUP_CONNECTED(MyApp::OnConnected)
120 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected)
121 END_EVENT_TABLE()
122
123 // the event tables connect the wxWidgets events with the functions (event
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.
126 BEGIN_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)
131 EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs)
132 EVT_MENU(NetTest_Check, MyFrame::OnCheck)
133
134 EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI)
135
136 EVT_IDLE(MyFrame::OnIdle)
137 END_EVENT_TABLE()
138
139 // Create a new application object: this macro will allow wxWidgets to create
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)
144 IMPLEMENT_APP(MyApp)
145
146 // ============================================================================
147 // implementation
148 // ============================================================================
149
150 // ----------------------------------------------------------------------------
151 // the application class
152 // ----------------------------------------------------------------------------
153
154 // `Main program' equivalent: the program execution "starts" here
155 bool MyApp::OnInit()
156 {
157 if ( !wxApp::OnInit() )
158 return false;
159
160 // Create the main application window
161 MyFrame *frame = new MyFrame(wxT("Dial-up wxWidgets demo"),
162 wxPoint(50, 50), wxSize(450, 340));
163
164 // Show it
165 frame->Show(true);
166
167 // Init dial up manager
168 m_dial = wxDialUpManager::Create();
169
170 if ( !m_dial->IsOk() )
171 {
172 wxLogError(wxT("The sample can't run on this system."));
173
174 #if wxUSE_LOG
175 wxLog::GetActiveTarget()->Flush();
176 #endif // wxUSE_LOG
177
178 // do it here, OnExit() won't be called
179 delete m_dial;
180
181 return false;
182 }
183
184 #if wxUSE_STATUSBAR
185 frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? wxT("LAN") : wxT("No LAN"), 2);
186 #endif // wxUSE_STATUSBAR
187
188 return true;
189 }
190
191 int MyApp::OnExit()
192 {
193 delete m_dial;
194
195 // exit code is 0, everything is ok
196 return 0;
197 }
198
199 void MyApp::OnConnected(wxDialUpEvent& event)
200 {
201 const wxChar *msg;
202 if ( event.IsOwnEvent() )
203 {
204 msg = event.IsConnectedEvent() ? wxT("Successfully connected")
205 : wxT("Dialing failed");
206
207 wxLogStatus(wxEmptyString);
208 }
209 else
210 {
211 msg = event.IsConnectedEvent() ? wxT("Just connected!")
212 : wxT("Disconnected");
213 }
214
215 wxLogMessage(msg);
216 }
217
218 // ----------------------------------------------------------------------------
219 // main frame
220 // ----------------------------------------------------------------------------
221
222 // frame constructor
223 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
224 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
225 {
226 SetIcon(wxICON(sample));
227
228 // create a menu bar
229 wxMenu *menuFile = new wxMenu;
230
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"));
233 menuFile->AppendSeparator();
234 menuFile->Append(NetTest_EnumISP, wxT("&Enumerate ISPs...\tCtrl-E"));
235 menuFile->Append(NetTest_Check, wxT("&Check connection status...\tCtrl-C"));
236 menuFile->AppendSeparator();
237 menuFile->Append(NetTest_About, wxT("&About\tCtrl-A"), wxT("Show about dialog"));
238 menuFile->AppendSeparator();
239 menuFile->Append(NetTest_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
240
241 // now append the freshly created menu to the menu bar...
242 wxMenuBar *menuBar = new wxMenuBar;
243 menuBar->Append(menuFile, wxT("&File"));
244
245 // ... and attach this menu bar to the frame
246 SetMenuBar(menuBar);
247
248 #if wxUSE_STATUSBAR
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);
253 #endif // wxUSE_STATUSBAR
254 }
255
256
257 // event handlers
258
259 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
260 {
261 // true is to force the frame to close
262 Close(true);
263 }
264
265 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
266 {
267 wxString msg;
268 msg.Printf( wxT("This is the network functions test sample.\n")
269 wxT("(c) 1999 Vadim Zeitlin") );
270
271 wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this);
272 }
273
274 void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event))
275 {
276 if ( wxGetApp().GetDialer()->HangUp() )
277 {
278 wxLogStatus(this, wxT("Connection was successfully terminated."));
279 }
280 else
281 {
282 wxLogStatus(this, wxT("Failed to hang up."));
283 }
284 }
285
286 void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event))
287 {
288 wxLogStatus(this, wxT("Preparing to dial..."));
289 wxYield();
290 wxBeginBusyCursor();
291
292 if ( wxGetApp().GetDialer()->Dial() )
293 {
294 wxLogStatus(this, wxT("Dialing..."));
295 }
296 else
297 {
298 wxLogStatus(this, wxT("Dialing attempt failed."));
299 }
300
301 wxEndBusyCursor();
302 }
303
304 void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event))
305 {
306 if(wxGetApp().GetDialer()->IsOnline())
307 {
308 wxLogMessage(wxT("Network is online."));
309 }
310 else
311 {
312 wxLogMessage(wxT("Network is offline."));
313 }
314 }
315
316 void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event))
317 {
318 wxArrayString names;
319 size_t nCount = wxGetApp().GetDialer()->GetISPNames(names);
320 if ( nCount == 0 )
321 {
322 wxLogWarning(wxT("No ISPs found."));
323 }
324 else
325 {
326 wxString msg = wxT("Known ISPs:\n");
327 for ( size_t n = 0; n < nCount; n++ )
328 {
329 msg << names[n] << '\n';
330 }
331
332 wxLogMessage(msg);
333 }
334 }
335
336 void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
337 {
338 // disable this item while dialing
339 event.Enable( !wxGetApp().GetDialer()->IsDialing() );
340 }
341
342 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
343 {
344 static int s_isOnline = -1; // not true nor false
345
346 bool isOnline = wxGetApp().GetDialer()->IsOnline();
347 if ( s_isOnline != (int)isOnline )
348 {
349 s_isOnline = isOnline;
350
351 #if wxUSE_STATUSBAR
352 SetStatusText(isOnline ? wxT("Online") : wxT("Offline"), 1);
353 #endif // wxUSE_STATUSBAR
354 }
355 }