]>
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 | |
09884325 VZ |
7 | // Copyright: (c) Vadim Zeitlin |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
09884325 VZ |
19 | // For compilers that support precompilation, includes "wx/wx.h". |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | // for all others, include the necessary headers (this file is usually all you | |
be5a51fb | 27 | // need because it includes almost all "standard" wxWidgets headers |
09884325 VZ |
28 | #ifndef WX_PRECOMP |
29 | #include "wx/wx.h" | |
30 | #endif | |
31 | ||
9832ce0b GD |
32 | #if !wxUSE_DIALUP_MANAGER |
33 | #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h! | |
34 | #endif | |
35 | ||
e90c1d2a | 36 | #include "wx/dialup.h" |
09884325 | 37 | |
e7092398 | 38 | #ifndef wxHAS_IMAGES_IN_RESOURCES |
41f02b9a FM |
39 | #include "../sample.xpm" |
40 | #endif | |
41 | ||
09884325 VZ |
42 | // ---------------------------------------------------------------------------- |
43 | // private classes | |
44 | // ---------------------------------------------------------------------------- | |
45 | ||
46 | // Define a new application type, each program should derive a class from wxApp | |
47 | class MyApp : public wxApp | |
48 | { | |
49 | public: | |
50 | // override base class virtuals | |
51 | // ---------------------------- | |
52 | ||
53 | // this one is called on application startup and is a good place for the app | |
54 | // initialization (doing it here and not in the ctor allows to have an error | |
55 | // return: if OnInit() returns false, the application terminates) | |
56 | virtual bool OnInit(); | |
57 | ||
24df4c19 VZ |
58 | // called before the application termination |
59 | virtual int OnExit(); | |
60 | ||
09884325 VZ |
61 | // event handlers |
62 | void OnConnected(wxDialUpEvent& event); | |
63 | ||
24df4c19 VZ |
64 | // accessor to dial up manager |
65 | wxDialUpManager *GetDialer() const { return m_dial; } | |
66 | ||
09884325 | 67 | private: |
24df4c19 VZ |
68 | wxDialUpManager *m_dial; |
69 | ||
8caa4ed1 | 70 | DECLARE_EVENT_TABLE() |
09884325 VZ |
71 | }; |
72 | ||
73 | // Define a new frame type: this is going to be our main frame | |
74 | class MyFrame : public wxFrame | |
75 | { | |
76 | public: | |
77 | // ctor(s) | |
78 | MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size); | |
79 | ||
80 | // event handlers (these functions should _not_ be virtual) | |
81 | void OnQuit(wxCommandEvent& event); | |
82 | void OnAbout(wxCommandEvent& event); | |
83 | void OnHangUp(wxCommandEvent& event); | |
84 | void OnDial(wxCommandEvent& event); | |
2690830e | 85 | void OnEnumISPs(wxCommandEvent& event); |
24e1f1ca | 86 | void OnCheck(wxCommandEvent& event); |
24df4c19 VZ |
87 | void OnUpdateUI(wxUpdateUIEvent& event); |
88 | ||
09884325 VZ |
89 | void OnIdle(wxIdleEvent& event); |
90 | ||
91 | private: | |
be5a51fb | 92 | // any class wishing to process wxWidgets events must use this macro |
09884325 VZ |
93 | DECLARE_EVENT_TABLE() |
94 | }; | |
95 | ||
96 | // ---------------------------------------------------------------------------- | |
97 | // constants | |
98 | // ---------------------------------------------------------------------------- | |
99 | ||
100 | // IDs for the controls and the menu commands | |
101 | enum | |
102 | { | |
103 | // menu items | |
104 | NetTest_Quit = 1, | |
105 | NetTest_About, | |
106 | NetTest_HangUp, | |
2690830e VZ |
107 | NetTest_Dial, |
108 | NetTest_EnumISP, | |
24e1f1ca | 109 | NetTest_Check, |
2690830e | 110 | NetTest_Max |
09884325 VZ |
111 | }; |
112 | ||
113 | // ---------------------------------------------------------------------------- | |
be5a51fb | 114 | // event tables and other macros for wxWidgets |
09884325 VZ |
115 | // ---------------------------------------------------------------------------- |
116 | ||
117 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
118 | EVT_DIALUP_CONNECTED(MyApp::OnConnected) | |
119 | EVT_DIALUP_DISCONNECTED(MyApp::OnConnected) | |
120 | END_EVENT_TABLE() | |
121 | ||
be5a51fb | 122 | // the event tables connect the wxWidgets events with the functions (event |
09884325 VZ |
123 | // handlers) which process them. It can be also done at run-time, but for the |
124 | // simple menu events like this the static method is much simpler. | |
125 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
126 | EVT_MENU(NetTest_Quit, MyFrame::OnQuit) | |
127 | EVT_MENU(NetTest_About, MyFrame::OnAbout) | |
128 | EVT_MENU(NetTest_HangUp, MyFrame::OnHangUp) | |
129 | EVT_MENU(NetTest_Dial, MyFrame::OnDial) | |
2690830e | 130 | EVT_MENU(NetTest_EnumISP, MyFrame::OnEnumISPs) |
24e1f1ca | 131 | EVT_MENU(NetTest_Check, MyFrame::OnCheck) |
09884325 | 132 | |
24df4c19 VZ |
133 | EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI) |
134 | ||
09884325 VZ |
135 | EVT_IDLE(MyFrame::OnIdle) |
136 | END_EVENT_TABLE() | |
137 | ||
be5a51fb | 138 | // Create a new application object: this macro will allow wxWidgets to create |
09884325 VZ |
139 | // the application object during program execution (it's better than using a |
140 | // static object for many reasons) and also declares the accessor function | |
141 | // wxGetApp() which will return the reference of the right type (i.e. MyApp and | |
142 | // not wxApp) | |
143 | IMPLEMENT_APP(MyApp) | |
144 | ||
145 | // ============================================================================ | |
146 | // implementation | |
147 | // ============================================================================ | |
148 | ||
149 | // ---------------------------------------------------------------------------- | |
150 | // the application class | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
153 | // `Main program' equivalent: the program execution "starts" here | |
154 | bool MyApp::OnInit() | |
155 | { | |
45e6e6f8 VZ |
156 | if ( !wxApp::OnInit() ) |
157 | return false; | |
158 | ||
09884325 | 159 | // Create the main application window |
9a83f860 | 160 | MyFrame *frame = new MyFrame(wxT("Dial-up wxWidgets demo"), |
09884325 VZ |
161 | wxPoint(50, 50), wxSize(450, 340)); |
162 | ||
18f42b94 | 163 | // Show it |
f2aea0d1 | 164 | frame->Show(true); |
09884325 | 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 |
9a83f860 | 184 | frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? wxT("LAN") : wxT("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 | { |
41f02b9a FM |
225 | SetIcon(wxICON(sample)); |
226 | ||
09884325 VZ |
227 | // create a menu bar |
228 | wxMenu *menuFile = new wxMenu; | |
229 | ||
9a83f860 VZ |
230 | menuFile->Append(NetTest_Dial, wxT("&Dial\tCtrl-D"), wxT("Dial default ISP")); |
231 | menuFile->Append(NetTest_HangUp, wxT("&HangUp\tCtrl-H"), wxT("Hang up modem")); | |
09884325 | 232 | menuFile->AppendSeparator(); |
9a83f860 VZ |
233 | menuFile->Append(NetTest_EnumISP, wxT("&Enumerate ISPs...\tCtrl-E")); |
234 | menuFile->Append(NetTest_Check, wxT("&Check connection status...\tCtrl-C")); | |
2690830e | 235 | menuFile->AppendSeparator(); |
2d143b66 | 236 | menuFile->Append(NetTest_About, wxT("&About\tCtrl-A"), wxT("Show about dialog")); |
09884325 | 237 | menuFile->AppendSeparator(); |
9a83f860 | 238 | menuFile->Append(NetTest_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program")); |
09884325 VZ |
239 | |
240 | // now append the freshly created menu to the menu bar... | |
241 | wxMenuBar *menuBar = new wxMenuBar; | |
9a83f860 | 242 | menuBar->Append(menuFile, wxT("&File")); |
09884325 VZ |
243 | |
244 | // ... and attach this menu bar to the frame | |
245 | SetMenuBar(menuBar); | |
246 | ||
8520f137 | 247 | #if wxUSE_STATUSBAR |
2690830e VZ |
248 | // create status bar and fill the LAN field |
249 | CreateStatusBar(3); | |
250 | static const int widths[3] = { -1, 100, 60 }; | |
251 | SetStatusWidths(3, widths); | |
8520f137 | 252 | #endif // wxUSE_STATUSBAR |
09884325 VZ |
253 | } |
254 | ||
255 | ||
256 | // event handlers | |
257 | ||
258 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
259 | { | |
f2aea0d1 WS |
260 | // true is to force the frame to close |
261 | Close(true); | |
09884325 VZ |
262 | } |
263 | ||
264 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
265 | { | |
266 | wxString msg; | |
f565a6c2 | 267 | msg.Printf( wxT("This is the network functions test sample.\n") |
749bfe9a | 268 | wxT("(c) 1999 Vadim Zeitlin") ); |
09884325 | 269 | |
f565a6c2 | 270 | wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this); |
09884325 VZ |
271 | } |
272 | ||
273 | void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event)) | |
274 | { | |
24df4c19 | 275 | if ( wxGetApp().GetDialer()->HangUp() ) |
09884325 | 276 | { |
3103e8a9 | 277 | wxLogStatus(this, wxT("Connection was successfully terminated.")); |
09884325 VZ |
278 | } |
279 | else | |
280 | { | |
4693b20c | 281 | wxLogStatus(this, wxT("Failed to hang up.")); |
09884325 VZ |
282 | } |
283 | } | |
284 | ||
285 | void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event)) | |
286 | { | |
4693b20c | 287 | wxLogStatus(this, wxT("Preparing to dial...")); |
09884325 VZ |
288 | wxYield(); |
289 | wxBeginBusyCursor(); | |
290 | ||
2690830e | 291 | if ( wxGetApp().GetDialer()->Dial() ) |
09884325 | 292 | { |
4693b20c | 293 | wxLogStatus(this, wxT("Dialing...")); |
09884325 VZ |
294 | } |
295 | else | |
296 | { | |
4693b20c | 297 | wxLogStatus(this, wxT("Dialing attempt failed.")); |
09884325 VZ |
298 | } |
299 | ||
300 | wxEndBusyCursor(); | |
301 | } | |
302 | ||
24e1f1ca KB |
303 | void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event)) |
304 | { | |
305 | if(wxGetApp().GetDialer()->IsOnline()) | |
43b2d5e7 | 306 | { |
4693b20c | 307 | wxLogMessage(wxT("Network is online.")); |
43b2d5e7 | 308 | } |
24e1f1ca | 309 | else |
43b2d5e7 | 310 | { |
4693b20c | 311 | wxLogMessage(wxT("Network is offline.")); |
43b2d5e7 | 312 | } |
24e1f1ca KB |
313 | } |
314 | ||
2690830e VZ |
315 | void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event)) |
316 | { | |
317 | wxArrayString names; | |
318 | size_t nCount = wxGetApp().GetDialer()->GetISPNames(names); | |
319 | if ( nCount == 0 ) | |
320 | { | |
4693b20c | 321 | wxLogWarning(wxT("No ISPs found.")); |
2690830e VZ |
322 | } |
323 | else | |
324 | { | |
9a83f860 | 325 | wxString msg = wxT("Known ISPs:\n"); |
2690830e VZ |
326 | for ( size_t n = 0; n < nCount; n++ ) |
327 | { | |
328 | msg << names[n] << '\n'; | |
329 | } | |
330 | ||
331 | wxLogMessage(msg); | |
332 | } | |
333 | } | |
334 | ||
24df4c19 VZ |
335 | void MyFrame::OnUpdateUI(wxUpdateUIEvent& event) |
336 | { | |
337 | // disable this item while dialing | |
338 | event.Enable( !wxGetApp().GetDialer()->IsDialing() ); | |
339 | } | |
340 | ||
09884325 VZ |
341 | void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event)) |
342 | { | |
f2aea0d1 | 343 | static int s_isOnline = -1; // not true nor false |
09884325 | 344 | |
24df4c19 | 345 | bool isOnline = wxGetApp().GetDialer()->IsOnline(); |
09884325 VZ |
346 | if ( s_isOnline != (int)isOnline ) |
347 | { | |
348 | s_isOnline = isOnline; | |
349 | ||
8520f137 | 350 | #if wxUSE_STATUSBAR |
9a83f860 | 351 | SetStatusText(isOnline ? wxT("Online") : wxT("Offline"), 1); |
8520f137 | 352 | #endif // wxUSE_STATUSBAR |
09884325 VZ |
353 | } |
354 | } |