]>
git.saurik.com Git - wxWidgets.git/blob - samples/dialup/nettest.cpp
0ad2bd433035546fd17bd696a6d2662dd738c3d9
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating network-related functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers (this file is usually all you
28 // need because it includes almost all "standard" wxWidgets headers
33 #if !wxUSE_DIALUP_MANAGER
34 #error You must set wxUSE_DIALUP_MANAGER to 1 in setup.h!
37 #include "wx/dialup.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // Define a new application type, each program should derive a class from wxApp
44 class MyApp
: public wxApp
47 // override base class virtuals
48 // ----------------------------
50 // this one is called on application startup and is a good place for the app
51 // initialization (doing it here and not in the ctor allows to have an error
52 // return: if OnInit() returns false, the application terminates)
53 virtual bool OnInit();
55 // called before the application termination
59 void OnConnected(wxDialUpEvent
& event
);
61 // accessor to dial up manager
62 wxDialUpManager
*GetDialer() const { return m_dial
; }
65 wxDialUpManager
*m_dial
;
70 // Define a new frame type: this is going to be our main frame
71 class MyFrame
: public wxFrame
75 MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
77 // event handlers (these functions should _not_ be virtual)
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
80 void OnHangUp(wxCommandEvent
& event
);
81 void OnDial(wxCommandEvent
& event
);
82 void OnEnumISPs(wxCommandEvent
& event
);
83 void OnCheck(wxCommandEvent
& event
);
84 void OnUpdateUI(wxUpdateUIEvent
& event
);
86 void OnIdle(wxIdleEvent
& event
);
89 // any class wishing to process wxWidgets events must use this macro
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // IDs for the controls and the menu commands
110 // ----------------------------------------------------------------------------
111 // event tables and other macros for wxWidgets
112 // ----------------------------------------------------------------------------
114 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
115 EVT_DIALUP_CONNECTED(MyApp::OnConnected
)
116 EVT_DIALUP_DISCONNECTED(MyApp::OnConnected
)
119 // the event tables connect the wxWidgets events with the functions (event
120 // handlers) which process them. It can be also done at run-time, but for the
121 // simple menu events like this the static method is much simpler.
122 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
123 EVT_MENU(NetTest_Quit
, MyFrame::OnQuit
)
124 EVT_MENU(NetTest_About
, MyFrame::OnAbout
)
125 EVT_MENU(NetTest_HangUp
, MyFrame::OnHangUp
)
126 EVT_MENU(NetTest_Dial
, MyFrame::OnDial
)
127 EVT_MENU(NetTest_EnumISP
, MyFrame::OnEnumISPs
)
128 EVT_MENU(NetTest_Check
, MyFrame::OnCheck
)
130 EVT_UPDATE_UI(NetTest_Dial
, MyFrame::OnUpdateUI
)
132 EVT_IDLE(MyFrame::OnIdle
)
135 // Create a new application object: this macro will allow wxWidgets to create
136 // the application object during program execution (it's better than using a
137 // static object for many reasons) and also declares the accessor function
138 // wxGetApp() which will return the reference of the right type (i.e. MyApp and
142 // ============================================================================
144 // ============================================================================
146 // ----------------------------------------------------------------------------
147 // the application class
148 // ----------------------------------------------------------------------------
150 // `Main program' equivalent: the program execution "starts" here
153 if ( !wxApp::OnInit() )
156 // Create the main application window
157 MyFrame
*frame
= new MyFrame(_T("Dial-up wxWidgets demo"),
158 wxPoint(50, 50), wxSize(450, 340));
160 // Show it and tell the application that it's our main window
164 // Init dial up manager
165 m_dial
= wxDialUpManager::Create();
167 if ( !m_dial
->IsOk() )
169 wxLogError(wxT("The sample can't run on this system."));
172 wxLog::GetActiveTarget()->Flush();
175 // do it here, OnExit() won't be called
182 frame
->SetStatusText(GetDialer()->IsAlwaysOnline() ? _T("LAN") : _T("No LAN"), 2);
183 #endif // wxUSE_STATUSBAR
192 // exit code is 0, everything is ok
196 void MyApp::OnConnected(wxDialUpEvent
& event
)
199 if ( event
.IsOwnEvent() )
201 msg
= event
.IsConnectedEvent() ? wxT("Successfully connected")
202 : wxT("Dialing failed");
204 wxLogStatus(wxEmptyString
);
208 msg
= event
.IsConnectedEvent() ? wxT("Just connected!")
209 : wxT("Disconnected");
215 // ----------------------------------------------------------------------------
217 // ----------------------------------------------------------------------------
220 MyFrame::MyFrame(const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
)
221 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
, pos
, size
)
224 wxMenu
*menuFile
= new wxMenu
;
226 menuFile
->Append(NetTest_Dial
, _T("&Dial\tCtrl-D"), _T("Dial default ISP"));
227 menuFile
->Append(NetTest_HangUp
, _T("&HangUp\tCtrl-H"), _T("Hang up modem"));
228 menuFile
->AppendSeparator();
229 menuFile
->Append(NetTest_EnumISP
, _T("&Enumerate ISPs...\tCtrl-E"));
230 menuFile
->Append(NetTest_Check
, _T("&Check connection status...\tCtrl-C"));
231 menuFile
->AppendSeparator();
232 menuFile
->Append(NetTest_About
, _T("&About...\tCtrl-A"), _T("Show about dialog"));
233 menuFile
->AppendSeparator();
234 menuFile
->Append(NetTest_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
236 // now append the freshly created menu to the menu bar...
237 wxMenuBar
*menuBar
= new wxMenuBar
;
238 menuBar
->Append(menuFile
, _T("&File"));
240 // ... and attach this menu bar to the frame
244 // create status bar and fill the LAN field
246 static const int widths
[3] = { -1, 100, 60 };
247 SetStatusWidths(3, widths
);
248 #endif // wxUSE_STATUSBAR
254 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
256 // true is to force the frame to close
260 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
263 msg
.Printf( wxT("This is the network functions test sample.\n")
264 wxT("(c) 1999 Vadim Zeitlin") );
266 wxMessageBox(msg
, wxT("About NetTest"), wxOK
| wxICON_INFORMATION
, this);
269 void MyFrame::OnHangUp(wxCommandEvent
& WXUNUSED(event
))
271 if ( wxGetApp().GetDialer()->HangUp() )
273 wxLogStatus(this, wxT("Connection was successfully terminated."));
277 wxLogStatus(this, wxT("Failed to hang up."));
281 void MyFrame::OnDial(wxCommandEvent
& WXUNUSED(event
))
283 wxLogStatus(this, wxT("Preparing to dial..."));
287 if ( wxGetApp().GetDialer()->Dial() )
289 wxLogStatus(this, wxT("Dialing..."));
293 wxLogStatus(this, wxT("Dialing attempt failed."));
299 void MyFrame::OnCheck(wxCommandEvent
& WXUNUSED(event
))
301 if(wxGetApp().GetDialer()->IsOnline())
302 wxLogMessage(wxT("Network is online."));
304 wxLogMessage(wxT("Network is offline."));
307 void MyFrame::OnEnumISPs(wxCommandEvent
& WXUNUSED(event
))
310 size_t nCount
= wxGetApp().GetDialer()->GetISPNames(names
);
313 wxLogWarning(wxT("No ISPs found."));
317 wxString msg
= _T("Known ISPs:\n");
318 for ( size_t n
= 0; n
< nCount
; n
++ )
320 msg
<< names
[n
] << '\n';
327 void MyFrame::OnUpdateUI(wxUpdateUIEvent
& event
)
329 // disable this item while dialing
330 event
.Enable( !wxGetApp().GetDialer()->IsDialing() );
333 void MyFrame::OnIdle(wxIdleEvent
& WXUNUSED(event
))
335 static int s_isOnline
= -1; // not true nor false
337 bool isOnline
= wxGetApp().GetDialer()->IsOnline();
338 if ( s_isOnline
!= (int)isOnline
)
340 s_isOnline
= isOnline
;
343 SetStatusText(isOnline
? _T("Online") : _T("Offline"), 1);
344 #endif // wxUSE_STATUSBAR