]>
Commit | Line | Data |
---|---|---|
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 | ||
3d08c0e7 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
11fdee42 WS |
21 | #pragma implementation |
22 | #pragma interface | |
09884325 VZ |
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 | |
be5a51fb | 33 | // need because it includes almost all "standard" wxWidgets headers |
09884325 VZ |
34 | #ifndef WX_PRECOMP |
35 | #include "wx/wx.h" | |
36 | #endif | |
37 | ||
9832ce0b GD |
38 | #if !wxUSE_DIALUP_MANAGER |
39 | #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h! | |
40 | #endif | |
41 | ||
e90c1d2a | 42 | #include "wx/dialup.h" |
09884325 VZ |
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 | ||
24df4c19 VZ |
60 | // called before the application termination |
61 | virtual int OnExit(); | |
62 | ||
09884325 VZ |
63 | // event handlers |
64 | void OnConnected(wxDialUpEvent& event); | |
65 | ||
24df4c19 VZ |
66 | // accessor to dial up manager |
67 | wxDialUpManager *GetDialer() const { return m_dial; } | |
68 | ||
09884325 | 69 | private: |
24df4c19 VZ |
70 | wxDialUpManager *m_dial; |
71 | ||
8caa4ed1 | 72 | DECLARE_EVENT_TABLE() |
09884325 VZ |
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); | |
2690830e | 87 | void OnEnumISPs(wxCommandEvent& event); |
24e1f1ca | 88 | void OnCheck(wxCommandEvent& event); |
24df4c19 VZ |
89 | void OnUpdateUI(wxUpdateUIEvent& event); |
90 | ||
09884325 VZ |
91 | void OnIdle(wxIdleEvent& event); |
92 | ||
93 | private: | |
be5a51fb | 94 | // any class wishing to process wxWidgets events must use this macro |
09884325 VZ |
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, | |
2690830e VZ |
109 | NetTest_Dial, |
110 | NetTest_EnumISP, | |
24e1f1ca | 111 | NetTest_Check, |
2690830e | 112 | NetTest_Max |
09884325 VZ |
113 | }; |
114 | ||
115 | // ---------------------------------------------------------------------------- | |
be5a51fb | 116 | // event tables and other macros for wxWidgets |
09884325 VZ |
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 | ||
be5a51fb | 124 | // the event tables connect the wxWidgets events with the functions (event |
09884325 VZ |
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) | |
2690830e | 132 | EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs) |
24e1f1ca | 133 | EVT_MENU(NetTest_Check, MyFrame::OnCheck) |
09884325 | 134 | |
24df4c19 VZ |
135 | EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI) |
136 | ||
09884325 VZ |
137 | EVT_IDLE(MyFrame::OnIdle) |
138 | END_EVENT_TABLE() | |
139 | ||
be5a51fb | 140 | // Create a new application object: this macro will allow wxWidgets to create |
09884325 VZ |
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 | |
be5a51fb | 159 | MyFrame *frame = new MyFrame(_T("Dial-up wxWidgets demo"), |
09884325 VZ |
160 | wxPoint(50, 50), wxSize(450, 340)); |
161 | ||
162 | // Show it and tell the application that it's our main window | |
f2aea0d1 | 163 | frame->Show(true); |
09884325 VZ |
164 | SetTopWindow(frame); |
165 | ||
24df4c19 VZ |
166 | // Init dial up manager |
167 | m_dial = wxDialUpManager::Create(); | |
168 | ||
169 | if ( !m_dial->IsOk() ) | |
170 | { | |
4693b20c | 171 | wxLogError(wxT("The sample can't run on this system.")); |
24df4c19 | 172 | |
f07941fc | 173 | #if wxUSE_LOG |
24df4c19 | 174 | wxLog::GetActiveTarget()->Flush(); |
f07941fc | 175 | #endif // wxUSE_LOG |
24df4c19 VZ |
176 | |
177 | // do it here, OnExit() won't be called | |
178 | delete m_dial; | |
179 | ||
f2aea0d1 | 180 | return false; |
24df4c19 VZ |
181 | } |
182 | ||
8520f137 | 183 | #if wxUSE_STATUSBAR |
9f84eccd | 184 | frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? _T("LAN") : _T("No LAN"), 2); |
8520f137 | 185 | #endif // wxUSE_STATUSBAR |
2690830e | 186 | |
f2aea0d1 | 187 | return true; |
09884325 VZ |
188 | } |
189 | ||
24df4c19 VZ |
190 | int MyApp::OnExit() |
191 | { | |
192 | delete m_dial; | |
193 | ||
194 | // exit code is 0, everything is ok | |
195 | return 0; | |
196 | } | |
197 | ||
09884325 VZ |
198 | void MyApp::OnConnected(wxDialUpEvent& event) |
199 | { | |
4693b20c | 200 | const wxChar *msg; |
24df4c19 VZ |
201 | if ( event.IsOwnEvent() ) |
202 | { | |
4693b20c MB |
203 | msg = event.IsConnectedEvent() ? wxT("Successfully connected") |
204 | : wxT("Dialing failed"); | |
24df4c19 | 205 | |
aec18ff7 | 206 | wxLogStatus(wxEmptyString); |
24df4c19 VZ |
207 | } |
208 | else | |
209 | { | |
4693b20c MB |
210 | msg = event.IsConnectedEvent() ? wxT("Just connected!") |
211 | : wxT("Disconnected"); | |
24df4c19 VZ |
212 | } |
213 | ||
2690830e | 214 | wxLogMessage(msg); |
09884325 VZ |
215 | } |
216 | ||
217 | // ---------------------------------------------------------------------------- | |
218 | // main frame | |
219 | // ---------------------------------------------------------------------------- | |
220 | ||
221 | // frame constructor | |
222 | MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) | |
f2aea0d1 | 223 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size) |
09884325 | 224 | { |
09884325 VZ |
225 | // create a menu bar |
226 | wxMenu *menuFile = new wxMenu; | |
227 | ||
9f84eccd MB |
228 | menuFile->Append(NetTest_Dial, _T("&Dial\tCtrl-D"), _T("Dial default ISP")); |
229 | menuFile->Append(NetTest_HangUp, _T("&HangUp\tCtrl-H"), _T("Hang up modem")); | |
09884325 | 230 | menuFile->AppendSeparator(); |
9f84eccd MB |
231 | menuFile->Append(NetTest_EnumISP, _T("&Enumerate ISPs...\tCtrl-E")); |
232 | menuFile->Append(NetTest_Check, _T("&Check connection status...\tCtrl-C")); | |
2690830e | 233 | menuFile->AppendSeparator(); |
9f84eccd | 234 | menuFile->Append(NetTest_About, _T("&About...\tCtrl-A"), _T("Show about dialog")); |
09884325 | 235 | menuFile->AppendSeparator(); |
9f84eccd | 236 | menuFile->Append(NetTest_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
09884325 VZ |
237 | |
238 | // now append the freshly created menu to the menu bar... | |
239 | wxMenuBar *menuBar = new wxMenuBar; | |
9f84eccd | 240 | menuBar->Append(menuFile, _T("&File")); |
09884325 VZ |
241 | |
242 | // ... and attach this menu bar to the frame | |
243 | SetMenuBar(menuBar); | |
244 | ||
8520f137 | 245 | #if wxUSE_STATUSBAR |
2690830e VZ |
246 | // create status bar and fill the LAN field |
247 | CreateStatusBar(3); | |
248 | static const int widths[3] = { -1, 100, 60 }; | |
249 | SetStatusWidths(3, widths); | |
8520f137 | 250 | #endif // wxUSE_STATUSBAR |
09884325 VZ |
251 | } |
252 | ||
253 | ||
254 | // event handlers | |
255 | ||
256 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
257 | { | |
f2aea0d1 WS |
258 | // true is to force the frame to close |
259 | Close(true); | |
09884325 VZ |
260 | } |
261 | ||
262 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
263 | { | |
264 | wxString msg; | |
f565a6c2 | 265 | msg.Printf( wxT("This is the network functions test sample.\n") |
749bfe9a | 266 | wxT("(c) 1999 Vadim Zeitlin") ); |
09884325 | 267 | |
f565a6c2 | 268 | wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this); |
09884325 VZ |
269 | } |
270 | ||
271 | void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event)) | |
272 | { | |
24df4c19 | 273 | if ( wxGetApp().GetDialer()->HangUp() ) |
09884325 | 274 | { |
4693b20c | 275 | wxLogStatus(this, wxT("Connection was succesfully terminated.")); |
09884325 VZ |
276 | } |
277 | else | |
278 | { | |
4693b20c | 279 | wxLogStatus(this, wxT("Failed to hang up.")); |
09884325 VZ |
280 | } |
281 | } | |
282 | ||
283 | void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event)) | |
284 | { | |
4693b20c | 285 | wxLogStatus(this, wxT("Preparing to dial...")); |
09884325 VZ |
286 | wxYield(); |
287 | wxBeginBusyCursor(); | |
288 | ||
2690830e | 289 | if ( wxGetApp().GetDialer()->Dial() ) |
09884325 | 290 | { |
4693b20c | 291 | wxLogStatus(this, wxT("Dialing...")); |
09884325 VZ |
292 | } |
293 | else | |
294 | { | |
4693b20c | 295 | wxLogStatus(this, wxT("Dialing attempt failed.")); |
09884325 VZ |
296 | } |
297 | ||
298 | wxEndBusyCursor(); | |
299 | } | |
300 | ||
24e1f1ca KB |
301 | void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event)) |
302 | { | |
303 | if(wxGetApp().GetDialer()->IsOnline()) | |
4693b20c | 304 | wxLogMessage(wxT("Network is online.")); |
24e1f1ca | 305 | else |
4693b20c | 306 | wxLogMessage(wxT("Network is offline.")); |
24e1f1ca KB |
307 | } |
308 | ||
2690830e VZ |
309 | void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event)) |
310 | { | |
311 | wxArrayString names; | |
312 | size_t nCount = wxGetApp().GetDialer()->GetISPNames(names); | |
313 | if ( nCount == 0 ) | |
314 | { | |
4693b20c | 315 | wxLogWarning(wxT("No ISPs found.")); |
2690830e VZ |
316 | } |
317 | else | |
318 | { | |
9f84eccd | 319 | wxString msg = _T("Known ISPs:\n"); |
2690830e VZ |
320 | for ( size_t n = 0; n < nCount; n++ ) |
321 | { | |
322 | msg << names[n] << '\n'; | |
323 | } | |
324 | ||
325 | wxLogMessage(msg); | |
326 | } | |
327 | } | |
328 | ||
24df4c19 VZ |
329 | void MyFrame::OnUpdateUI(wxUpdateUIEvent& event) |
330 | { | |
331 | // disable this item while dialing | |
332 | event.Enable( !wxGetApp().GetDialer()->IsDialing() ); | |
333 | } | |
334 | ||
09884325 VZ |
335 | void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event)) |
336 | { | |
f2aea0d1 | 337 | static int s_isOnline = -1; // not true nor false |
09884325 | 338 | |
24df4c19 | 339 | bool isOnline = wxGetApp().GetDialer()->IsOnline(); |
09884325 VZ |
340 | if ( s_isOnline != (int)isOnline ) |
341 | { | |
342 | s_isOnline = isOnline; | |
343 | ||
8520f137 | 344 | #if wxUSE_STATUSBAR |
9f84eccd | 345 | SetStatusText(isOnline ? _T("Online") : _T("Offline"), 1); |
8520f137 | 346 | #endif // wxUSE_STATUSBAR |
09884325 VZ |
347 | } |
348 | } |