]> git.saurik.com Git - wxWidgets.git/blob - samples/nettest/nettest.cpp
81b1307b3abdd3f7c6b06b73d24ab00af64f08c8
[wxWidgets.git] / samples / nettest / 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/net.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
84 void OnUpdateUI(wxUpdateUIEvent& event);
85
86 void OnIdle(wxIdleEvent& event);
87
88 private:
89 // any class wishing to process wxWindows events must use this macro
90 DECLARE_EVENT_TABLE()
91 };
92
93 // ----------------------------------------------------------------------------
94 // constants
95 // ----------------------------------------------------------------------------
96
97 // IDs for the controls and the menu commands
98 enum
99 {
100 // menu items
101 NetTest_Quit = 1,
102 NetTest_About,
103 NetTest_HangUp,
104 NetTest_Dial
105 };
106
107 // ----------------------------------------------------------------------------
108 // event tables and other macros for wxWindows
109 // ----------------------------------------------------------------------------
110
111 BEGIN_EVENT_TABLE(MyApp, wxApp)
112 EVT_DIALUP_CONNECTED(MyApp::OnConnected)
113 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected)
114 END_EVENT_TABLE()
115
116 // the event tables connect the wxWindows events with the functions (event
117 // handlers) which process them. It can be also done at run-time, but for the
118 // simple menu events like this the static method is much simpler.
119 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
120 EVT_MENU(NetTest_Quit, MyFrame::OnQuit)
121 EVT_MENU(NetTest_About, MyFrame::OnAbout)
122 EVT_MENU(NetTest_HangUp, MyFrame::OnHangUp)
123 EVT_MENU(NetTest_Dial, MyFrame::OnDial)
124
125 EVT_UPDATE_UI(NetTest_Dial, MyFrame::OnUpdateUI)
126
127 EVT_IDLE(MyFrame::OnIdle)
128 END_EVENT_TABLE()
129
130 // Create a new application object: this macro will allow wxWindows to create
131 // the application object during program execution (it's better than using a
132 // static object for many reasons) and also declares the accessor function
133 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
134 // not wxApp)
135 IMPLEMENT_APP(MyApp)
136
137 // ============================================================================
138 // implementation
139 // ============================================================================
140
141 // ----------------------------------------------------------------------------
142 // the application class
143 // ----------------------------------------------------------------------------
144
145 // `Main program' equivalent: the program execution "starts" here
146 bool MyApp::OnInit()
147 {
148 // Create the main application window
149 MyFrame *frame = new MyFrame("Dial-up wxWindows demo",
150 wxPoint(50, 50), wxSize(450, 340));
151
152 // Show it and tell the application that it's our main window
153 frame->Show(TRUE);
154 SetTopWindow(frame);
155
156 // Init dial up manager
157 m_dial = wxDialUpManager::Create();
158
159 if ( !m_dial->IsOk() )
160 {
161 wxLogError("The sample can't run on this system.");
162
163 wxLog::GetActiveTarget()->Flush();
164
165 // do it here, OnExit() won't be called
166 delete m_dial;
167
168 return FALSE;
169 }
170
171 return TRUE;
172 }
173
174 int MyApp::OnExit()
175 {
176 delete m_dial;
177
178 // exit code is 0, everything is ok
179 return 0;
180 }
181
182 void MyApp::OnConnected(wxDialUpEvent& event)
183 {
184 const char *msg;
185 if ( event.IsOwnEvent() )
186 {
187 msg = event.IsConnectedEvent() ? "Successfully connected"
188 : "Dialing failed";
189
190 wxLogStatus("");
191 }
192 else
193 {
194 msg = event.IsConnectedEvent() ? "Just connected!"
195 : "Disconnected";
196 }
197
198 wxMessageBox(msg, "Dial Up Manager Notification",
199 wxOK | wxICON_INFORMATION,
200 GetTopWindow());
201 }
202
203 // ----------------------------------------------------------------------------
204 // main frame
205 // ----------------------------------------------------------------------------
206
207 // frame constructor
208 MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
209 : wxFrame((wxFrame *)NULL, -1, title, pos, size)
210 {
211 // set the frame icon
212 SetIcon(wxICON(mondrian));
213
214 // create a menu bar
215 wxMenu *menuFile = new wxMenu;
216
217 menuFile->Append(NetTest_Dial, "&Dial\tCtrl-D", "Dial default ISP");
218 menuFile->Append(NetTest_HangUp, "&HangUp\tCtrl-H", "Hang up modem");
219 menuFile->AppendSeparator();
220 menuFile->Append(NetTest_About, "&About...\tCtrl-A", "Show about dialog");
221 menuFile->AppendSeparator();
222 menuFile->Append(NetTest_Quit, "E&xit\tAlt-X", "Quit this program");
223
224 // now append the freshly created menu to the menu bar...
225 wxMenuBar *menuBar = new wxMenuBar;
226 menuBar->Append(menuFile, "&File");
227
228 // ... and attach this menu bar to the frame
229 SetMenuBar(menuBar);
230
231 CreateStatusBar(2);
232 }
233
234
235 // event handlers
236
237 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
238 {
239 // TRUE is to force the frame to close
240 Close(TRUE);
241 }
242
243 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
244 {
245 wxString msg;
246 msg.Printf(_T("This is the network functions test sample.\n"
247 "© 1999 Vadim Zeitlin"));
248
249 wxMessageBox(msg, _T("About NetTest"), wxOK | wxICON_INFORMATION, this);
250 }
251
252 void MyFrame::OnHangUp(wxCommandEvent& WXUNUSED(event))
253 {
254 if ( wxGetApp().GetDialer()->HangUp() )
255 {
256 wxLogStatus(this, "Connection was succesfully terminated.");
257 }
258 else
259 {
260 wxLogStatus(this, "Failed to hang up.");
261 }
262 }
263
264 void MyFrame::OnDial(wxCommandEvent& WXUNUSED(event))
265 {
266 wxLogStatus(this, "Preparing to dial...");
267 wxYield();
268 wxBeginBusyCursor();
269
270 if ( wxGetApp().GetDialer()->Dial("Free", "zeitlin", "") )
271 {
272 wxLogStatus(this, "Dialing...");
273 }
274 else
275 {
276 wxLogStatus(this, "Dialing attempt failed.");
277 }
278
279 wxEndBusyCursor();
280 }
281
282 void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
283 {
284 // disable this item while dialing
285 event.Enable( !wxGetApp().GetDialer()->IsDialing() );
286 }
287
288 void MyFrame::OnIdle(wxIdleEvent& WXUNUSED(event))
289 {
290 static int s_isOnline = -1; // not TRUE nor FALSE
291
292 bool isOnline = wxGetApp().GetDialer()->IsOnline();
293 if ( s_isOnline != (int)isOnline )
294 {
295 s_isOnline = isOnline;
296
297 SetStatusText(isOnline ? "Online" : "Offline", 1);
298 }
299 }