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