]> git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
Made wxInfoBar::SetFont() change the text message font.
[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 and tell the application that it's our main window
165 frame->Show(true);
166 SetTopWindow(frame);
167
168 // Init dial up manager
169 m_dial = wxDialUpManager::Create();
170
171 if ( !m_dial->IsOk() )
172 {
173 wxLogError(wxT("The sample can't run on this system."));
174
175 #if wxUSE_LOG
176 wxLog::GetActiveTarget()->Flush();
177 #endif // wxUSE_LOG
178
179 // do it here, OnExit() won't be called
180 delete m_dial;
181
182 return false;
183 }
184
185 #if wxUSE_STATUSBAR
186 frame->SetStatusText(GetDialer()->IsAlwaysOnline() ? wxT("LAN") : wxT("No LAN"), 2);
187 #endif // wxUSE_STATUSBAR
188
189 return true;
190 }
191
192 int MyApp::OnExit()
193 {
194 delete m_dial;
195
196 // exit code is 0, everything is ok
197 return 0;
198 }
199
200 void MyApp::OnConnected(wxDialUpEvent& event)
201 {
202 const wxChar *msg;
203 if ( event.IsOwnEvent() )
204 {
205 msg = event.IsConnectedEvent() ? wxT("Successfully connected")
206 : wxT("Dialing failed");
207
208 wxLogStatus(wxEmptyString);
209 }
210 else
211 {
212 msg = event.IsConnectedEvent() ? wxT("Just connected!")
213 : wxT("Disconnected");
214 }
215
216 wxLogMessage(msg);
217 }
218
219 // ----------------------------------------------------------------------------
220 // main frame
221 // ----------------------------------------------------------------------------
222
223 // frame constructor
224 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
225 : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
226 {
227 SetIcon(wxICON(sample));
228
229 // create a menu bar
230 wxMenu *menuFile = new wxMenu;
231
232 menuFile->Append(NetTest_Dial, wxT("&Dial\tCtrl-D"), wxT("Dial default ISP"));
233 menuFile->Append(NetTest_HangUp, wxT("&HangUp\tCtrl-H"), wxT("Hang up modem"));
234 menuFile->AppendSeparator();
235 menuFile->Append(NetTest_EnumISP, wxT("&Enumerate ISPs...\tCtrl-E"));
236 menuFile->Append(NetTest_Check, wxT("&Check connection status...\tCtrl-C"));
237 menuFile->AppendSeparator();
238 menuFile->Append(NetTest_About, wxT("&About...\tCtrl-A"), wxT("Show about dialog"));
239 menuFile->AppendSeparator();
240 menuFile->Append(NetTest_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
241
242 // now append the freshly created menu to the menu bar...
243 wxMenuBar *menuBar = new wxMenuBar;
244 menuBar->Append(menuFile, wxT("&File"));
245
246 // ... and attach this menu bar to the frame
247 SetMenuBar(menuBar);
248
249 #if wxUSE_STATUSBAR
250 // create status bar and fill the LAN field
251 CreateStatusBar(3);
252 static const int widths[3] = { -1, 100, 60 };
253 SetStatusWidths(3, widths);
254 #endif // wxUSE_STATUSBAR
255 }
256
257
258 // event handlers
259
260 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
261 {
262 // true is to force the frame to close
263 Close(true);
264 }
265
266 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
267 {
268 wxString msg;
269 msg.Printf( wxT("This is the network functions test sample.\n")
270 wxT("(c) 1999 Vadim Zeitlin") );
271
272 wxMessageBox(msg, wxT("About NetTest"), wxOK | wxICON_INFORMATION, this);
273 }
274
275 void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event))
276 {
277 if ( wxGetApp().GetDialer()->HangUp() )
278 {
279 wxLogStatus(this, wxT("Connection was successfully terminated."));
280 }
281 else
282 {
283 wxLogStatus(this, wxT("Failed to hang up."));
284 }
285 }
286
287 void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event))
288 {
289 wxLogStatus(this, wxT("Preparing to dial..."));
290 wxYield();
291 wxBeginBusyCursor();
292
293 if ( wxGetApp().GetDialer()->Dial() )
294 {
295 wxLogStatus(this, wxT("Dialing..."));
296 }
297 else
298 {
299 wxLogStatus(this, wxT("Dialing attempt failed."));
300 }
301
302 wxEndBusyCursor();
303 }
304
305 void MyFrame::OnCheck(wxCommandEvent& WXUNUSED(event))
306 {
307 if(wxGetApp().GetDialer()->IsOnline())
308 {
309 wxLogMessage(wxT("Network is online."));
310 }
311 else
312 {
313 wxLogMessage(wxT("Network is offline."));
314 }
315 }
316
317 void MyFrame::OnEnumISPs(wxCommandEvent& WXUNUSED(event))
318 {
319 wxArrayString names;
320 size_t nCount = wxGetApp().GetDialer()->GetISPNames(names);
321 if ( nCount == 0 )
322 {
323 wxLogWarning(wxT("No ISPs found."));
324 }
325 else
326 {
327 wxString msg = wxT("Known ISPs:\n");
328 for ( size_t n = 0; n < nCount; n++ )
329 {
330 msg << names[n] << '\n';
331 }
332
333 wxLogMessage(msg);
334 }
335 }
336
337 void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
338 {
339 // disable this item while dialing
340 event.Enable( !wxGetApp().GetDialer()->IsDialing() );
341 }
342
343 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
344 {
345 static int s_isOnline = -1; // not true nor false
346
347 bool isOnline = wxGetApp().GetDialer()->IsOnline();
348 if ( s_isOnline != (int)isOnline )
349 {
350 s_isOnline = isOnline;
351
352 #if wxUSE_STATUSBAR
353 SetStatusText(isOnline ? wxT("Online") : wxT("Offline"), 1);
354 #endif // wxUSE_STATUSBAR
355 }
356 }